Download Pdf File using Spring Mvc Rest Controller

Spring Configuration File

Lets define the spring specific configurations in springexamples-servlet.xml file. This file is located under /WEB-INF/.. folder.
Here we are using annotation to define the RestController class and its request handler. To process all the annotation we have provided base package com.technicalkeeda
DefaultAnnotationHandlerMapping is used to map request with class and/or methods that are annotated with @RequestMapping annotation
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  />
  base-package="com.technicalkeeda" />
     
  class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   name="prefix">
   /WEB-INF/jsp/
name="suffix"> .jsp

RestController to Download File

Simple Spring Mvc RestController to download the pdf file. Follow below steps to download the sample "abc.pdf" file.
  1. Read @PathVariable value (fileName)
  2. Locate the pdf file using ClassPathResource under classpath folder.
  3. Set HttpHeaders like Content TypeContent Disposition and other Access-Control headers.
  4. Return the response. Here I have used ResponseEntity with an InputStreamResource along with header values and its http OK status
@RestController
@RequestMapping("/download")
public class DownloadFileRestController {
 @Autowired
 ServletContext context;

 @RequestMapping(value = "/pdf/{fileName:.+}", method = RequestMethod.GET, produces = "application/pdf")
 public ResponseEntity<InputStreamResource> download(@PathVariable("fileName") String fileName) throws IOException {
  System.out.println("Calling Download:- " + fileName);
  ClassPathResource pdfFile = new ClassPathResource("downloads/" + fileName);
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.parseMediaType("application/pdf"));
  headers.add("Access-Control-Allow-Origin", "*");
  headers.add("Access-Control-Allow-Methods", "GET, POST, PUT");
  headers.add("Access-Control-Allow-Headers", "Content-Type");
  headers.add("Content-Disposition", "filename=" + fileName);
  headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
  headers.add("Pragma", "no-cache");
  headers.add("Expires", "0");

  headers.setContentLength(pdfFile.contentLength());
  ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(
    new InputStreamResource(pdfFile.getInputStream()), headers, HttpStatus.OK);
  return response;

 }

Comments