我希望java代码在浏览器上查看pdf文件而不下载它们 [英] i want java code to view pdf file on browser without downloading them
本文介绍了我希望java代码在浏览器上查看pdf文件而不下载它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在搜索java代码,无需下载即可在浏览器上查看pdf文件。这些文件将通过数据库插入。欢迎使用帮助。
i am searching for java code to view pdf file on browser without downloading them. The files will be inserted through database.Help will be appreciated.
推荐答案
您需要使用servlet来执行此操作:
You need to use a servlet to do that:
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class viewPDF extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = null;
DB1 db = new DB1();
Connection conn=db.dbConnect(
"jdbc:jtds:sqlserver://localhost:1433/smpp","sa","");
try {
response.setContentType("application/pdf");
out = response.getOutputStream();
byte[] b = db.getPDFData(conn);
out.write(b,0,b.length);
out.close();
} catch (Exception e) {
throw new ServletException(
"Exception in Excel Sample Servlet", e);
} finally {
if (out != null)
out.close();
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
在此处查看更多信息以及使用的完整工作示例来自浏览器中数据库的PDF: java-tips
See more info here and a fully working example which uses a PDF from a database in a browser: java-tips
这篇关于我希望java代码在浏览器上查看pdf文件而不下载它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文