先将自己的代码贴上来!
<html>
<head>
<title>My JSP 'pdfTest.jsp' starting page</title>
<script type="text/javascript">
window.οnlοad=function(){
document.all[document.getElementById("PDFNotKnown") ? "IfNoAcrobat" : "showdiv"].style.display = "block";
//当能显示pdf的时候,设置工具栏无用
if(document.getElementById("showdiv").style.display=="block"){
pdf.SetShowToolbar(false);
//document.getElementById("pdf").EnableCopy=false;
//document.getElementById("pdf").EnablePrint=false;
}
}
/* // 禁用右键功能
function stop(){
return false;
}
document.οncοntextmenu=stop; */
//禁止F8按钮
function keypressed() {
if(event.keyCode == 119) {
event.keyCode = 0;
return false;
}
}
</script>
</head>
<body>
<div class="right" id="abc" style="margin-top:-20px;">
<div class="cont-boxwrap2" id="c1">
<div class="con2" id="c2">
<h2 class="title"><span>意外险查询</span></h2>
<div class="boxwrap2" style="width:660px;overflow:auto;" id="cb2">
<div class="text-tabel1" style="overflow:auto;">
<fieldset class="auto_height" style="border:1px solid #ccc; background-color:#FFFFFF;">
<legend class="theme-color f16">条款预览</legend>
<div style="overflow:hidden;height:660px;">
<DIV id="IfNoAcrobat" >
<a href="http://get.adobe.com/cn/reader/">你需要先安装Adobe Reader才能正常浏览文件,请点击这里下载Adobe Reader.</a>
</DIV>
<OBJECT type="application/pdf" width=0 height=0 style="display:none">
<DIV id="PDFNotKnown" > </DIV>
</OBJECT>
<DIV id="showdiv" style="HEIGHT: 10px; margin-top: 10px;">
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="100%" height="660" border="0" top="-10" name="pdf" id="pdf" >
<param name="toolbar" value="false">
<param name="pagemode" value="none">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="../jpg.pdf">
<embed src="../jpg.pdf" width="100%" height="660">
</object>
</DIV>
</div>
</fieldset>
</div>
<div class="btn-area" style="margin-top:10px;width:200px; margin-right:30%!important;margin-right:17%">
<div class="btn-themecolor" ><a href="###" οnclick=" javascript:window.history.go(-1);">返回</a></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
这里要考虑到多浏览器兼容的问题,需要用到object和embed标签。
Object、param标签及页面显示PDF文件的方法:http://blog.youkuaiyun.com/tanguang_honesty/article/details/8530731
兼容火狐ie 在html中给.swf传递参数(转):http://blog.sina.com.cn/s/blog_6283c54a0100i2b0.html
在html上面显示pdf代码(禁用工具栏):http://blog.youkuaiyun.com/tanguang_honesty/article/details/8530500
html标签之Object标签详解:http://blog.youkuaiyun.com/soliy/article/details/5404183
另外一种方法是直接在jsp页面上输出:
代码如下。
out.clear();
out = pageContext.pushBody();
response.setContentType("application/pdf");
String strPdfPath = "D://jpg1.pdf";
try {
//判断该路径下的文件是否存在
File file = new File(strPdfPath);
if (file.exists()) {
DataOutputStream temps = new DataOutputStream(response
.getOutputStream());
DataInputStream in = new DataInputStream(
new FileInputStream(strPdfPath));
byte[] b = new byte[2048];
while ((in.read(b)) != -1) {
temps.write(b);
temps.flush();
}
in.close();
temps.close();
} else {
out.print(strPdfPath + " 文件不存在!");
}
} catch (Exception e) {
out.println(e.getMessage());
}