NutzCN Logo
问答 修改upload.json中的上传文件大小参数不生效
发布于 285天前 作者 qq_fec37dcd 220 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

修改upload.json中的上传文件大小参数不生效

31 回复

贴配置,,入口方法

json配置

var ioc={
    tmpFilePool : {
        type : 'org.nutz.filepool.NutFilePool',
        // 临时文件最大个数为 1000 个
        args : [ "~/nutzwk/upload/tmp", 1000 ]
    },
    uploadImageContext : {
        type : 'org.nutz.mvc.upload.UploadingContext',
        singleton : false,
        args : [ { refer : 'tmpFilePool' } ],
        fields : {
        // 是否忽略空文件, 默认为 false
        ignoreNull : true,
        // 单个文件最大尺寸(大约的值,单位为字节,即 2097152 为 2M)
        maxFileSize : 666666600,
        // 正则表达式匹配可以支持的文件名
        nameFilter : '^(.+[.])(gif|jpg|png)$'
        }
    },
    imageUpload : {
        type : 'org.nutz.mvc.upload.UploadAdaptor',
        singleton : false,
        args : [ { refer : 'uploadImageContext' } ]
    },
    mpImageUpload : {
        type : 'org.nutz.mvc.upload.UploadAdaptor',
        singleton : false,
        args : [ { refer : 'uploadImageContext' } ]
    },
    uploadFileContext : {
        type : 'org.nutz.mvc.upload.UploadingContext',
        singleton : false,
        args : [ { refer : 'tmpFilePool' } ],
        fields : {
            // 是否忽略空文件, 默认为 false
            ignoreNull : true,
            // 单个文件最大尺寸(大约的值,单位为字节,即 20971520 为 20M)
            maxFileSize : 20971520,
            // 正则表达式匹配可以支持的文件名
            nameFilter : '^(.+[.])(gif|jpg|png|doc|docx|xls|xlsx|rar|zip|7z|flv|swf|mkv|avi|txt|xml|pdf|md|pptx|ppt|flv|swf|mkv|avi|rm|rmvb|wmv|mp4|mov|mpg|mpeg|class|jar)$'
        }
    },
    fileUpload : {
        type : 'org.nutz.mvc.upload.UploadAdaptor',
        singleton : false,
        args : [ { refer : 'uploadFileContext' } ]
    },
    uploadVideoContext : {
        type : 'org.nutz.mvc.upload.UploadingContext',
        singleton : false,
        args : [ { refer : 'tmpFilePool' } ],
        fields : {
            // 是否忽略空文件, 默认为 false
            ignoreNull : true,
            // 单个文件最大尺寸(大约的值,单位为字节,即 209715200 为 200M)
            maxFileSize : 209715200,
            // 正则表达式匹配可以支持的文件名
            nameFilter : '^(.+[.])(flv|swf|mkv|avi|rm|rmvb|wmv|mp4|mov|mpg|mpeg)$'
        }
    },
    videoUpload : {
        type : 'org.nutz.mvc.upload.UploadAdaptor',
        singleton : false,
        args : [ { refer : 'uploadVideoContext' } ]
    }
};
@Modules(scanPackage = true, packages = "com.meipin.modules")
@Ok("json:full")
@Fail("http:500")
@IocBy(type = ComboIocProvider.class, args = {"*json", "config/ioc/", "*anno", "com.meipin", "*tx", "*quartz", "*async"})
@Localization(value = "locales/", defaultLocalizationKey = "zh_CN")
@Encoding(input = "UTF-8", output = "UTF-8")
@Views({BeetlViewMaker.class, PdfViewMaker.class, MpJsonViewMaker.class})
@SetupBy(value = Setup.class)
@ChainBy(args = "config/chain/nutzwk-mvc-chain.json")
@SessionBy(ShiroSessionProvider.class)
public class Module {
}

贴入口方法的代码

@IocBean
@At("/open/file/upload")
public class UploadController {
    private static final Log log = Logs.get();

    @AdaptBy(type = UploadAdaptor.class, args = {"ioc:mpImageUpload"})
    @POST
    @At
    @Ok("json")
    @RequiresAuthentication
    //AdaptorErrorContext必须是最后一个参数
    public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (tf == null) {
                return Result.error("空文件");
            } else {
                BufferedImage image = Images.read(tf.getInputStream());

                String p = Globals.AppRoot;
                String f = Globals.AppUploadPath + "/image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/"
                        + "IMG_" + image.getWidth() + "_" + image.getHeight() + "@"
                        + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                Files.write(new File(p + f), tf.getInputStream());
                return Result.success("上传成功", Globals.AppBase + f);
            }
        } catch (Exception e) {
            return Result.error("系统错误");
        } catch (Throwable e) {
            return Result.error("图片格式错误");
        }
    }
}

@wendal

ioc:mpImageUpload --> uploadImageContext --> uploadImageContext

maxFileSize : 666666600

你传的是多少, 把报错信息及其报错之前的日志都贴一下

@wendal 文件大小是5.2m


