Does anyone have any idea of any products or libraries like Apache Commons FileUpload that will deal with PUT file uploads?
Any friendly advice or direction would be very much appreciated!
Full Story:
We are starting to implement a file upload rest(like) service for our java webapp, but there doesn't seem to be any 'easy' solutions for dealing with file uploads via the HTTP PUT method.
We are hoping to find a library like the Apache Commons FileUpload project, but something that doesn't only deal with "Form-based File Upload in HTML" and/or "multipart/form-data".
We really like FileUpload's ability to store files temporarily, move those files when asked, and then clean up the temporary files after they are no longer used. We also like the fact that Spring will automajically bind the MultipartFile List to our command object and its just available for us to use when it gets into our other html form based file upload controllers.
Full Stack Background:
- Spring MVC (3.2.3.RELEASE)
- Tomcat 7
- We are trying to follow a layered architecture (UI, services/business logic, persistence)
Thank you for your time!
The following url is an example that shows the ability to upload a file from the request's InputStream. The code gets the work done but it isn't quite production quality.
https://boplicity.nl/confluence/display/spring/Using+HTTP+PUT+and+Spring+MVC+to+upload+files
We are using the following curl command to test our webservice:
curl -v -k -X PUT --data-binary @"c:/java/files/tempfilename.txt" https://localhost:8443/api/file/tempfilename.txt
xwoker then gave the following nice curl example:
curl -v -X PUT -T "myfile" http://localhost:8080/mytargetfilename
I'm not aware of any libraries that fulfill your requirements. But if you don't mind to do some coding, I think nice way to create something similar would be to write your own
that converts the request body to File in the tmp directory:
In the controller you would define your method with:
Don't forget to register your FileMessageConverter, e.g.
The curl command to invoke the upload:
It is quite easy to get spring to respond correctly to a File Upload request for a HTTP PUT Method.
All it takes is overriding the isMultipart() method in a customized MultipartResolver class.
What is really important is that the default MultipartResolver is extended so that the isMultipart() method will return a true for either a POST or PUT request.
In general there are two default MultipartResolver implementations: CommonsMultipartResolver (used with Apache Commons FileUpload) and StandardServletMultipartResolver (used with Servlet 3.0+ Part API).
Since we are using Apache Commons FileUpload we extended the CommonsMultipartResolver class.
There is documentation on the MultipartResolver's Javadoc page that explains how to properly define a customized MultipartResolver for your application (emphasis is mine):
For an xml configured application it will look close to the following:
For an annotation configured application it will look close to the following:
More information on Annotation configuration of MultipartResolver.