如何使用servlet将PDF文件发送到客户端。

时间:2016-04-08 17:04:55

标签: java web-services pdf file-upload

我尝试使用servlet将一个小代码发送到客户端。我已经开发了一个代码,下面给出了一个代码。当我在glassfish服务器上的NetBeans中运行此代码时,会出现一个弹出下载窗口,我点击打开选项它会说"格式有问题"而且我无法查看文件中的内容。 那么有人可以帮我解决这个逻辑错误吗?

  package com.transferPDF;

  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;

  public class PDFserver extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    int BUFF_SIZE = 1024;
    byte[] buffer = new byte[BUFF_SIZE];
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment;filename=E:/test.pdf");
    File filePDF=new File("E:/test.pdf");
    FileInputStream fis = new FileInputStream(filePDF);
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
       response.setContentLength((int) filePDF.length());
        OutputStream os = response.getOutputStream();

        try {
            int byteRead = 0;
            while ((byteRead = fis.read()) != -1) {
            os.write(buffer, 0, byteRead);

            }
            os.flush();
            } catch (Exception excp) {
            excp.printStackTrace();
        } finally {
            os.close();
            fis.close();
        }
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
public String getServletInfo() {
    return "Short description";
  }

}

2 个答案:

答案 0 :(得分:0)

使用以下标题

response.setHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");

答案 1 :(得分:0)

您只需要对您撰写PDF的方式进行一些更改。

我在这里获得了代码,以返回正确呈现Chrome的PDF。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        int BUFF_SIZE = 1024;
        byte[] buffer = new byte[BUFF_SIZE];
        response.setContentType("application/pdf");
        response.setHeader("Content-Type", "application/pdf");
        File filePDF = new File("c:\\my.pdf");
        FileInputStream fis = new FileInputStream(filePDF);     
        OutputStream os = response.getOutputStream();
        try
        {
            response.setContentLength((int) filePDF.length());
            int byteRead = 0;
            while ((byteRead = fis.read()) != -1)
            {
                os.write(byteRead);
            }
            os.flush();
        }
        catch (Exception excp)
        {
            excp.printStackTrace();
        }
        finally
        {
            os.close();
            fis.close();
        }
    }