[英]How to send PDF file to client using servlet.?
I'm trying a small code to send PDF file to client using servlet. 我正在尝试一个小的代码,以使用servlet将PDF文件发送到客户端。 I've developed one code which is attahed below.
我已经开发了一个附在下面的代码。 When I run this code in NetBeans on glassfish server, one pop download window appears and I click on open option It's says "There is problem with format" and I'm not able to see content from file.
当我在glassfish服务器上的NetBeans中运行此代码时,将出现一个弹出下载窗口,然后单击打开选项。它说“格式存在问题”,而我看不到文件中的内容。 So can anyone help me to solve this logical error ?
那么有人可以帮助我解决这个逻辑错误吗?
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";
}
}
标头使用以下内容
response.setHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
You just needed to make a few changes to the way you're writing out the PDF. 您只需要对编写PDF的方式进行一些更改。
I got the code here to return a PDF that rendered correctly Chrome. 我在这里获得了返回正确呈现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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.