2017-01-11 16:29:17,656 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:183) DEBUG - Select Html4 Form upload parser --> /open/file/qiniu/upload/image 2017-01-11 16:29:17,665 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) DEBUG - FastUpload : /open/file/qiniu/upload/image 2017-01-11 16:29:20,678 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) DEBUG - info created 2017-01-11 16:29:20,678 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) DEBUG - Params map created - 0 params 2017-01-11 16:29:20,679 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) DEBUG - boundary: ------WebKitFormBoundary2fNAOCUzkLRcFE5Q 2017-01-11 16:29:20,682 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) DEBUG - skip first boundary 2017-01-11 16:29:21,365 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) DEBUG - Reading... 2017-01-11 16:29:21,916 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[23_iso100_14mm.jpg],fieldName=[Filedata] 2017-01-11 16:29:21,917 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) DEBUG - Upload Info: name=23_iso100_14mm.jpg,content_type=image/jpeg 2017-01-11 16:29:22,112 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.UploadOutOfSizeException: File '23_iso100_14mm.jpg' out of size! at org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:171) at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:185) at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:61) at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:226) at org.nutz.mvc.upload.UploadAdaptor.adapt(UploadAdaptor.java:116) 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 com.meipin.common.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35) at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at com.meipin.common.processor.NutShiroProcessor.process(NutShiroProcessor.java:52) 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.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at com.meipin.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:37) at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) at com.meipin.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:21) 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:198) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at com.meipin.common.filter.RouteFilter.doFilter(RouteFilter.java:33) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 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:950) 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:620) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 2017-01-11 16:29:22,323 com.meipin.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [POST]URI=/open/file/qiniu/upload/image 4669ms

我加断点查看了UploadingContext初始化时一直都没我设置那个文件大小

类上标注的是 @At("/open/file/upload") 方法名称 image

所以全路径是 /open/file/upload/image

日志中显示的 /open/file/qiniu/upload/image

并非同一个方法

@wendal /open/file/qiniu/upload/image 这个一样也是不行

 @AdaptBy(type = UploadAdaptor.class, args = {"ioc:mpImageUpload"})
    @POST
    @At
    @Ok("json")
    //@RequiresAuthentication
    //AdaptorErrorContext必须是最后一个参数
    public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (tf == null) {
                return Result.error("空文件");
            } else {

                BufferedImage image = Images.read(tf.getInputStream());

                String imagePath = new QiNiuUpload().upload(InputStreamUtils.inputStreamToByte(tf.getInputStream()),
                        "IMG_" + image.getWidth() + "_" + image.getHeight() + "@");

                if (!Strings.isBlank(imagePath)) {
                    return Result.success("上传成功", imagePath);
                } else {
                    return Result.error(100, "上传失败");
                }

//                BufferedImage image = Images.read(tf.getInputStream());
//                System.out.println(image.getHeight());
//                System.out.println(image.getWidth());
//
//                //第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
//                Zone z = Zone.autoZone();
//                Configuration c = new Configuration(z);
//                //创建上传对象
//                UploadManager uploadManager = new UploadManager(c);
//                try {
//                    //调用put方法上传
//                    Response res = uploadManager.put(InputStreamUtils.inputStreamToByte(tf.getInputStream()), null, QiNiuUpload.getUpToken("IMG"));
//                    StringMap map = res.jsonToMap();
//
////                    System.out.println(res.jsonToMap());
//                } catch (QiniuException e) {
//                    return Result.error(101, "上传失败");
//                }
            }
        } catch (Exception e) {
            return Result.error("系统错误");
        } catch (Throwable e) {
            return Result.error("图片格式错误");
        }
    }

看上去配置没错,然后你说maxFileSize没有设置为你要的值(推测debug的是setMaxFileSize)

那,只剩下一个可能性了, 有2个uploadImageContext的ioc定义,全文搜索一下吧

搜索了下 好像只upload.json中有uploadImageContext这个参数,另外我把配置中所有文件大小都改为666666600 ,FastUploading中取的maxFileSize 大小还是 2097152 @wendal

会不会是缓存的原因?

@qq_fec37dcd 搜一下2097152

目前项目中除了注释中有这个数,还有就是有一个moxie.js中有这个数,其它没了

debug一下UploadingContext的几个构造方法

@wendal 一直走的这个方法

 public UploadingContext(FilePool pool) {
        charset = Encoding.UTF8;
        bufferSize = 8192;
        setFilePool(pool);
    }

@qq_fec37dcd 那maxFileSize不会无缘无故变呢,没debug过setMaxFileSize吗

有,但不知道从哪里调用了

debug时没有经过UploadAdaptor

修改nameFilter也不生效

顺着堆栈往上找嘛.

PS, 我觉得你把这个upload.js删了都不会报错, 99.99%是有其他配置文件存在

@wendal 已经找出问题了,我把json配置里的键名都改成带双引号就没问题了

@wendal 错了,不是上面的原因,是我把项目的依赖打包到jar包里了,项目修改了,这个配置没有修改,不好意思

@wendal 问个题外的问题,我执行mvn clean package项目自己打的那个jar包更新了,项目正常了,我怎么才能把这个jar移除掉呢?

@qq_fec37dcd 打包的时候排除掉?

对,我现在的lib目录里多一个项目名的jar包,这个我也是debug JsonLoader才发现的,也不知道什么时候进去了,所以想移除掉,我直接删除掉后,再编译还是有

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