gpt4 book ai didi

java - 带有多部分文件上传的 Spring REST API 不适用于可选参数

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:26 30 4
gpt4 key购买 nike

我正在使用 Spring 框架,但不太熟悉它。我正在编写一个 REST API,特别是一个处理文件上传的 POST 方法,其中文件是可选的。

我已尝试简化该方法,但仍然存在问题。这是我的东西

@RequestMapping(method = RequestMethod.POST, value="/items")
public String create(@RequestParam(value="file", required=false) MultipartFile file) {
return "Create";
}

当我在我的表单请求中包含一个文件时,该方法工作正常,但是当我没有文件时(因为我希望它是可选的)。我尝试用 HttpServletRequest 参数替换 @RequestParam 产生相同的结果。

我收到以下堆栈跟踪的 500 错误:

java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:99)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.&lt;init&gt;(StandardMultipartHttpServletRequest.java:77)
org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1067)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:906)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.apache.catalina.connector.Request.parseParts(Request.java:2806)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.apache.tomcat.util.http.fileupload.FileUploadException: Stream ended unexpectedly
org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:311)
org.apache.catalina.connector.Request.parseParts(Request.java:2737)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
</pre></p><p><b>root cause</b> <pre>org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
org.apache.tomcat.util.http.fileupload.MultipartStream.readHeaders(MultipartStream.java:480)
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:889)
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.&lt;init&gt;(FileUploadBase.java:854)
org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:259)
org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:283)
org.apache.catalina.connector.Request.parseParts(Request.java:2737)
org.apache.catalina.connector.Request.parseParameters(Request.java:3096)
org.apache.catalina.connector.Request.getParameter(Request.java:1145)
org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:382)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

我在网上搜索时只找到一篇帖子,它谈到这可能是 Spring 中的一个错误。我已经在这个问题上停留了一段时间,非常感谢您的帮助。

谢谢

最佳答案

这是我最终得到的解决方案。

@RequestMapping(method = RequestMethod.POST, value = "/object")
ResponseEntity<Object> create(@RequestParam("is_file") boolean isFile, HttpServletRequest request) {
if(isFile) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
} else{
// You can add other parameters (as long as it isn't a multipart parameter) and do other stuff here
}

关于java - 带有多部分文件上传的 Spring REST API 不适用于可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424177/

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