前端
获取数据
<%
List<TRulePicInfo> picList=(List<TRulePicInfo>)request.getAttribute("picList");
<%for(int i=0;i<picList.size();i++){
TRulePicInfo rulePicInfo=picList.get(i);
long id = rulePicInfo.getId();
String filePath=rulePicInfo.getPicPath();
String format = filePath.substring(filePath.lastIndexOf(".")+1);
String path = filePath.replaceAll("\\\\", "/");
request.setAttribute("format",format);
String titleName = rulePicInfo.getPicName();
%>
<tr>
<!-- <td><a target="_blank" href="ruleInfoAction@browseFile.ces?idArray=<%=id %>"><%=titleName %></a><br/></td> -->
<td>
<a target="_blank" href="downloadAction@download.ces?affixPath=<%=filePath%>"><%=titleName %></a>
<ac:if test="${format == 'pdf' || format == 'doc' ||format == 'docx' }">
<a onclick="isAcrobatPluginInstall('<%=path%>')">点击预览</a>
</ac:if>
<br/>
</td>
</tr>
<%}%>
<script type="text/javascript">
function forward(url){
window.location.href = url;
}
function quit(){
if(confirm("确定要退出系统吗?")) {
window.location.href="loginOutAction.ces";
}
}
//判断是否是 IE 浏览器
//判断本电脑是否安装了 adobe PDF阅读器插件
function isAcrobatPluginInstall(path){
//如果是firefox浏览器
if (navigator.plugins && navigator.plugins.length) {
for (x=0; x<navigator.plugins.length;x++) {
if (navigator.plugins[x].name== 'Adobe Acrobat'){
join(path);
return true;
}
}
}else if (window.ActiveXObject){//下面代码都是处理IE浏览器的情况
for (x=2; x<10; x++){
try{
oAcro=eval_r("new ActiveXObject('PDF.PdfCtrl."+x+"');");
if (oAcro){
join(path);
return true;
}
}catch(e) {}
}
try{
oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
if (oAcro4){
join(path);
return true;
}
}catch(e) {}
try{
oAcro7=new ActiveXObject('AcroPDF.PDF.1');
if (oAcro7){
join(path);
return true;
}
}catch(e) {}
}
alert("检测到该电脑未安装 Adobe阅读PDF插件,无法在线查看,请安装后重试");
return;
}
//追加超链接元素,并触发
function join(filePath){
if(document.getElementById("H2") != null){
document.body.removeChild(document.getElementById("H2"))
return;
}
document.body.appendChild(document.createElement("br"));
var a = document.createElement("a");
a.id="H2";
a.href="ruleInfoAction@pdfNowShow.ces?path="+filePath;
a.innerText="预览PDF";
document.body.appendChild(a)
var h2 = document.getElementById("H2");
document.getElementById("H2").style.display="none";
h2.onclick=function(){
}
h2.click();
}
</script>
后台
/**
* 结合adobe IE 插件,支持在线打开PDF,兼容所有IE
* @author Cc
*/
public void pdfNowShow() throws IOException {
String filePath = Function.getParameter(request, "path");
File file = new File(filePath);
if (!file.exists()) {
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] bs = new byte[1024];
int len = 0;
response.reset(); //处理头部空白页信息
URL u = new URL("file:///" + filePath);
String contentType = u.openConnection().getContentType();
response.setContentType(contentType);
//inline 在线预览方式
response.setHeader("Content-Disposition", "inline;filename="+"");
OutputStream out = response.getOutputStream();
while ((len = br.read(bs)) > 0) {
out.write(bs, 0, len);
}
out.flush();
out.close();
br.close();
return;
}