一:在jsp上显示pdf
<%@ page language="java" import="java.util.*,java.io.*"pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><%=basePath%>"><% out.clear(); out = pageContext.pushBody(); response.setContentType("application/pdf"); try { String strPdfPath = new String("D://200701010001.PDF"); //判断该路径下的文件是否存在 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()); }%>
二:如果访问者的计算机上没有装acrobat reader,直接用iframe或者直接用链接打开,那就不是打开文档,而是直接下载了,为了防止下载,想像到媒体文件的播放方式,加上一些官方的docs,考虑用用object标签使用pdf插件嵌入ie中,结果可行。 代码如下:
Checking if Acrobat Reader installed (IE4+)...
三:在网页中直接显示pdf格式的文件方便阅读。但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了,不过还是贴出来了,各取所需吗。(1.pdf要改成自己的pdf文件路径插入到页面文件中)
Html代码:
标准浏览器中:
Java代码 :alt : test.pdf alt : test.pdf
IE7.0以上版本的浏览器中可用如下方法,低版本的IE会显示两个Object区域 :
Java代码 :
alt : test.pdf alt : test.pdf
低版本浏览器中的处理方法1:
Java代码 :
alt : Adobe Reader.pdf alt : Adobe Reader.pdf
低版本浏览器中的处理方法2:通过CSS控制显示隐藏 :
Html代码
/* hides the second object from all versions of IE */ * html object.hiddenObjectForIE { display: none; } /* display the second object only for IE5 Mac */ /* IE Mac \*//*/ * html object.hiddenObjectForIE { display: inline; } /**/ /* hides the second object from all versions of IE */ * html object.hiddenObjectForIE { display: none; } /* display the second object only for IE5 Mac */ /* IE Mac \*//*/ * html object.hiddenObjectForIE { display: inline; } /**/alt : test.pdf
Html代码