【JSP】如何在jsp中打开pdf文件。


// java代码
public ActionForward getSoaPdf(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws IOException {

String path = request.getParameter("path");
if (IsPdf(path)) {
response.setContentType("application/pdf");
} else {
response.setContentType("application/vnd.ms-excel");
}
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
//加上下面这句则可以在浏览器外打开,或者保存
//resp.setHeader("Content-disposition", "attachment;filename=sample.pdf");

File pdf = null;
BufferedInputStream buf = null;
try {
pdf = new File("G:\\Soa_Pdf\\" + path);
response.setContentLength((int) pdf.length());
FileInputStream input = new FileInputStream(pdf);
buf = new BufferedInputStream(input);
int readBytes = 0;
while ((readBytes = buf.read()) != -1) {
out.write(readBytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) out.close();
if (buf != null) buf.close();
}
return null;
}
// js代码
function openDetailPdf(path, cnTitle, enTitle) {
art.dialog.open('operatorquery.do?method=getSoaPdf&path=' + path, {
title: cnTitle + " " + enTitle,
width: 1100,
// height: 450,
height: 580,
left: '133px',
top: '10px',
fixed: true,
resize: false,
padding: '5px 5px',
});
}

显示效果:

 

posted @ 2017-12-27 15:59  Legolas_4  阅读(1517)  评论(0编辑  收藏  举报