jsp直接打开本地的文件夹的pdf文件
发布网友
发布时间:2022-04-20 08:03
我来回答
共2个回答
热心网友
时间:2022-04-20 09:32
jsp中要利用java来实现打开,可以通过浏览器打开:
以下程序实现了读取某个路径下的pdf文件,并用浏览器打开:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends HttpServlet {
private static final long serialVersionUID = -3065671125866266804L;
public PDFServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
FileInputStream in = new FileInputStream(new File("d:/1.pdf"));
OutputStream out = response.getOutputStream();
byte[] b = new byte[512];
while ((in.read(b)) != -1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}
public void init() throws ServletException {
}
}
热心网友
时间:2022-04-20 10:50
直接用一个a标签 src的地址是pdf的文件路径 就行了嘛
或者用js window.open(“pdf路径”)