2017-01-25 1 views
-1

das ist mein DateipfadWie kann ich die PDF-Datei im Frühjahr mvc herunterladen?

public final static String BOOKINGPDFFILE= "D:/Hotels/pdf/"; 

Dieser Code unten ist das, was ich geschrieben habe, pdf aus dem obigen Ressourcenordner

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()); 
    } 

} 

Antwort

0

Sie können so etwas wie dies versuchen:

Und in Ihrer Web-Seite können Sie den Link auf diese Weise schreiben:

<a href="/yourWebAppCtx/yourControllerRoot/downloadPdf" target="_blank"> download PDF </a> 

Angelo

1

Hier ist der Weg, hoffe, es hilft.

@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()); 
    } 
} 
+0

i auch Commons IO verwende aber in diesem Teil erhalte ich Fehler FileUtils.copyFile (Datei, Antwort. getOutputStream()); –

+0

Wenn Sie mehr Details über den Fehler und Ihre Spring MVC-Konfiguration lassen könnten, könnte ich Ihnen helfen. –

+0

Der Fehler ist im Grunde 404 nicht gefunden. Wenn ich den Download-Button klicke, wird es in neue Registerkarte umgeleitet und zeigt "404 nicht gefunden." Muss ich hinzufügen diesen Teil verwende ich 4.0.1 Spring Framework .. Datei für die PDF-Generierung –