gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:47:07 27 4
gpt4 key购买 nike

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

这是我尝试上传文件时的生产日志:

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)

这是我期望的开发日志:

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

生产 tomcat:

  • server.info=Apache Tomcat/8.5.23
  • server.number=8.5.23.0
  • server.built=2017 年 9 月 28 日 10:30:11 世界标准时间

开发 tomcat(嵌入或在 docker 中)

  • server.info=Apache Tomcat/8.5.23
  • server.number=8.5.23.0
  • server.built=2017 年 9 月 28 日 10:30:11 世界标准时间

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

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

这是失败的 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

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

最佳答案

我终于找到了如何解决我的上传问题。

我必须使用 "/upload" 配置 MultipartConfigElement @Bean ,这是我的 UploadController 的端点.

关于java - 在 Docker 上运行时,Spring Boot 上传功能在生产环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61008074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com