如何将文件pdf读出到jsp页面
1个回答
展开全部
在不需要使用插件,直接打开通过链接方式打开
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ page language="java" import="java.util.*,java.io.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<%
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());
}
%>
<body>
</body>
</html>
如果访问者的计算机上没有装acrobat reader,直接用iframe或者直接用链接打开,那就不是打开文档,而是直接下载了,为了防止下载,想像到媒体文件的播放方式,加上一些官方的docs,考虑用用object标签使用pdf插件嵌入ie中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<META http-equiv="Content-Style-Type" content="text/css">
<META http-equiv="Content-Script-Type" content="text/javascript">
<TITLE>Checking if Acrobat Reader installed (IE4+)...</TITLE>
<SCRIPT for="window" event="onload"
<!--
document.all [
document.all.PDFNotKnown ? "IfNoAcrobat" : "IfAcrobat"
] .style.display = "block";
//--></SCRIPT>
</HEAD>
<BODY>
<NOSCRIPT>
Cannot determine if you have Acrobat Reader (or the full Acrobat)
installed <FONT size="-1">(because JavaScript is unavailable or
turned off)</FONT>.
</NOSCRIPT>
<DIV id="IfNoAcrobat" style="display:none">
<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" style="display:none"> </DIV>
</OBJECT>
<DIV id=showdiv
style="Z-INDEX: 0; LEFT:10px; WIDTH: 990px; POSITION: absolute; TOP: -8px; HEIGHT: 10px">
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="990" height="700" border="0" top="-10" name="pdf">
<param name="toolbar" value="false">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="MXL.pdf">
</object>
</DIV>
</BODY>
</HTML>
在网页中直接显示pdf格式的文件方便阅读。但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了,不过还是贴出来了,各取所需吗。(1.pdf要改成自己的pdf文件路径插入到页面文件中)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Html代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
标准浏览器中:
Java代码
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
IE7.0以上版本的浏览器中可用如下方法,低版本的IE会显示两个Object区域
Java代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
低版本浏览器中的处理方法1:
Java代码
<!--[if IE]>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<![endif]-->
<!--[if !IE]> <!-->
<object data="1.pdf" type="application/pdf" width="800" height="1050">
alt : <a href='http://get.adobe.com/cn/reader'>Adobe Reader.pdf</a>
</object>
<!--<![endif]-->
<!--[if IE]>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<![endif]-->
<!--[if !IE]> <!-->
<object data="1.pdf" type="application/pdf" width="800" height="1050">
alt : <a href='http://get.adobe.com/cn/reader'>Adobe Reader.pdf</a>
</object>
<!--<![endif]-->
低版本浏览器中的处理方法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; }
/**/
Html代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200" class="hiddenObjectForIE">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ page language="java" import="java.util.*,java.io.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<%
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());
}
%>
<body>
</body>
</html>
如果访问者的计算机上没有装acrobat reader,直接用iframe或者直接用链接打开,那就不是打开文档,而是直接下载了,为了防止下载,想像到媒体文件的播放方式,加上一些官方的docs,考虑用用object标签使用pdf插件嵌入ie中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<META http-equiv="Content-Style-Type" content="text/css">
<META http-equiv="Content-Script-Type" content="text/javascript">
<TITLE>Checking if Acrobat Reader installed (IE4+)...</TITLE>
<SCRIPT for="window" event="onload"
<!--
document.all [
document.all.PDFNotKnown ? "IfNoAcrobat" : "IfAcrobat"
] .style.display = "block";
//--></SCRIPT>
</HEAD>
<BODY>
<NOSCRIPT>
Cannot determine if you have Acrobat Reader (or the full Acrobat)
installed <FONT size="-1">(because JavaScript is unavailable or
turned off)</FONT>.
</NOSCRIPT>
<DIV id="IfNoAcrobat" style="display:none">
<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" style="display:none"> </DIV>
</OBJECT>
<DIV id=showdiv
style="Z-INDEX: 0; LEFT:10px; WIDTH: 990px; POSITION: absolute; TOP: -8px; HEIGHT: 10px">
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="990" height="700" border="0" top="-10" name="pdf">
<param name="toolbar" value="false">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="MXL.pdf">
</object>
</DIV>
</BODY>
</HTML>
在网页中直接显示pdf格式的文件方便阅读。但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了,不过还是贴出来了,各取所需吗。(1.pdf要改成自己的pdf文件路径插入到页面文件中)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Html代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
标准浏览器中:
Java代码
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
IE7.0以上版本的浏览器中可用如下方法,低版本的IE会显示两个Object区域
Java代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
低版本浏览器中的处理方法1:
Java代码
<!--[if IE]>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<![endif]-->
<!--[if !IE]> <!-->
<object data="1.pdf" type="application/pdf" width="800" height="1050">
alt : <a href='http://get.adobe.com/cn/reader'>Adobe Reader.pdf</a>
</object>
<!--<![endif]-->
<!--[if IE]>
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
</object>
<![endif]-->
<!--[if !IE]> <!-->
<object data="1.pdf" type="application/pdf" width="800" height="1050">
alt : <a href='http://get.adobe.com/cn/reader'>Adobe Reader.pdf</a>
</object>
<!--<![endif]-->
低版本浏览器中的处理方法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; }
/**/
Html代码
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="800" height="1050" border="0">
<param name="_Version" value="65539">
<param name="_ExtentX" value="20108">
<param name="_ExtentY" value="10866">
<param name="_StockProps" value="0">
<param name="SRC" value="1.pdf">
<object data="1.pdf" type="application/pdf" width="300" height="200" class="hiddenObjectForIE">
alt : <a href="1.pdf">test.pdf</a>
</object>
</object>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |