我正在使用Spring框架,但不太熟悉它。我正在编写一个REST API,特别是一个处理文件上传的POST方法,该文件是可选的。
我试图简化方法,但仍有问题。这就是我所拥有的
@RequestMapping(method = RequestMethod.POST, value="/items")
public String create(@RequestParam(value="file", required=false) MultipartFile file) {
return "Create";
}
当我在表单请求中包含文件时,该方法正常工作,但是当我没有文件时(因为我希望它是可选的)。我尝试用@RequestParam
参数替换HttpServletRequest
,产生相同的结果。
我使用以下堆栈跟踪得到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.<init>(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.<init>(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)
我在网上搜索时只发现了一个帖子,并且它在春天谈到这可能是一个错误。我一直坚持这个问题一段时间了,非常感谢一些帮助。
由于
答案 0 :(得分:3)
这是我最终解决的问题。
@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
}
}
答案 1 :(得分:3)
我可以看到问题有点旧,但我在这里提出答案,因为上传文件主题总是存在。 我在这个问题的上下文中测试了Spring Boot 1.3.5.RELEASE。 我将rest服务定义如下,当文件没有附加到multipart / form-data时它可以工作。 我使用RestTemplate客户端测试了服务。
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> upload(@RequestPart(value = "name", required = false) String name,
@RequestPart(value = "file", required = false) MultipartFile file)
答案 2 :(得分:0)
我有同样的问题,只是添加required=false
为我工作。请在下面找到示例代码,
@RequestMapping(method = RequestMethod.POST, value="/items")
public String create(@RequestParam(value ="file", required=false) MultipartFile file) {
return "Create";
}