关于使用java或者前段jsp打开本地PDF文件

第一种比较简单的方法,但是该方法IE无法打开

function  _doOption(value,row,index){
	var peruuid=row.GOODS_REG_UUID;//有值说明已经审核
	return "<a href='javascript:void(0);' onclick='_ApproveDig(\""+peruuid+"\")' class='easyui-linkbutton' data-options='iconCls:\"icon-remove\",plain:true'>文书查看</a>";
}


function _ApproveDig(peruuid){
	var type='2';
	//window.location.href="checkEvidence_OpenPdf.do?uuid="+peruuid+'&type='+type;
	var url="checkEvidence_OpenPdf.do?uuid="+peruuid+'&type='+type;
	window.open (url);
}


public String OpenPdf() throws Exception{
		String uuid=request.getParameter("uuid");
		String doc=checkEvidenceService.queryURLByid(uuid);
		String pf ="C:"; 
		String filePath=pf+doc;
		request.setAttribute("filePath", filePath);
		return "pdfview";
	//	String url1="C:/home/monopy/document/1e3edcd0-6aef-4a84-9218-9cd730394606/阿里巴巴Java开发手册.pdf";  测试用
		response.setContentType("application/pdf");
		FileInputStream in = new FileInputStream(new File(filePath));
		OutputStream out = response.getOutputStream();
        byte[] b = new byte[512];
        while ((in.read(b)) != -1) {
            out.write(b);
        }
        out.flush();
        in.close();
        out.close();
	}

2.第二种方法,稍微有点复杂,这个方法IE是可以打开PDF文件的

function  _doOption(value,row,index){
	var peruuid=row.GOODS_REG_UUID;//有值说明已经审核
	return "<a href='javascript:void(0);' onclick='_ApproveDig(\""+peruuid+"\")' class='easyui-linkbutton' data-options='iconCls:\"icon-remove\",plain:true'>文书查看</a>";
}
AJAX的请求只是去后台拿完整的url,无其他意图,然后使用window.location.href去访问jsp

function _ApproveDig(peruuid){
	$.ajax({
		cache: true,
        type: "POST",
        url:context+'/check/checkEvidence_OpenPdf.do',
        data:{uuid:peruuid},
        async: false,
        dataType: 'json',
		async : true,
		success : function(data){
			var url=data.url;
		    var url1 =encodeURI(encodeURI(context+'/include/ntko/pdfView.jsp?filePath='+url,"utf-8"),"utf-8");
			//var url1=context+'/include/ntko/pdfView.jsp?filePath='+url;
			window.location.href=url1;
		}
	});
}

这是预览页面,需要插件 ntko,需要插件的可以私我,这个上传太麻烦  ntkooledocall.cab,OfficeControl.cab,需要对url进行再次转码,否则 会中文乱码

<%@ page language="java" import="java.util.*,java.io.*,java.net.*"  pageEncoding="UTF-8"%>
<%@page contentType="text/html;charset=utf-8" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);

	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<script type="text/javascript" src="jquery.media.js"></script>
	<head>
		<base href="<%=basePath%>" target="_self">
	</head>
	<script type="text/javascript">
	<%
		    	out.clear();
		    	out = pageContext.pushBody();
		    	response.setContentType("application/pdf");
		    	try {
			    //String filePath = "C:/home/monopy/document/1e3edcd0-6aef-4a84-9218-9cd730394606/阿里巴巴Java开发手册.pdf";
			    	//String filePath = request.getParameter("filePath");
			    	String filePath=URLDecoder.decode(request.getParameter("filePath"), "UTF-8");

			    	/*
			    	filePath = filePath.replace("/kjsb/", "");			// 
			    	filePath = request.getRealPath("/") + filePath;
			    	*/
			    	
			    	System.out.println("------filePath-------" + filePath);
			    	//判断该路径下的文件是否存在
					File file = new File(filePath);
			    	if (file.exists()) {
				    	 DataOutputStream temps = new DataOutputStream(response.getOutputStream());
				    	 DataInputStream in = new DataInputStream(
				    	 new FileInputStream(filePath));
				    	 byte[] b = new byte[2048];
				    	 while ((in.read(b)) != -1) {
						    temps.write(b);
						    temps.flush();
					     }
			    		in.close();
			    		temps.close();
				    } else {
				    	System.out.println("文件不存在!");
				    }
		    	} catch (Exception e) {
		    		out.println(e.getMessage());
		    	}
	    	%> 
	</script>
	<body>
		<br>
	</body>
</html>

 

转载于:https://my.oschina.net/u/3053442/blog/873569

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值