NutzCN Logo
问答 现在上传成功是返回一个图片路径,我想直接返回上传的图片到页面怎么改?
发布于 2718天前 作者 qq_ce1cdaf7 2136 次浏览 复制 上一个帖子 下一个帖子
标签:
@At("/html4")
	@AdaptBy(type=UploadAdaptor.class, args="ioc:upload")
	//上传
	public Object html4(@Param("f")TempFile tmpFile, AdaptorErrorContext errCtx) {
		if (errCtx != null) {
			log.info(errCtx.getAdaptorErr());
			return false;
		}
		if (tmpFile == null || tmpFile.getFile().length() < 1024) {
			return false;
		}
		
		
		log.debug(tmpFile.getMeta().getFileLocalName());
		
		
		
		File file = tmpFile.getFile();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		System.out.println(Files.getSuffixName(file).toLowerCase());
		System.out.println(webPath(uuid));
		String dest = webPath(uuid) + "." + Files.getSuffixName(file).toLowerCase();
		System.out.println();
		String smallPath = webPath(uuid)  + Files.getSuffixName(file).toLowerCase();
		try {
			Images.zoomScale(file, new File(smallPath), 128, 128, Color.BLACK);
			file.renameTo(new File(dest));
		} catch (Throwable e) {
			log.info(e);
			return false;
		}
		
		
		return "../upload/images/" + uuid + "."+ Files.getSuffixName(file).toLowerCase();
	}
	
	public String webPath(String path) {
		return Mvcs.getServletContext().getRealPath("/upload/images/") + path;
	}

现在上传成功是返回一个图片路径,我想直接返回上传的图片到页面怎么改?

15 回复
@Ok("raw:jpg")

return new File(return "../upload/images/" + uuid + "."+ Files.getSuffixName(file).toLowerCase());

多写了return

我删了括号里的return,您看看我写的对吗?


@At("/html4") @Ok("raw:jpg") @AdaptBy(type=UploadAdaptor.class, args="ioc:upload") //上传 public Object html4(@Param("f")TempFile tmpFile, AdaptorErrorContext errCtx) { if (errCtx != null) { log.info(errCtx.getAdaptorErr()); return false; } if (tmpFile == null || tmpFile.getFile().length() < 1024) { return false; } log.debug(tmpFile.getMeta().getFileLocalName()); File file = tmpFile.getFile(); String uuid = UUID.randomUUID().toString().replaceAll("-", ""); System.out.println(Files.getSuffixName(file).toLowerCase()); System.out.println(webPath(uuid)); String dest = webPath(uuid) + "." + Files.getSuffixName(file).toLowerCase(); System.out.println(); String smallPath = webPath(uuid) + Files.getSuffixName(file).toLowerCase(); try { Images.zoomScale(file, new File(smallPath), 128, 128, Color.BLACK); file.renameTo(new File(dest)); } catch (Throwable e) { log.info(e); return false; } return new File( "../upload/images/" + uuid + "."+ Files.getSuffixName(file).toLowerCase()); //return "../upload/images/" + uuid + "."+ Files.getSuffixName(file).toLowerCase(); } public String webPath(String path) { return Mvcs.getServletContext().getRealPath("/upload/images/") + path; }

试了,报404
2017-07-14 10:18:59,669 org.nutz.mvc.view.RawView.render(RawView.java:108) DEBUG - File downloading ... F:\ljx\eclipse..\upload\images\fa0bd81f682e4f3da25c8a6a741e9cf0.jpg
2017-07-14 10:18:59,670 org.nutz.mvc.view.RawView.render(RawView.java:110) DEBUG - File downloading ... Not Exist : F:\ljx\eclipse..\upload\images\fa0bd81f682e4f3da25c8a6a741e9cf0.jpg
2017-07-14 10:19:22,986 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:92) DEBUG - Found mapping for [POST] path=/upload/html4 : UploadModule.html4(UploadModule.java:32)
2017-07-14 10:19:22,986 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:149) DEBUG - Get 'uploadModule'<class com.welcome.moudle.UploadModule>

