方式一:(仅适用于IE且计算机安装有pdf阅读器)
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="100%" height="100%" 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="fileUrl>">
</object>
方式二:(仅适用于IE且计算机安装有pdf阅读器)
<object data="fileUrl" type="application/pdf" width="100%" height="100%">
</object>
方法三:以文件流的形式直接返回页面(推荐使用,需要计算机安装有pdf阅读器)
java代码:
@RequestMapping(value = "/a/oa/attachment/fileDetail")
public String fileDetail(HttpServletRequest request,HttpServletResponse response,@RequestParam("fileId") String fileId) throws Exception{
OaAttachment oaAttachment = oaAttachmentService.queryOneById(fileId);
String fileName = oaAttachment.getFileName();//文件名含格式
String name = fileName.substring(0, fileName.lastIndexOf("."));//文件名
String path = oaAttachment.getFilePath();
String realPath = request.getServletContext().getRealPath("");
String pdfPath = realPath+"static/fileTemp/"+path;
String fileUrl = pdfPath+name+".pdf";
File file = new File(fileUrl);
FileInputStream is = new FileInputStream(file);
response.setContentType("application/pdf;charset=UTF-8");
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int read;
while((read=is.read(b))!=-1){
out.write(b,0,read);
}
is.close();
out.flush();
out.close();
//request.setAttribute("fileUrl", fileUrl);
return "a/oa/attachment/fileDetail.jsp";
}
jsp:代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.clear();
out = pageContext.pushBody();
%>
</body>
</html>
方式四:(不推荐兼容性太差)
使用PDFObject.js 插件;
方式五:(推荐使用)
使用pdf.js插件
项目中引入pdf.js插件插件后
要显示的jsp页面:
注:static为存放pdf.js插件的文件夹,一般放在webapp下,web-inf上级;
generic为pdf.js的插件文件夹;
viewer.html为pdf.js插件文件下的视图文件,当?file参数为空或不传时会跳到pdf.js的默认页面;
要使pdf高度变为100%,需要去掉<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
标准中的“Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd”即可。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
window.onload = function (){
var iframe = document.getElementById("showIframe");
//文件url
var fileUrl = '<%=request.getAttribute("fileUrl")%>';
//jsp页面提取文件时的路径 转码方式 encodeURIComponent(filepath)或 encodeURI(encodeURI(filepath))
var filepath = "../../../"+fileUrl;
iframe.src = "/static/generic/web/viewer.html?file="+encodeURIComponent(filepath);
};
</script>
</head>
<body>
<iframe id="showIframe" src="" width="100%" height="100%"></iframe>
</body>
</html>