最近一个项目用到了apache-commons-fileupload组件 ,当客户端上传文件时,服务器却报了以下异常:
发现异常:
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (5309) exceeds the configured maximum (0)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115)
at com.knowledge.service.impl.FileServiceImpl.uploadFile(FileServiceImpl.java:38)
at com.knowledge.controller.FileOpServlet.doGet(FileOpServlet.java:31)
at com.knowledge.controller.FileOpServlet.doPost(FileOpServlet.java:78)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
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:169)
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:436)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)
代码如下:
DiskFileItemFactory factory = new DiskFileItemFactory();//文件项工厂
ServletFileUpload sfu = new ServletFileUpload(factory);
System.out.println(sfu.isMultipartContent(request));
sfu.setFileSizeMax(2*1024*1024*1024);//单个文件大小限制2G
sfu.setSizeMax(4*1024*1024*1024);//总文件大小限制4G
发现异常:
the request was rejected because its size (5309) exceeds the configured maximum (0) 这句话说明我设置的文件限制大小为0,可我设置的是2*1024*1024*1024(2G)于是看了<pre name="code" class="java">sfu.setFileSizeMax(2*1024*1024*1024)这个方法,参数是一个long型的值,而我传入的是int型的值,强转变成0了, 所以改为<pre name="code" class="java">sfu.setFileSizeMax((long)2*1024*1024*1024) 解决问题!