Glassfish的 - 从服务器检索

问题描述:

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和服务器的初学者级别,所以请告诉我如何进行下载操作。

您可以使用任何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目录。