我正在尝试使用多部分请求在远程系统的文件夹中写入文件,而上传大小超过1 MB时会出现错误:
“stacktrace”:[“org.springframework.web.multipart.MultipartException:无法解析多部分servlet请求;嵌套异常是java.io.IOException:org.apache.tomcat.util.http.fileupload.FileUploadBase $ IOFileUploadException:处理multipart / form-data请求失败./upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp(权限被拒绝)
我尝试在app.properties文件中设置一些属性
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.file-size-threshold=10MB
spring.servlet.multipart.max-file-size = 10MB
spring.servlet.multipart.max-request-size = 10MB
spring.servlet.multipart.file-size-threshold=10MB
控制器方法:
@RestController
@RequestMapping(path = PcoCloudServicesController.PATH, produces = MediaType.APPLICATION_JSON_VALUE)
@MultipartConfig(
location = "/tmp",
fileSizeThreshold = 10485760,
maxFileSize = 10485760,
maxRequestSize = 10485760)
public class PcoCloudServicesController {
private static final String TENANT_ID = "tenantId";
@PostMapping("/writefile")
public JsonNode writeFileToFolderInPco(
HttpServletRequest request,
@RequestParam("file") MultipartFile file,
@RequestParam("file-data") String fileData) {
logger.debug(
"Controller method for writing file payload: {} and name: {}",
file,
file.getOriginalFilename());
IPCoCloudServices pcoService =
PCoCloudServicePersistance.getInstance(
(String) request.getAttribute(StringConstants.TENANT_ID));
return pcoService.writeFileToFolderInPco(file, fileData);
}
}
}
在这里,我尝试使用@MultipartConfig注释仍然无法解析请求/upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp(权限被拒绝)
完整的堆栈跟踪:
"stacktrace":["org.springframework.web.multipart.MultipartException: Failed to 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. /upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp (Permission denied)","\tat org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:123)","\tat org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:114)","\tat org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:87)","\tat org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:71)","\tat org.springframework.web.multipart.support.MultipartResolutionDelegate.resolveMultipartArgument(MultipartResolutionDelegate.java:98)","\tat org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:164)","\tat org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:107)","\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:126)","\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:166)","\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)","\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)","\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)","\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)","\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)","\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)","\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)","\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)","\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)","\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:661)","\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)","\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:742)","\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)","\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)","\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)","\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)","\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)","\tat org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)","\tat org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)","\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)","\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)","\tat org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)","\tat org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)","\tat org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)","\tat org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)","\tat org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)","\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)","\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)","\tat com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter.doFilterRequest(RequestLoggingFilter.java:123)","\tat com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter.doFilter(RequestLoggingFilter.java:74)","\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)","\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)","\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)","\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)","\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)","\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)","\tat com.sap.xs.java.valves.ErrorReportValve.invoke(ErrorReportValve.java:66)","\tat ch.qos.logback.access.tomcat.LogbackValve.invoke(LogbackValve.java:256)","\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)","\tat com.sap.xs.security.UserInfoValve.invoke(UserInfoValve.java:19)","\tat com.sap.xs.statistics.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:43)","\tat com.sap.xs.logging.catalina.RuntimeInfoValve.invoke(RuntimeInfoValve.java:40)","\tat org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:679)","\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)","\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)","\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)","\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)","\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)","\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)","\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)","\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)","\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)","\tat java.lang.Thread.run(Thread.java:836)","Caused by: java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. /upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp (Permission denied)","\tat org.apache.catalina.connector.Request.parseParts(Request.java:2950)","\tat org.apache.catalina.connector.Request.getParts(Request.java:2811)","\tat org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1084)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:347)","\tat org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:94)","\t... 81 more","Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. /upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp (Permission denied)","\tat org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297)","\tat org.apache.catalina.connector.Request.parseParts(Request.java:2902)","\t... 90 more","Caused by: java.io.FileNotFoundException: /upload_c8f18d91_ba00_4810_96de_422cdd2e9b75_00000016.tmp (Permission denied)","\tat java.io.FileOutputStream.open0(Native Method)","\tat java.io.FileOutputStream.open(FileOutputStream.java:270)","\tat java.io.FileOutputStream.<init>(FileOutputStream.java:213)","\tat java.io.FileOutputStream.<init>(FileOutputStream.java:162)","\tat org.apache.tomcat.util.http.fileupload.DeferredFileOutputStream.thresholdReached(DeferredFileOutputStream.java:151)","\tat org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.checkThreshold(ThresholdingOutputStream.java:200)","\tat org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.write(ThresholdingOutputStream.java:126)","\tat org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:105)","\tat org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68)","\tat org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293)","\t... 91 more"] }
经过一番挣扎后,终于找到了导致实际问题的原因。在Spring Dispatcher Servlet中,没有默认的多部分解析器。所以我们需要注入一个名为“multipartResolver”的bean,如下所示:
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10485760);
return multipartResolver;
}
这里使用setMaxUploadSize方法我定义了我想要支持的最大文件大小。它解决了我的最大文件大小问题。但是在这里我们需要在常见的异常处理程序类中捕获MaxUploadSizeExceededException。有关更多详细信息,您可以通过此链接https://www.baeldung.com/spring-maxuploadsizeexceeded