關於 使用 java 或者 前段 jsp 打開 本地 pdf 文件 欄目 Java 简体版
原文   原文鏈接

第一種比較簡單的方法,可是該方法IE沒法打開javascript

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文件的html

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進行再次轉碼,不然 會中文亂碼java

<%@ 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>
相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息