I have a path like POST /animals/1/images. I have read in the documentation that you can declare MultipartFile as a parameter and it will contain the file. But is this right? I mean, when you only have one file to upload (or an array of files) do I need to use the content type multipart/form-data? If not, how should the method signature be?
Add a comment
|
2 Answers
You can use this code fragment:
@POST
@Path("/uploadfile")
public void upload(File file) {
//TODO code goes here
}
2 Comments
Max von Hippel
Hi Tarcisio, and welcome to Stack Overflow! Could you maybe add a written explanation of why (or why not) the content type
multipart/form-data is needed?Tarcisio Cardoso
Because besides being easier to treat the post method on the server side the "multipart / form-data" encode the data providing greater guarantee. Otherwise the body will have to be treated to separate the data from the attachment.
As far as I know if you are uploading any file with POST via HTTP protocol, you have to use multipart/form-data. The form-data is just a name since usually (always?) when you are sending something via HTTP you use form.
Using MultipartFile parameter is a valid solution for this case - of course unless you prefer to write a whole Command object with MultipartFile as its only property, which would be preferred way, according to conventions.
1 Comment
sam
If your form contain other information than just this file, I would recommend you to use the default Spring solution for upload (with Apache upload solution), and to avoid the knew solution using Servlet 3, because you will have problem with UTF-8 encoding. I said that just in case because I spend lots of time with this matter.