Spring's multipart (fileupload) support - Java-Springs

Introduction

Spring's built-in multipart support handles file uploads in web applications. You enable this multipart support with pluggable Multi part Resolver objects,defined in the org. spring frame work. web. multi part package. Spring provides a Multi part Resolver for use with Commons FileUpload).

By default, Spring does no multipart handling, because some developers want to handle multiparts themselves.You enable Spring multipart handling by adding a multipart resolver to the web application's context.Each request is inspected to see if it contains a multipart.If no multipart is found, the request continues as expected.If a multipart is found in the request, the Multi part Resolver that has been declared in your context is used. After that, the multipart attribute in your request is treated like any other attribute.

Using the MultipartResolver

The following example shows how to use the CommonsMultipartResolver:

<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="100000"/>
</bean>

Of course you also need to put the appropriate jars in your classpath for the multipart resolver to work. In the case of the Commons Multi part Resolver, you need to use commons-fileupload.jar.When the Spring Dispatcher Servlet detects a multi-part request, it activates the resolver that has been declared in your ontext and hands over the request. The resolver then wraps the current HttpServletRequest into a Multi part Http Servlet Request that supports multipart file uploads. Using the Multi part Http Servlet Request, you can get information about the multiparts contained by this request and actually get access to the multipart files themselves in your controllers.

Handling a file upload in a form

After the MultipartResolver completes its job, the request is processed like any other. First, create a form with a file input that will allow the user to upload a form. The encoding attribute (enctype="multipart/form-data") lets the browser know how to encode the form as multipart request:

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="/form" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>

The next step is to create a controller that handles the file upload. This controller is very similar to a normal annotated @Controller, except that we use Multi part Http Servlet Request or MultipartFile in the method parameters:

@Controller
public class FileUpoadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
}

Note how the @Request Param method parameters map to the input elements declared in the form.In this example, nothing is done with the byte[], but in practice you can save it in a database, store it on the file system, and so on.

Finally, you will have to declare the controller and the resolver in the application context:

<beans>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<!-- Declare explicitly, or use <context:annotation-config/> -->
<bean id="fileUploadController" class="examples.FileUploadController"/>
</beans>

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics