I did a POC for spring 3 rest multipart file upload. Its working fine. But when i tried integrating with my application i am facing issues.
It throws following exception:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found**"
Please let me know if I am wrong in any part of my code.
Beans:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="file" value="multipart/mixed" /> </map> </property> </bean> <!-- multipart resolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="50000000" /> </bean>
Controller:
@Controller public class MultipleFilesRecieve { @RequestMapping ( value = "/saveMultiple", method = RequestMethod.POST ) public String save( FileUploadForm uploadForm ) { List<MultipartFile> files = uploadForm.getFiles( ); List<String> fileNames = new ArrayList<String>( ); if ( null != files && files.size( ) > 0 ) { for ( MultipartFile multipartFile : files ) { String fileName = multipartFile.getOriginalFilename( ); fileNames.add( fileName ); } } return "multifileSuccess"; } }
multipart/form-data contains boundary to separate name/value pairs. The boundary acts like a marker of each chunk of name/value pairs passed when a form gets submitted. The boundary is automatically added to a content-type of a request header.
Postman handles this natively by selecting the form/data option for the body and automatically creates boundaries.
Spring Boot file uploader Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and. Send a response code to the client indicating the Spring file upload was successful.
multipart/form-data [RFC1867] The multipart/form-data content type is intended to allow information providers to express file upload requests uniformly, and to provide a MIME-compatible representation for file upload responses.
The problem isn't in your code - it's in your request. You're missing boundary in your multipart request. As it said in specification:
The Content-Type field for multipart entities requires one parameter, "boundary", which is used to specify the encapsulation boundary. The encapsulation boundary is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) followed by the boundary parameter value from the Content-Type header field.
This and this posts should also be helpful.
@sermolaev is right in his answer.
I want to share my experience related to this problem. I've encountered this problem in Postman, but I could not understand the root cause for it for a long time. My request template seemed to be correct cause Postman included boundary
in it...
Eventually I've discovered that when you're specifying Content-Type=multipart/form
header by yourself, it overrides the one added automatically by Postman. And this leads to the same error as yours. My solution was as simple as removing Content-Type
header.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With