null
2017-07-14 10:19:22,986 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:186) DEBUG - Select Html4 Form upload parser --> /welcome/upload/html4
2017-07-14 10:19:22,987 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) DEBUG - FastUpload : /upload/html4
2017-07-14 10:19:22,987 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) DEBUG - info created
2017-07-14 10:19:22,987 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) DEBUG - Params map created - 0 params
2017-07-14 10:19:22,987 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) DEBUG - boundary:
------WebKitFormBoundaryzDXfXigwXtgZzWYg
2017-07-14 10:19:22,987 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) DEBUG - skip first boundary
2017-07-14 10:19:22,988 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) DEBUG - Reading...
2017-07-14 10:19:22,988 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[1612522R8-4.jpg],fieldName=[f]
2017-07-14 10:19:22,988 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) DEBUG - Upload Info: name=1612522R8-4.jpg,content_type=image/jpeg
2017-07-14 10:19:22,994 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[s]
2017-07-14 10:19:22,994 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[s] value=[]
2017-07-14 10:19:22,994 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:230) DEBUG - ...Done 6092 bytes readed
2017-07-14 10:19:22,995 com.welcome.moudle.UploadModule.html4(UploadModule.java:41) DEBUG - 1612522R8-4.jpg
jpg
E:\apache-tomcat-8.0.33\webapps\welcome\upload\images\64a3262585784a9295d9e3287647b7af

2017-07-14 10:19:23,008 org.nutz.mvc.view.RawView.render(RawView.java:108) DEBUG - File downloading ... F:\ljx\eclipse..\upload\images\64a3262585784a9295d9e3287647b7af.jpg
2017-07-14 10:19:23,009 org.nutz.mvc.view.RawView.render(RawView.java:110) DEBUG - File downloading ... Not Exist : F:\ljx\eclipse..\upload\images\64a3262585784a9295d9e3287647b7af.jpg

```
我的图片保存到E盘了,为什么报F盘文件找不到

怎么有2个点

return new File( "../upload/images/" + uuid + "."+ Files.getSuffixName(file).toLowerCase());
您指的是这两个点吗,这两个点不用也行,我删了也还是报错

那你得拼出一个合法的,存在的路径嘛

return new File(Mvcs.getServletContext().getRealPath("/upload/images/") + uuid + "."+ Files.getSuffixName(file).toLowerCase()));

点击提交之后 图片直接被下载下来了 在页面不能显示 而且页面没有跳转 该怎么样才能直接显示在页面上

哦,你是提交. 那就改一下,变成

return new FileInputStream(...);

好了,谢谢啦

返回return new FileInputStream(...);的时候我前端页面怎么获取

参照nutzwk的
上传后返回
return Result.success("上传成功", f + "_s" + suffix);

$('#file_upload').uploadifive({
            'auto': true,
            'multi': true,
            'removeCompleted':true,
            'width': '150px',
            'height': '30',
            'buttonText': '请选择合同附件图片',
            'fileType': 'image/*',
            'fileSizeLimit': 1024,
            'queueSizeLimit': 6,
            'formData': {},
            'queueID': 'queue',
            'uploadScript': '${base}/open/file/upload/dbimage',
            'onUploadComplete': function (file, data) {
                data = JSON.parse(data);
                if (data.code == 0) {
        			layer.msg(data.msg, {
        				icon: 1,
        				time: 1000
        				});
                    imgId++;
                    var c="divImg";
                    if(imgId==1){
                        c="divImgD";
                    }
                    $("#img").append("<div id='imgId"+imgId+"' class='"+c+"'>" +
                            "<img  onclick=\"setImg('imgId"+imgId+"')\" src='" + data.data + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                            //"<a style='float: right;padding-top: 4px;' class='btn-delUploadImg' onclick=\"delImg('imgId"+imgId+"')\"></i></div>");
                    		"<a href=\"javascript:void(0);\" style=\"float:right;padding:7px 5px;\" class=\"easyui-linkbutton\"  onclick=\"delImg('imgId"+imgId+"')\">删除</a></div>");
                    sort.destroy();
                    sort = Sortable.create(container);
                } else {
                    layer.msg(data.msg, {icon: 2,time: 2000});
                }
            },
            'onSelect' : ....
                    $('#file_upload').uploadifive('cancel', $('.uploadifive-queue-item').first().data('file'));
                }
            }
        });
添加回复
请先登陆
回到顶部