在 Docker 上运行时,Spring Boot 上传功能在生产中不起作用

Posted

技术标签:

【中文标题】在 Docker 上运行时,Spring Boot 上传功能在生产中不起作用【英文标题】:Spring Boot upload feature doesn't work in production while running on Docker 【发布时间】:2020-07-15 09:33:05 【问题描述】:

在部署到生产环境后,我遇到了 Spring Boot 1.5.9 中的上传功能问题。

这是我尝试上传文件时production 的登录信息:

2020-04-03 08:27:42,031 DEBUG org.springframework.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/upload]
2020-04-03 08:27:42,052 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Looking up handler method for path /upload
2020-04-03 08:27:42,052 DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<?> com.icimatin.api.controllers.UploadController.saveUpload(java.lang.Long,java.lang.Long,org.springframework.web.multipart.MultipartFile)]
2020-04-03 08:27:42,053 DEBUG org.springframework.web.cors.DefaultCorsProcessor : Skip CORS processing: response already contains "Access-Control-Allow-Origin" header
2020-04-03 08:27:42,063 DEBUG org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod : Failed to resolve argument 2 of type 'org.springframework.web.multipart.MultipartFile'
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:193)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:109)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:167)
    at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.kopaxgroup.api.core.security.filter.CORSFilter.doFilter(CORSFilter.java:45)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:677)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

这是我预期的development 的登录:

DispatcherServlet with name 'dispatcherServlet' processing POST request for [/upload]
Found multipart file [file] of size 768065 bytes with original filename [987acd26-27eb-4006-980d-b52d590e32cb.png], stored at [/tmp/tomcat.8336796726960983145.8080/work/Tomcat/localhost/ROOT/upload_39cf59d7_5fbb_4d72_bb1b_85bcf4ae5d94_00000002.tmp]
Looking up handler method for path /upload
Returning handler method [public org.springframework.http.ResponseEntity<?> com.icimatin.api.controllers.UploadController.saveUpload(java.lang.Long,java.lang.Long,org.springframework.web.multipart.MultipartFile)]
Skip CORS processing: response already contains "Access-Control-Allow-Origin" header
Saving image to: upload/images
==>  Preparing: insert into "prom_photos"("digest","note","filename","prom_promotion_id") values(?,?,?,?) 
==> Parameters: 915b26746bd9585bb5538d196e6804bb(String), native(String), 987acd26-27eb-4006-980d-b52d590e32cb.png(String), 22(Long)
<==    Updates: 1
==>  Preparing: select currval('"prom_photos_id_seq"') 
==> Parameters: 
<==      Total: 1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Cleaning up multipart file [file] with original filename [987acd26-27eb-4006-980d-b52d590e32cb.png], stored at [/tmp/tomcat.8336796726960983145.8080/work/Tomcat/localhost/ROOT/upload_39cf59d7_5fbb_4d72_bb1b_85bcf4ae5d94_00000002.tmp]
Successfully completed request

productiontomcat:

server.info=Apache Tomcat/8.5.23 server.number=8.5.23.0 server.built=2017 年 9 月 28 日 10:30:11 UTC

development tomcat(嵌入或在 docker 内)

server.info=Apache Tomcat/8.5.23 server.number=8.5.23.0 server.built=2017 年 9 月 28 日 10:30:11 UTC

我可以看出的一个区别是,这个 Docker 映像是“FROM frolvlad/alpine-java:jdk8-slim”,而我正在从 Debian Stretch 开发嵌入式 Tomcat。

我现在已经在development(使用 Docker)中复制了production 环境,并且发生了同样的错误。

这是失败的 cURL 请求(复制自开发者工具):

