Nearby lessons
28 of 35Spring - File Upload and Download
- Build a form that can carry a file, and say why enctype matters
- Configure a MultipartResolver in the Spring configuration file
- Write controllers for both the upload and the download direction
Uploading and downloading files in Spring Web MVC — the multipart form, the MultipartResolver configuration, the commons-fileupload and commons-io jars, the upload controller, and a controller that streams a file back to the browser.
File Upload in Spring WEB MVC
IN general, in web applications, it is very frequent to perform upload operations inorder to upload files from client to Server.
To perform File Upload operation in SPring WEB MVC applications, Spring WEB MVC framework has provided some predefined library in the form of "org.springframework.web.multipart.commons.CommonsMultipartResolver"
If we want to perform Upload operation in Spring WEB MVC applications then we have to use the following steps.
Prepare a user form with File components — uploadform.jsp
- In User form , we must specify "POST" as "method" attribute in form tag.
- in User form, we must provide "multipart/form-data" as "enctype" attribute in form tag.
EX:
EX:UploadController.java
@Controller public class UploadController {
----- @RequestMapping(value="/upload", method=RequestMethod.POST) public ModelAndView uploadFiles(@RequestParam MultipartFile file) { String status = ""; try{ String file_Name = multipartFile.getOriginalFilename(); FileOutputStream fos = new FileOutputStream("E:/uploads/"+file_Name); byte[] bt = multipartFile.getBytes(); fos.write(bt); status = "SUCCESS";
} catch (Exception e) { status = "FAILURE"; e.printStackTrace();
} return new ModelAndView("status", "status", status); } }
- Configure CommonsMultipartResolver class as bean with the name "multipartResolver" in
spring configuration file
EX: ds-servlet.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Note: For File Upload operation, Spring Framework is using Commons File Upload library internally, so that, we must provide the following JAR files in web application lib folder along with all Spring JARs.
commons-fileupload-1.4.jar commons-io-2.6.jar — index.jsp
Example:
commons-fileupload-1.4.jar commons-io-2.6.jar — uploadform.jsp
commons-fileupload-1.4.jar commons-io-2.6.jar — status.jsp
commons-fileupload-1.4.jar commons-io-2.6.jar — UploadController.java
commons-fileupload-1.4.jar commons-io-2.6.jar — ds-servlet.xml
commons-fileupload-1.4.jar commons-io-2.6.jar — web.xml
File Downloading in Spring WEB MVC — downloadpage.jsp
In general, in web applications, it is very frequent operation to perform download operation.
If we want to perform Download operation in Spring WEB MVC Application then we have to use the following steps.
- Prepare a File with "Download" button and with the downloadable resource.
EX:
Prepare Controller class — index.jsp
IN Controller class and in action method we have to use the following steps.
- Set Content Type as "APPLICATION/OCTET-STREAM".
response.setContentType("APPLICATION/OCTET-STREM);
- Set "content-disposition" response header to response object.
response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
- Create File and FileInputStream object with the downloadable resource.
File file = new File("E:\\images\\Java_Python.jpg"); FileInputStream fis = new FileInputStream(file);
- Create OutputStream from response object.
OutputStream os = response.getOutputStream()
- Get bit by bit from FileInputStream and write bit by bit to OutputStream int val = fis.read();
while(val != -1) { os.write(val); val = fis.read();
Example:
Prepare Controller class — downloadpage.jsp
Prepare Controller class — DownloadController.java
Prepare Controller class — ds-servlet.xml
Prepare Controller class — web.xml
- Without a MultipartResolver bean the upload silently arrives as an ordinary parameter
- commons-fileupload and commons-io are required alongside the Spring jars
- Downloading is a matter of setting the response headers and writing the bytes yourself