I have a web application with spring in which I do some file upload. Under eclipse, using Jetty (the maven plugin) it works perfectly. But when I deploy the application unde
Use @RequestPart
instead of @RequestParam
.
From the source:
Annotation that can be used to associate the part of a "multipart/form-data" request with a method argument. Supported method argument types include {@link MultipartFile} in conjunction with Spring's {@link MultipartResolver} abstraction, {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests, or otherwise for any other method argument, the content of the part is passed through an {@link HttpMessageConverter} taking into consideration the 'Content-Type' header of the request part. This is analogous to what @{@link RequestBody} does to resolve an argument based on the content of a non-multipart regular request.
Note that @{@link RequestParam} annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @{@link RequestParam} relies on type conversion via a registered {@link Converter} or {@link PropertyEditor} while @{@link RequestPart} relies on {@link HttpMessageConverter}s taking into consideration the 'Content-Type' header of the request part. @{@link RequestParam} is likely to be used with name-value form fields while @{@link RequestPart} is likely to be used with parts containing more complex content (e.g. JSON, XML).
Thanks to @Bart I was able to find the following simple solution :
In the intercepting method, use @ModelAttribute instead of @RequestParam :
@RequestMapping(value = IMPORT_PAGE, method = RequestMethod.POST)
public String recieveFile(@RequestParam("importType") String importType,
@ModelAttribute("file") UploadedFile uploadedFile, final HttpSession session)
{
MultipartFile multipartFile = uploadedFile.getFile();
Where UploadedFile is the following class :
public class UploadedFile
{
private String type;
private MultipartFile file;
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public void setFile(MultipartFile file)
{
this.file = file;
}
public MultipartFile getFile()
{
return file;
}
}
And it's working !!
Thanks everyone for your help.