curl 'http://localhost:8080/upload' \
   -H 'Connection: keep-alive' \
   -H 'Pragma: no-cache' \
   -H 'Cache-Control: no-cache' \
   -H 'accept: application/json' \
   -H 'Sec-Fetch-Dest: empty' \
   -H 'authorization: Bearer eyJhb...w8Q' \
   -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/80.0.3987.132 Safari/537.36' \
   -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryN52ik7rZuZDvSvzd' \
   -H 'Origin: http://localhost:19006' \
   -H 'Sec-Fetch-Site: same-site' \
   -H 'Sec-Fetch-Mode: cors' -H 'Referer: http://localhost:19006/professionels/verifier-la-promotion/test--23.html' \
   -H 'Accept-Language: en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,ru-RU;q=0.6,ru;q=0.5,vi-VN;q=0.4,vi;q=0.3' \
   --data-binary $'------WebKitFormBoundaryN52ik7rZuZDvSvzd\r\nContent-Disposition: form-data; name="typeId"\r\n\r\n1\r\n------WebKitFormBoundaryN52ik7rZuZDvSvzd\r\nContent-Disposition: form-data; name="id"\r\n\r\n23\r\n------WebKitFormBoundaryN52ik7rZuZDvSvzd\r\nContent-Disposition: form-data; name="file"; filename="64f0bc37-398a-491b-97ab-d2373fbb1cab.png"\r\nContent-Type: image/png\r\n\r\n\u0089PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0005\u0000\u0000\u0000\u0002Ð\u0008\u0006\u0000\u0000\u0000ÏÝV\u0000\u0000 \u0000IDATx^ä½i\u0093eÉq%vóeVõ\u008eÍ\u000c\u0002Áu8Æ/ÒüÉ\u0021Ð\u0000\u0017I¿mÄo4\u0018\u008eı\u0021A\u001a\u001b ... \u0088æF\u009f³\u0006Lî\u001fÄÌW7µ,˦_Ù\u000c\u0000\u008e\u0018\u00805hë gÆ:²þ\u008fþHÜFÿ\u0017ú¸|\u0001ÈÙ ~\u0000\u0000\u0000\u0000IEND®B`\u0082\r\n------WebKitFormBoundaryN52ik7rZuZDvSvzd--\r\n' \
   --compressed

这是成功的 cURL 请求:

curl 'http://localhost:8080/upload' \
    -H 'Connection: keep-alive' \
    -H 'Pragma: no-cache' \
    -H 'Cache-Control: no-cache' \
    -H 'accept: application/json' \
    -H 'Sec-Fetch-Dest: empty' \
    -H 'authorization: Bearer eyJhbGciO...8reENc' \
    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36' \
    -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7xhALLmrd7lgfprY' \
    -H 'Origin: http://localhost:19006' \
    -H 'Sec-Fetch-Site: same-site' \
    -H 'Sec-Fetch-Mode: cors' -H 'Referer: http://localhost:19006/professionels/verifier-la-promotion/test--24.html' \
    -H 'Accept-Language: en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,ru-RU;q=0.6,ru;q=0.5,vi-VN;q=0.4,vi;q=0.3' \
    --data-binary $'------WebKitFormBoundary7xhALLmrd7lgfprY\r\nContent-Disposition: form-data; name="typeId"\r\n\r\n1\r\n------WebKitFormBoundary7xhALLmrd7lgfprY\r\nContent-Disposition: form-data; name="id"\r\n\r\n24\r\n------WebKitFormBoundary7xhALLmrd7lgfprY\r\nContent-Disposition: form-data; name="file"; filename="955c04b4-4ba2-41e2-851f-d9eb22e81fb0.png"\r\nContent-Type: image/png\r\n\r\n\u0089PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0005\u0000\u0000\u0000\u0002Ð\u0008\u0006\u0000\u0000\u0000ÏÝV\u0000\u0000 \u0000IDATx^ä½i\u0093eÉq%vó...þ\u008fþHÜFÿ\u0017ú¸|\u0001ÈÙ ~\u0000\u0000\u0000\u0000IEND®B`\u0082\r\n------WebKitFormBoundary7xhALLmrd7lgfprY--\r\n' \
    --compressed

什么可能破坏了上传功能?

【问题讨论】:

您确定要发送相同的请求吗?似乎请求 multipart 没有完成。 是的@HakanDilek 我 100% 确定。我刚刚在 docker 中启动了开发环境,我得到了完全相同的结果。我添加了 cURL 失败和成功请求,以便您确认。 您能否将--verbose 添加到您的 curl 命令并在此处发布结果? 我才发现这个不好找,错误误导了。 【参考方案1】:

我终于找到了解决上传问题的方法。

我必须将MultipartConfigElement @Bean 配置为"/upload",这是我的UploadController 的端点。

【讨论】:

虽然我在想它应该是spring.http.multipart.location 的路径我也认为spring.http.multipart.location 是上传时存储的临时文件的位置我还是不明白为什么。 ..我一直都错了吗?很想知道更多。

以上是关于在 Docker 上运行时,Spring Boot 上传功能在生产中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在Docker容器中运行Spring Boot GraalVM原生镜像

无法从通过intellij运行的spring boot应用程序连接到我在docker上运行的kafka

在 Windows 上通过 Maven spring-boot:build-image 构建 docker 映像时如何定义架构 arm64?

Spring Boot + MySQL docker 容器

在 localhost 上运行的 Spring Boot 应用程序无法与在 docker 上运行的 redis 通信

如何将主机上运行的 Spring Boot 应用程序连接到 Docker 上运行的 Postgresql?