I’m trying to upload a file (like image file) into a Restful server like this:
curl -X POST -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" --data-binary @sample.jpg http://localhost:3000/test
However, the request doesn’t seem to be processed by my Restful server resource.
- How to upload a file (small and large ) to a Spring web resource
- Is the “attachment” type upload is totally different than uploading through a simple Form upload?
- If the Content-Type:
multipart/form-data; boundary=...how can I get the actual content type, like: “image/jpeg” etc.
This is the code snippet for my web resource:
try
{
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator fileIterator = upload.getItemIterator(req);
while (fileIterator.hasNext()) {
FileItemStream item = fileIterator.next();
byte[] content = ByteStreams.toByteArray(item.openStream());
}
...
}
My concern is the type of client-side upload that the code can support, I use Rest Console Chrome plugin and upload attachment which works with this code, but the cURL code above is not working.
Also, what are the possible FileItemIterators that can be retrieved from a HttpServletRequest?
Here is the complete code of my restful web service resource: http://bit.ly/QwTOLw
You might want to take a look at the Spring manual’s section on Spring’s multipart (file upload) support. Make sure you have a
MultipartResolverbean declared in your context and consider mapping the file as aMultipartFilein a form-backing object so you can have Spring handle resolving/reading the multipart data itself (rather than trying to parse the FileItemIterator, ServletFileUpload etc yourself).