您的位置:首页 > Web前端 > JavaScript

pdf文档的下载与查看 jsp页面

2015-09-23 13:46 344 查看

文件下载

Controller层代码

@RequestMapping("/download")
public static ResponseEntity(Byte[]) downloadPdf(HttpServletRequest request,HttpServletResponse response) throws Exception{
String str = request.getParameter("str“)//获取下载请求传来的参数
.......................................................
String filePath=PropertyUtil.getProperty("path")//定义一个配置文件property,对应写一个propertyUti类来获取配置中的文件路径

//发送消息头,头文件
response.setCharacterEncoding("utf-8");
response.setContentType("application/pdf");
response.setHeader(("Content-Disposition",

"attachment;fileName=fapiao.pdf");/************这一块都是文件消息头格式设置*/

InputStream input = null;//字节流读取文件对象
OutputStream os = null;//字节流输出文件对象
try{
input = new InputStream(new File(filePath));//将文件以字节流的方式读取出来
os=response.getOutputStream();//响应输出流对象

byte[] b = new byte[2048]
int length;
while(length=input.read(b)>0){
os.write(b,0,length);//循环读取,b大小,length长度的字节文件;从0开始读取,知道文件读取完毕

}
os.flush();//这个一定要加上

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(os!=null){
os.close();
os=null;
}
if(inputStream!=null){
inputStream.close();
inputStream=null;
}
}
return null;
}

jsp页面pdf 文档显示

Controller层代码 前端可以使用一个查看按钮绑定一个viewUrl的请求,通过这个查看组件来显示文档内容在JSP页面

@RequestMapping("viewUrl")

public static ResponseEntity(byte[]) downloadPdf(HttpServletRequest request,HttpServletResponse response) throws Exception{
String str = request.getParameter("str“)//获取下载请求传来的参数
.......................................................
String filePath=PropertyUtil.getProperty("path")//定义一个配置文件property,对应写一个propertyUti类来获取配置中的文件路径
/********这里一般会通过path以及参数信息然后调用service层从数据库中查询到对应的文件路径filePath*****************/

//发送消息头,头文件
response.setContentType("application/pdf");/***********这个关键点*/

InputStream input = null;//字节流读取文件对象
OutputStream os = null;//字节流输出文件对象
try{
input = new InputStream(new File(filePath));//将文件以字节流的方式读取出来
os=response.getOutputStream();//响应输出流对象,发回jsp页面

byte[] b = new byte[2048]
int length;
while(length=input.read(b)>0){
os.write(b,0,length);//循环读取,b大小,length长度的字节文件;从0开始读取,知道文件读取完毕

}
os.flush();//这个一定要加上

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(os!=null){
os.close();
os=null;
}
if(inputStream!=null){
inputStream.close();
inputStream=null;
}
}
return null;
}

jsp文件查看pdf文档方法2通过html标签的方式三种标签:

注意 IE浏览器 object标签 data ,src 的路径动态传输时 引号问题

以下是一个jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE jsp PUBLIC "-//W3C//DTD html 4.01 Transitional//EN" "http://www.w3.org/TR/jsp4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<jsp:include page="${base}/jsp/import.jsp" flush="true" />//导入的公共js文件部分

<title>沪友电子发票综合服务平台</title>

</head>

<body>

<jsp:include page="${base}/jsp/top.jsp" flush="true" />//公共header部分

<section

style="text-align:center; border: 1px solid green;width:1000;height:800;margin: 5px auto;font-size: 13px;display: block;">

<h1>发票样票</h1>

<object

classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="990" data=

'<%=request.getContextPath()%>/<%=session.getAttribute("downloadUrl")%>'>//data属性也可以,和src属性一样

height="700" border="0" top="-10" name="pdf">

<param name="src" value='<%=request.getContextPath()%>/<%=session.getAttribute("downloadUrl")%>'>//后台通过session绑定文件URL

<embed id="embed_PDF" src ='<%=request.getContextPath()%>/<%=session.getAttribute("downloadUrl")%>' width="990"

height="700">

</object>

<iframe id="myIframe" src="test.pdf " frameBorder=0 marginwidth=0 marginheight=0 scrolling=no width="100%" onload="iframeFitHeight(this)"> </iframe>

</section>

<footer> <jsp:include page="${base}/jsp/foot.jsp" flush="true" />//公共页脚部分,注意导入公共部分绝对路径的写法

</footer>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: