This code is a RestEasy code for handling upload:
@Path("/fileupload")
public class UploadService {
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form)
{
// Handle form
}
}
Is there anything similar using Spring that can handle MultipartForm
just like this?
Here is an example showing how you could use MVC Annotations to achieve something similar in Spring:
You should be able to post to this endpoint from the html form, assuming the name of the file element is 'myFile'. Your form could look like the following:
The @InitBinder code is important because it instructs Spring to convert the files to a byte array, which can then be turned into the MultipartFile
Spring includes has a multipartresolver that relies on commons-fileupload, so to use it you have to include it in your build.
In your applicationContext.xml
In your controller, use org.springframework.web.multipart.MultipartFile.