上传大文件不会在Tomcat中工作

时间:2014-11-19 13:09:44

标签: java spring tomcat upload

我有一个在Tomcat7上运行的Web应用程序(Java / spring / hibernate)。

我设法轻松上传文件,但现在我需要能够上传大文件(我有一个36MB的文件要上传)。

每当我上传一个大文件时,我都会收到405(Method Not Allowed)错误

日志显示以下行:

415157         2014-11-19 14:53:03,662 INFO [http-bio-443-exec-52] com.eloan.controller.api.UploadFileController WFPIRSUM.PRN uploaded! 
428860         2014-11-19 14:53:17,365 WARN [http-bio-443-exec-52] org.springframework.web.servlet.PageNotFound Request method 'POST' not supported

第一行来自我的代码:

@RequestMapping(value = "/boi", method = RequestMethod.POST)
@CheckSessionAnnotation(type = 99)
@ResponseBody
public Integer upload(MultipartHttpServletRequest request, HttpServletResponse response) throws E000EloanException {
    LenderDetails anss = usService.getCurrentUserLenderDetails();
    int totalLines = 0;

    if (null == anss) { // user not in session...
        throw new E666UserNotInSessionException("User not in Session");
    }

    // this is a fix for IE
    response.setContentType("text/html");

    // 1. build an iterator
    Iterator<String> itr = request.getFileNames();
    MultipartFile mpf = null;

    String doc_type = request.getParameter("doc_type");
    List<String> lines = new ArrayList<String>();
    // 2. get each file
    while (itr.hasNext()) {

        mpf = request.getFile(itr.next());
        LOG.info(mpf.getOriginalFilename() + " uploaded! ");

        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(mpf.getInputStream(), "Windows-1255"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                // LOG.info("read [" + line + "] doc_tyep [" + doc_type +
                // "]");
                lines.add(line);
                // boiService.addBankOfIsraelEntry(line, doc_type);
                totalLines++;
            }

        } catch (IOException e) {
            LOG.error("File upload error for ", e);
            throw new E107IllegalCallException("File Upload Error");
        }

    } ...

为了支持大文件,我尝试了以下方法(将multipart配置中的最大大小设置为注释和函数调用:

@EnableWebMvc
@Configuration
@EnableAsync
@MultipartConfig(maxFileSize = 52428800)
public class EloanApiWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
/**
 * Supports FileUploads.
 */
@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(52428800);
    return multipartResolver;
}
}

我尝试在server.xml连接器中设置它:

<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
           maxThreads="150" scheme="https" secure="true" maxPostSize="52428800"
           keystoreFile="/var/lib/tomcat7/conf/keystore" keystorePass="Lj6xK8sk"
           clientAuth="false" sslProtocol="TLS" server="eLoanSecureBSD-Server" />

在web.xml文件中:

<multipart-config>
        <max-file-size>52428800</max-file-size>
        <max-request-size>52428800</max-request-size>
    <file-size-threshold>52428800</file-size-threshold>
    </multipart-config>

它不起作用!

顺便说一句 - 当我在eclipse中运行时(使用主服务器中的JETTY服务器),它运行完美而没有任何问题......

谢谢你们!

1 个答案:

答案 0 :(得分:0)

最后,问题只在于Tomcat的内存配置。 我失去了内存异常,我在日志中没有C,直到我将org.springframework日志设置为调试(实际上无法理解为什么会出现这种情况)。

在给予进程更多内存之后,它开始运行良好。

我在这里添加了我所见过的日志,如果它感兴趣的话......

112361 2014-11-19 16:23:42,500 INFO [http-bio-443-exec-22] com.eloan.controller.api.UploadFileController wfpirsum.zip uploaded! 120026 2014-11-19 16:23:50,165 DEBUG [http-bio-443-exec-22] org.springframework.web.multipart.commons.CommonsMultipartResolver Cleaning up multipart file [files[]] with original filename [wfpirsum.zip], stored at [/var/lib/tomcat7/work/Catalina/localhost/_/upload_54d020ea_b425_473f_a2c7_3dc7d8d4a330_00000001.tmp] 120043 2014-11-19 16:23:50,182 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.DispatcherServlet Could not complete request org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) [tomcat-servlet-api-3.0.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) [tomcat-servlet-api-3.0.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [tomcat-catalina-7.0.52.jar:7.0.52] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) [tomcat-coyote-7.0.52.jar:7.0.52] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) [tomcat-coyote-7.0.52.jar:7.0.52] at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315) [tomcat-coyote-7.0.52.jar:7.0.52] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_65] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_65] at java.lang.Thread.run(Thread.java:745) [na:1.7.0_65] java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOfRange(Arrays.java:2694) ~[na:1.7.0_65] at java.lang.String.<init>(String.java:203) ~[na:1.7.0_65] at java.io.BufferedReader.readLine(BufferedReader.java:349) ~[na:1.7.0_65] at java.io.BufferedReader.readLine(BufferedReader.java:382) ~[na:1.7.0_65] at com.eloan.controller.api.UploadFileController.uploadBankOfIsrael(UploadFileController.java:91) ~[UploadFileController.class:na] at com.eloan.controller.api.UploadFileController$$FastClassByCGLIB$$aa339559.invoke(<generated>) ~[spring-core-3.2.0.RELEASE.jar:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at com.eloan.aspect.CheckSessionAspect.checkSession(CheckSessionAspect.java:53) ~[CheckSessionAspect.class:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_65] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_65] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_65] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_65] at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) ~[spring-aop-3.2.0.RELEASE.jar:3.2.0.RELEASE] at com.eloan.controller.api.UploadFileController$$EnhancerByCGLIB$$9a9d7ba.uploadBankOfIsrael(<generated>) ~[spring-core-3.2.0.RELEASE.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_65] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_65] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_65] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_65] at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE] 120056 2014-11-19 16:23:50,195 DEBUG [QuartzScheduler_quartzScheduler-ip-10-101-11-1001416406956159_ClusterManager] org.springframework.jdbc.datasource.DataSourceUtils Returning JDBC Connection to DataSource 120056 2014-11-19 16:23:50,195 DEBUG [QuartzScheduler_quartzScheduler-ip-10-101-11-1001416406956159_ClusterManager] org.springframework.scheduling.quartz.LocalDataSourceJobStore ClusterManager: Check-in complete. 120089 2014-11-19 16:23:50,228 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.DispatcherServlet DispatcherServlet with name 'appServlet' processing POST request for [/404.html] 120089 2014-11-19 16:23:50,228 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping Looking up handler method for path /404.html 120103 2014-11-19 16:23:50,242 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping Did not find handler method for [/404.html] 120104 2014-11-19 16:23:50,243 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Matching patterns for request [/404.html] are [/*.html] 120104 2014-11-19 16:23:50,243 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping URI Template variables for request [/404.html] are {} 120104 2014-11-19 16:23:50,243 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Mapping [/404.html] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@507f1f06] and 1 interceptor 120105 2014-11-19 16:23:50,244 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver Resolving exception from handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@507f1f06]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 120106 2014-11-19 16:23:50,245 DEBUG [http-bio-443-exec-22] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver Resolving exception from handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@507f1f06]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported 120106 2014-11-19 16:23:50,245 WARN [http-bio-443-exec-22] org.springframework.web.servlet.PageNotFound Request method 'POST' not supported
相关问题