2016-08-19 42 views
0

PDF文件使用servlet中,我必須在根目錄下保存爲PDF文件(玻璃魚服務器),存儲在以下位置Glassfish的 - 從服務器檢索

D:\NetBeansProjects\pdfapp\build\web\pdf-test100.pdf 

PDFMergerUtility pdfMerger = new PDFMergerUtility(); 
pdfMerger.addSources(sources); 
String pdfFileName = "pdf-test100.pdf"; 
String contextPath = getServletContext().getRealPath("/"); 
    pdfMerger.setDestinationFileName(contextPath+pdfFileName); 
    pdfMerger.mergeDocuments(); 

文件我下一步我想從上面的路徑下載文件 jsp頁面我創建一個超鏈接,一旦點擊從上面的路徑下載的文件。

我是servlet和服務器的初學者級別,所以請告訴我如何進行下載操作。

回答

0

您可以使用任何servlet API教程。 Servlet API爲Glassfish或任何其他應用程序容器提供相同的行爲。例如。 this tutorial

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     performTask(request, response); 
} 
private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, 
      IOException { 

     String pdfFileName = "pdf-test100.pdf"; 
     String contextPath = getServletContext().getRealPath(File.separator); 
     File pdfFile = new File(contextPath + pdfFileName); 

     response.setContentType("application/pdf"); 
     response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName); 
     response.setContentLength((int) pdfFile.length()); 

     FileInputStream fileInputStream = new FileInputStream(pdfFile); 
     OutputStream responseOutputStream = response.getOutputStream(); 
     int bytes; 
     while ((bytes = fileInputStream.read()) != -1) { 
      responseOutputStream.write(bytes); 
     } 
} 

把你pdf-test100.pdf文件到WEB-INF目錄。