Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultipartForm handling with Spring

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?

like image 939
quarks Avatar asked Nov 04 '22 15:11

quarks


1 Answers

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

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="<max file size>"/>
</bean>

In your controller, use org.springframework.web.multipart.MultipartFile.

@RequestMapping(method=RequestMethod.POST, value="/multipartexample")
public String examplePost(@RequestParam("fileUpload") MultipartFile file){
    // Handle form upload and return a view
    // ...
}
like image 192
pap Avatar answered Nov 08 '22 04:11

pap