当前位置: 首页 > 知识库问答 >
问题:

文件上载中断时“无法解析多部分servlet请求”

骆照
2023-03-14

我正在使用Spring Boot(1.5.7)和ng2文件上传(1.2.1)将文件上传到服务器。

所以我遇到了下一个问题,当文件上传开始并中断时(例如关闭浏览器选项卡),Spring Boot会记录下一个错误(两次):

2018-10-10 14:45:57.054 ERROR 11540 --- [http-nio-8282-exec-6] o.a.c.c.C.[.[.[.[dispatcherServlet]      : line[182] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket] with root cause
java.io.EOFException: Unexpected EOF read on the socket
    at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:362) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.Request.doRead(Request.java:476) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:344) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:369) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:190) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) ~[spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) [tomcat-embed-core-8.0.46.jar:8.0.46]
...

当我将@ExceptionHandler添加到@ControllerAdvice时:

@ExceptionHandler(MultipartException.class)
protected ResponseData handleFileUploadingError(Exception e) {
    log.warn("Failed to upload attachment", e);
    return ResponseData.with(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

和应用程序的下一个属性。属性:

spring.mvc.throw-exception-if-no-handler-found=true

现在,当文件上载中断时,@ExceptionHandler会按照应该的方式记录错误(下一个代码段),但在Spring Boot之后,仍然会不时从dispatcherServlet记录错误(如第一个代码段中所示)

2018-10-10 14:52:32.655 WARN 14392 --- [http-nio-8282-exec-9] c.c.i.c.a.ExceptionHandlerAdvice         : line[76] - Failed to upload attachment
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:79) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1104) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110) [spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176) [spring-security-oauth2-2.1.1.RELEASE.jar:?]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) [spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_161]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
Caused by: java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.apache.catalina.connector.Request.parseParts(Request.java:2876) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more
Caused by: java.io.EOFException: Unexpected EOF read on the socket
    at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:362) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.Request.doRead(Request.java:476) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:344) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:369) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:190) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more

以下是前端应用程序响应文件上传的部分代码:

this.uploader.onAfterAddingFile = (fileItem) => {
            this.setAuthTokenHeader();
            this.uploader.uploadItem(fileItem);
            this.uploadedFiles.push({
                id: null,
                originalName: fileItem.file.name,
                attachmentName: null
            });
            this.changedCallback(this.uploadedFiles);
            this.scrollToFilesUX();
        };

我不知道会出什么问题。先谢谢你。

另外,我注意到,如果@ExceptionHandler方法类型为void,并且除了记录错误之外什么都不做,那么一切都会正常工作

更新

我认为这没关系,因为控制器甚至没有被调用,因为我知道它会发生,因为文件首先上传到服务器(在这一步会引发异常),然后控制器执行。但这是我的控制器方法(类注释为@RestController):

@PostMapping(value = "/attachments")
    public ResponseData put(@RequestParam(value = "storeFile", required = false) MultipartFile multipartFile,
                            @RequestParam(required = false) List<DocumentType> documentTypes) throws IOException {
        try {
            return ResponseData.with(attachmentService.uploadTempFile(multipartFile, documentTypes));
        } catch (AttachmentValidationException e) {
            return ResponseData.with(e.getTempFileName(), e.getMessages(), HttpStatus.NOT_ACCEPTABLE);
        }
    }

更新2

这是chaned@ExceptionHandler方法。在这种情况下,Spring Boot不会记录任何错误(这很好)。但是现在,异常日志作为WARN记录两次。如果早期是我的WARN日志Spring Boot重复ERROR日志,现在是我的WARN日志重复(时间不同)

@ExceptionHandler(value = MultipartException.class)
    public void handleFileUploadingError(HttpServletResponse response, Exception exception) {
        log.warn("Failed to upload attachment", exception);
        try {
            response.sendError(Response.SC_INTERNAL_SERVER_ERROR, exception.getMessage());
        } catch (IOException e) {
            log.error("Failed to send error response", e);
        }
    }

共有3个答案

赏开宇
2023-03-14

在您的Web安全配置适配器中禁用csrf,我不确定这是什么,但我的一位专家朋友说禁用它可以发送多部分资源。

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
}
钮出野
2023-03-14

我已经调试了您的控制器建议,发现方法被调用了两次,所以它的日志记录警告出现了两次,所以似乎sendError再次发送响应以处理异常。。(不确定)。。

以下是我可以看到的两种解决方案,它们对我有效。。

方法1返回响应实体

@ExceptionHandler(MultipartException.class)
    public ResponseEntity handleFileUploadingError(MultipartException exception) {
        log.warn("Failed to upload attachment", exception);
        return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);

    }

方法2相似但返回字符串

    @ExceptionHandler(value = MultipartException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public String handleFileUploadingError(MultipartException exception) {
        logger.warn("Failed to upload attachment", exception);
        return exception.getMessage();
    }
上官飞
2023-03-14

我认为这种行为是正确的,因为您仍然在这里记录异常(e参数):

log.warn("Failed to upload attachment", e);

请注意,您的异常已从ERROR更改为WARN,就像您在yout异常处理程序中以WARN形式记录一样。

 类似资料:
  • 当我尝试用我的代码下载文件时,我出现了这样的错误: HTTP状态500-请求处理失败;嵌套异常是org.springframework.web.multipart.multipartException:无法解析多部分servlet请求;嵌套异常是java.io.ioException:临时上载位置[/tmp/tomcat.5139949927832460132.8080/work/tomcat/l

  • 我有下面的上传文件方法来上传多部分文件,我看到413请求实体太大,同时上传一个像28MB的大尺寸文件。在tomcat 9服务器配置中,我已经将设置为50MB,并且还尝试了下面链接中建议的设置。我已经在文件中添加了下面的属性。 我在下面的链接中尝试了一些建议,但仍然看到相同的错误:Spring Boot Upload Multipart 413请求实体太大

  • 我已经用Spring Initializr创建了一个Spring Boot 2演示应用程序,并在下面添加了控制器: 然后,我添加了一个包含此配置的应用程序.yaml 文件: 我的目标是让控制器在开始读取< code>payload文件之前解析并记录< code>metadata文件,但是< code > resolve-lazyly 设置似乎被Boot忽略了:控制器内部的代码在整个主体被读取之前不

  • Im使用Spring Boot,并希望使用控制器接收多部分文件上传。当发送文件时,我一直得到错误415不支持的内容类型响应,并且控制器从未到达 我尝试在HTML/JSP页面和使用RESTTemplate的独立客户端应用程序中使用Form:Action发送。所有尝试的结果都是一样的 从multipart文档看来,边界参数必须添加到multipart上载,但这似乎与控制器接收不匹配 我的控制器方法设置

  • 关于Spring MVC应用程序中的多部分文件上传问题,我已经看到了很多关于stackoverflow的答案。我一步一步地确保我不会重复别人犯过的错误。 这是我的表格 在pom文件中我有依赖项 因此,当我停在调试器中时,request对象显示文件已被接收,甚至存储在jBoss临时文件夹中,我并不感到惊讶。 你能指出Spring看不到上传的文件我会错过什么吗?

  • 我想从angularJS客户端上传文件。我已经启用了CSRF保护,它可以正常工作,但当我尝试上载文件时,会出现错误: 在请求参数“_CSRF”或标头“X-XSRF-Token”上发现无效的CSRF令牌“null”。 但是令牌在请求头中,并且是正确的!当我禁用CSRF保护时,我可以毫无问题地上传文件。 除此之外,CSRF保护工作正常。 以下是我当前的配置: 安全Configuration.java