NutzCN Logo
问答 文件上传文件格式处理
发布于 2582天前 作者 Saviour 2264 次浏览 复制 上一个帖子 下一个帖子
标签:

我使用了nutz的文件上传适配器配置如下

@At("/uploadRoomPic")
	@Ok("json")
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:picUpload" })
	public ServiceResult<Map> uploadRoomPic(HttpServletRequest req, @Param("rid")String rid, @Param("roomPic")TempFile tf, AdaptorErrorContext err) {
		ServiceResult<Map> sr = new ServiceResult<Map>();
		if (err != null && err.getAdaptorErr() != null) {
			System.out.println(err.toString());
			sr.setSuccess(false);
			sr.setCode("2440");
			sr.setMsg("文件格式不对");
			return sr;
		}
                ....
}

IOC配置如下

tmpFilePool : {
            type : 'org.nutz.filepool.NutFilePool',
            // 临时文件最大个数为 1000 个
            args : [ {java:'$utils.getPath("/WEB-INF/tmp")'}, 1000 ]
        },
        uploadFileContext : {
            type : 'org.nutz.mvc.upload.UploadingContext',
            singleton : false,
            args : [ { refer : 'tmpFilePool' } ],
            fields : {
                // 是否忽略空文件, 默认为 false
                ignoreNull : true,
                // 单个文件最大尺寸(大约的值,单位为字节,即 1048576 为 1M)
                maxFileSize : 209715200,
                // 正则表达式匹配可以支持的文件名
                nameFilter : '^(.+[.])(jpg|png|jpeg)$'
            } 
        },
        picUpload : {
            type : 'org.nutz.mvc.upload.UploadAdaptor',
            singleton : false,
            args : [ { refer : 'uploadFileContext' } ] 
        }

此时如果上传文件大小和后缀符合ioc中所配置项则正常没有问题,当大小或者后缀不符合时则后台报错。
报错如下

2017-11-27 11:13:49,770 org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:231) INFO  - Adapter Error catched , but I found AdaptorErrorContext param, so, set it to args, and continue
org.nutz.mvc.upload.UploadUnsupportedFileNameException: Unsupport file name 'stone.xls' 
	at org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:140)
	at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:189)
	at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:65)
	at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:226)
	at org.nutz.mvc.upload.UploadAdaptor.adapt(UploadAdaptor.java:120)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:28)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:722)

我想问一下我能否避免报错 在报错前自己处理一下文件大小和后缀 或者在什么地方可以捕获此异常 还是只能自己写新的适配器来处理

4 回复

你可以去掉maxFileSize 和nameFilter配置,然后在代码里面自行判断哦

uploadFileContext : {
            type : 'org.nutz.mvc.upload.UploadingContext',
            singleton : false,
            args : [ { refer : 'tmpFilePool' } ],
            fields : {
                // 是否忽略空文件, 默认为 false
                ignoreNull : true,
                // 单个文件最大尺寸(大约的值,单位为字节,即 1048576 为 1M)
                maxFileSize : 209715200,
                // 正则表达式匹配可以支持的文件名
                nameFilter : '^(.+[.])(jpg|png|jpeg)$'
            } 
        },

你这里都写了只支持上传格式为:jpg|png|jpeg

哦 是哈!思维太定型!高!

添加回复
请先登陆
回到顶部