Nearby lessons

28 of 35

Spring - File Upload and Download

📌 What You Will Learn
  • 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:

Example02
Loading editor…

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:

Example05
Loading editor…

commons-fileupload-1.4.jar commons-io-2.6.jar — uploadform.jsp

Example06
Loading editor…

commons-fileupload-1.4.jar commons-io-2.6.jar — status.jsp

Example07
Loading editor…

commons-fileupload-1.4.jar commons-io-2.6.jar — UploadController.java

Example08
Loading editor…

commons-fileupload-1.4.jar commons-io-2.6.jar — ds-servlet.xml

Example09
Loading editor…

commons-fileupload-1.4.jar commons-io-2.6.jar — web.xml

Example10
Loading editor…

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:

Example11
Loading editor…

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:

Example12
Loading editor…

Prepare Controller class — downloadpage.jsp

Example13
Loading editor…

Prepare Controller class — DownloadController.java

Example14
Loading editor…

Prepare Controller class — ds-servlet.xml

Example15
Loading editor…

Prepare Controller class — web.xml

Example16
Loading editor…
📝 Key Takeaways
  • 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.