this is my file path
public final static String BOOKINGPDFFILE= "D:/Hotels/pdf/";
This below code is what I have written to download pdf from the above resource folder
Pdf="column name in database  i used for storing in database"
@RequestMapping(value = "/getpdf/{pdf}", method = RequestMethod.GET)
public  void getPdf(@PathVariable("pdf") String fileName, HttpServletResponse response,HttpServletRequest request) throws IOException {
   try {
        File file = new File(FileConstant.BOOKINGPDFFILE + fileName+ ".pdf");
        Files.copy(file.toPath(),response.getOutputStream());
    } catch (IOException ex) {
        System.out.println("Contract Not Found");
        System.out.println(ex.getMessage());
    }
}
                Here is the way, hope it help.
@RequestMapping(value = "/getpdf/{pdf}", method = RequestMethod.GET)
public  void getPdf(@PathVariable("pdf") String fileName, HttpServletResponse response) throws IOException {
    try {
        File file = new File(FileConstant.BOOKINGPDFFILE + fileName+ ".pdf");
        if (file.exists()) {
            // here I use Commons IO API to copy this file to the response output stream, I don't know which API you use.
            FileUtils.copyFile(file, response.getOutputStream());
            // here we define the content of this file to tell the browser how to handle it
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
            response.flushBuffer();
        } else {
            System.out.println("Contract Not Found");
        }
    } catch (IOException exception) {
        System.out.println("Contract Not Found");
        System.out.println(exception.getMessage());
    }
}
                        You may try something like this:
@RequestMapping(method = { RequestMethod.GET }, value = { "/downloadPdf" })
    public ResponseEntity<InputStreamResource> downloadPdf()
    {
        try
        {
            File file = new File(BOOKINGPDFFILE);
            HttpHeaders respHeaders = new HttpHeaders();
            MediaType mediaType = MediaType.parseMediaType("application/pdf");
            respHeaders.setContentType(mediaType);
            respHeaders.setContentLength(file.length());
            respHeaders.setContentDispositionFormData("attachment", file.getName());
            InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
            return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
        }
        catch (Exception e)
        {
            String message = "Errore nel download del file "+idForm+".csv; "+e.getMessage();
            logger.error(message, e);
            return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
And in your web page you can write the link in this way:
<a href="/yourWebAppCtx/yourControllerRoot/downloadPdf" target="_blank"> download PDF </a>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With