创建一个使用Java的HTTP multipart/form-data请求。

huangapple go评论103阅读模式
英文:

creating an HTTP multipart/form-data request with java

问题

抱歉,这段文本太长,无法一次性翻译完成。请分为多个较小的部分,我将分段为您翻译。以下是第一部分的翻译:

  1. 我正在尝试创建一个Util类,使用StringBuilder生成一个仅包含multipart/form-data请求的Http请求。所以这是在将文件和文本属性传递给我的工具类之后的输出:
  2. Content-Type: multipart/form-data; boundary=------------------------464842921662621063770712
  3. Content-Length: 608
  4. Host: localhost:8080
  5. Connection: Keep-Alive
  6. Accept-Encoding: gzip
  7. User-Agent: okhttp/4.2.2
  8. --------------------------464842921662621063770712
  9. Content-Disposition: form-data; name="file"; filename="upload-file.txt"
  10. Content-Type: text/plain; charset=UTF-8
  11. Lorem ipsum dolor sit amet, consectetur adipisicing elit. At, beatae consectetur, doloribus iste nemo pariatur, provident quae quam quis repellendus totam voluptatibus. Doloribus, itaque, porro? Ab autem deleniti magni quidem!
  12. --------------------------464842921662621063770712
  13. Content-Disposition: form-data; name="businessContext"
  14. Content-Type: text/plain; charset=UTF-8
  15. business-context
  16. --------------------------464842921662621063770712--

如果您需要翻译文本的其他部分,请将其分开发送。

英文:

i am trying to create Util class to generate an Http multipart/form-data request using only StringBuilder.
so this is the output that i have after passing a file and a text attribute to my util class:

  1. Content-Type: multipart/form-data; boundary=------------------------464842921662621063770712
  2. Content-Length: 608
  3. Host: localhost:8080
  4. Connection: Keep-Alive
  5. Accept-Encoding: gzip
  6. User-Agent: okhttp/4.2.2
  7. --------------------------464842921662621063770712
  8. Content-Disposition: form-data; name="file";filename="upload-file.txt"
  9. Content-Type: text/plain; charset=UTF-8
  10. Lorem ipsum dolor sit amet, consectetur adipisicing elit. At, beatae consectetur, doloribus iste nemo pariatur, provident quae quam quis repellendus totam voluptatibus. Doloribus, itaque, porro? Ab autem deleniti magni quidem!
  11. --------------------------464842921662621063770712
  12. Content-Disposition: form-data; name="businessContext"
  13. Content-Type: text/plain; charset=UTF-8
  14. business-context
  15. --------------------------464842921662621063770712--
  16. ]

but when sending this multipart-request with okhttp to runngin spring-boot application it throws this error:

  1. org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
  2. at org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:520) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  3. at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.findNextItem(FileItemIteratorImpl.java:232) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  4. at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.<init>(FileItemIteratorImpl.java:131) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  5. at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:255) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  6. at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:279) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  7. at org.apache.catalina.connector.Request.parseParts(Request.java:2869) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  8. at org.apache.catalina.connector.Request.parseParameters(Request.java:3202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  9. at org.apache.catalina.connector.Request.getParameterNames(Request.java:1170) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  10. at org.apache.catalina.connector.Request.getParameterMap(Request.java:1149) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  11. at org.apache.catalina.connector.RequestFacade.getParameterMap(RequestFacade.java:443) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  12. at org.springframework.web.servlet.DispatcherServlet.lambda$logRequest$2(DispatcherServlet.java:964) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  13. at org.springframework.core.log.LogFormatUtils.traceDebug(LogFormatUtils.java:86) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  14. at org.springframework.web.servlet.DispatcherServlet.logRequest(DispatcherServlet.java:956) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  15. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:911) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  16. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  17. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  18. at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
  19. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  20. at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
  21. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  22. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  23. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
  24. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  25. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  26. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  27. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  28. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  29. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  30. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  31. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  32. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  33. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  34. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  35. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  36. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  37. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  38. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
  39. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.37.jar:9.0.37]
  40. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.37.jar:9.0.37]
  41. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.37.jar:9.0.37]
  42. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.37.jar:9.0.37]
  43. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.37.jar:9.0.37]
  44. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.37.jar:9.0.37]
  45. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.37.jar:9.0.37]
  46. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.37.jar:9.0.37]
  47. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.37.jar:9.0.37]
  48. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) [tomcat-embed-core-9.0.37.jar:9.0.37]
  49. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.37.jar:9.0.37]
  50. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_252]
  51. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_252]
  52. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.37.jar:9.0.37]
  53. at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]

could someone please help me verify what is wrong with my multipart request!

答案1

得分: 0

turns out, i was using a bad end of the line "\n", the correct one is "\r\n"

see this thread

英文:

turns out, i was using a bad end of the line "\n", the correct one is "\r\n"

see this thread

huangapple
  • 本文由 发表于 2020年8月7日 08:22:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63293515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定