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部分
<sectionstyle="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>
相关文章推荐
- js url参数的获取和设置以及删除
- JS显示时间
- javascript调用EXE程序传值
- 《Effective JavaScript》- 1.2 理解JavaScript的浮点数
- FastJSON 之bean列表转换为JSON
- javascript 获取滚动条高度+常用js页面宽度与高度
- XML转JSON
- extjs4.0 Ext.Array 函数方法大全
- js中的href和onclick区别
- JavaScript 操作 Cookie
- js获取鼠标位置
- Javascript实现的简单右键菜单类
- jmap,jstack,jhat内存分析
- 关于JSP 页面中用绝对路径显示图片
- JavaScript字符串数组排序
- 在TypeScript中使用其他JS框架或库的方法
- JavaScript学习笔记之JS基础入门
- js中可以改变作用域的三中方式(改变this)
- 安卓新闻客户端(二) JSOUP解析HTML 抓取网页内容
- js获取日期:前天、昨天、今天、明天、后天