Reputation: 51
I'm using the Apache Commons FileUpload Stream API and it works perfectly with spring-boot-starter-parent 1.5.14.RELEASE:
public ResponseEntity<String> uploadFile(HttpServletRequest request) throws Exception
{
if (!ServletFileUpload.isMultipartContent(request))
{
return responseService.badRequest(request, "file", "Request is not multipart, please 'multipart/form-data' enctype for your form.");
}
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext())
{
FileItemStream item = iter.next();
if (!item.isFormField())
{
save(item.openStream(), item.getName());
return responseService.success(request);
}
}
return responseService.badRequest(request, "file", "Must contain a file stream");
}
I also have spring.http.multipart.enabled=false set in my properties file.
However, when I upgrade to 2.0.3.RELEASE then the FileItemIterator's hasNext() just returns false.
Has anyone been able to get this working with Spring 5/Springboot 2.0.x?
Upvotes: 5
Views: 1552
Reputation: 175
In your application.properties file set:
spring.servlet.multipart.enabled=false
If you have not set this you will find it exits appearing to close the stream immediately.
It is possible if you are following an older demo you have set the Spring Boot 1.x value of spring.http.multipart.enabled=false
. This has been Deprecated with Boot 2.0
That being said this is a good time to point out two specific resources:
1.) Spring Reference Documentation
Common Application Properties Spring 1.5.15
Common Application Properties Spring 2.0.X
These docs show changes between the Common Application Properties that you may not have been aware of. Leveraging an IDE such as IntelliJ it will call this out to you with the red-underlined squiggle with an info-tip (if you happen to be leveraging that).
2.) Spring Boot Migration Guide
Check out the Spring Boot 2.0 Migration Guide.
It receives regular updates and points out these types of pains alongside things such as tooling for migrating application.properties
, changes in defaults (which are significant), and some underpinnings that have changed.
I highly recommend this if you are migrating an application or just want to understand a little more about how your application will behave differently and some of the why.
Upvotes: 2