찾다

 >  Q&A  >  본문

java - spring mvc上传文件超过设定大小异常处理客户端无响应

spring mvc上传文件超过设定大小异常处理客户端无响应;

controller类

    @RequestMapping(path = "/file", method = RequestMethod.POST)
    public UploadMsg uploadFile(@RequestParam("uploadFile") MultipartFile file, String userCode) throws Exception {
        String fileId = genFileId();
        saveFileToSystem(file, fileId);
        UploadMsg uploadMsg = getUploadMsg(userCode, fileId, file.getSize());
        return uploadMsg;
    }
    
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<String> handleException(MaxUploadSizeExceededException ex) {
        System.out.println("=====================" + ex.getClass().getName());
        return ResponseEntity.ok("ok");
    }

spring配置文件配置文件大小不能超过1M

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="#{1*1024*1024}"/>
        <property name="resolveLazily" value="true"/>
    </bean>

下面是我用小于1M的文件访问时的情况

这是上传失败的情况

下面是我的日志打印情况

22:40:28,569 DEBUG http-apr-8080-exec-5 support.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'uploadFileController'
=====================org.springframework.web.multipart.MaxUploadSizeExceededException
22:40:28,574  WARN http-apr-8080-exec-5 commons.CommonsMultipartResolver:194 - Failed to perform multipart cleanup for servlet request
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 1048576 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6145455) exceeds the configured maximum (1048576)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:162)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver$1.initializeMultipart(CommonsMultipartResolver.java:134)
    at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getMultipartFiles(AbstractMultipartHttpServletRequest.java:126)
    at org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.getMultiFileMap(AbstractMultipartHttpServletRequest.java:106)
    at org.springframework.web.multipart.commons.CommonsMultipartResolver.cleanupMultipart(CommonsMultipartResolver.java:191)
    at org.springframework.web.servlet.DispatcherServlet.cleanupMultipart(DispatcherServlet.java:1107)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:106)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:141)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
    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)
Caused by: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6145455) exceeds the configured maximum (1048576)
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:968)
    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 org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:158)
    ... 35 more
=====================org.springframework.web.multipart.MaxUploadSizeExceededException

这句是我在@ExceptionHandler(MaxUploadSizeExceededException.class)注解的方法中打印的,我也在这个方法中作了响应处理,但是客户端访问的时候就会显示无响应,不知道是什么鬼,求大神解答!!!!

PHP中文网PHP中文网2806일 전827

모든 응답(3)나는 대답할 것이다

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:40:37

    spring mvc MaxUploadSizeExceededException 무한 루프 솔루션에 대한 답변을 여기에서 찾았습니다
    Tomcat의 버그라고 합니다. jetty로 실행해 보았는데 하위 버전에서는 문제가 없는 것으로 나타났습니다. tomcat, 아직 테스트해 보지 않았습니다.

    회신하다
    0
  • 黄舟

    黄舟2017-04-18 09:40:37

    으아악

    한 개가 빠졌는지 확인하세요@ResponseBody.

    회신하다
    0
  • PHP中文网

    PHP中文网2017-04-18 09:40:37

    Weblogic에도 이 문제가 있습니다. 이미지 업로드 시간이 초과되고 클라이언트 상태가 여전히 보류 중이며 응답이 없습니다. @ExceptionHandler 및 @ResponseBody가 맵 객체를 반환하도록 작성되었지만 클라이언트가 응답하지 않습니다. weblogic이 응답을 처리하지 않은 것으로 의심됩니다

    회신하다
    0
  • 취소회신하다