web项目移动端在线预览(pdf在线预览)

pdf格式的附件在pc端是可以直接打开的,但是在移动端,受浏览器的限制有些可能无法打开,这里采用项目内嵌pdf阅读器的方式实现pdf在移动端的在线预览
1.首先需要在项目中引入pdf阅读器,是一个文件夹,本项目放在了page目录下,这个可以去本账号的资源下载,纯免费
2.写一个页面(pdf.jsp),实现调用pdf阅读器
<%@page import="com.daorigin.law.Global"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="ifn" uri="/WEB-INF/tld/JSTLFunction.tld"%>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>标题</title>
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta charset="UTF-8">
    <meta name="format-detection" content="telephone=no" />
</head>
  <% 
	String scheme=request.getScheme(); 
	String server=request.getServerName() + ":" + request.getServerPort(); 
	String staticServer = scheme + "://" + server;
	String fileId = request.getParameter("fileId");	
	%> 
    <link href="<%=staticServer %>/static/css/reset.css" rel="stylesheet" type="text/css" >
	<link href="<%=staticServer %>/static/css/to_details.css" rel="stylesheet" type="text/css" >
<body>
    <iframe style="margin-top: 30px;" id="myiframe" width="100%" height="500px" onload="changeFrameHeight()" ></iframe>
    
    
    
    <script src="/static/js/ydmain.js"></script>
    <script src="/static/template/jQuery/jQuery-2.1.4.min.js"></script>
	<script src="/static/js/main.js"></script>
	
	<script type="text/javascript">
		$(document).ready(function(){
		//这里是pdf的路径,本项目需要对pdf进行处理,这里是后台的接口路径
			var encodeUrl = encodeURIComponent("<%=staticServer %>/law/common/file/downloadOrDetail.htm?objectId=<%=fileId%>");
			//调用阅读器在线阅读pdf
			var urlSrc="<%=staticServer %>/page/pdf/web/viewer.html?file="+encodeUrl;
			$("#myiframe").attr("src",urlSrc);
		});
		
		function changeFrameHeight(){
		    var ifm= document.getElementById("myiframe"); 
		    ifm.height=document.documentElement.clientHeight;
		}
		</script> 
</body>

</html>
3.后台处理
    //获取文件服务器连接
    URL url = new URL(Global.imgServer + "?file=" + fileVO.getFilePath() + "&name="
       + URLEncoder.encode(fileVO.getFileName(), "utf-8"));
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     DataInputStream input = new DataInputStream(conn.getInputStream());
     String filename = fileVO.getFileName();
     Boolean flag = request.getHeader("User-Agent").indexOf("like Gecko") > 0;
     if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") > 0 || flag) {
      filename = URLEncoder.encode(filename, "UTF-8");// IE浏览器
     } else {
      // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,
      // 这个文件名称用于浏览器的下载框中自动显示的文件名
      filename = new String(filename.replaceAll(" ", "").getBytes("UTF-8"), "ISO8859-1");
     }
     filename=filename.replaceAll("\\+","%20");
     byte[] buffer = new byte[1024];
     BufferedInputStream bis = null;
     //将pdf文件下载到本地
     File file=new  File(LawConfig.officeHtml+fileVO.getFileName());
     FileOutputStream out=new FileOutputStream(file,true);
     try {
      bis = new BufferedInputStream(input);
      int i = bis.read(buffer);
      while (i != -1) {
       out.write(buffer, 0, i);
       i = bis.read(buffer);
      }
     } catch (Exception e) {
      e.printStackTrace();
     } finally {
      if (bis != null) {
       try {
        bis.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
      if (input != null) {
       try {
        input.close();
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
      out.close();
     }
    //判断pdf是否需要解密处理
     System.setProperty("user.dir", "解密接口地址");
     InteKey mInteKey = new InteKey();
     System.out.println("开始验证,文件是:"+LawConfig.officeHtml + fileVO.getFileName());
     int ia = mInteKey.Ia(LawConfig.officeHtml + fileVO.getFileName());
     System.out.println("验证结果:" + ia);
     if (ia == 0) {     
      // 加密文件,需要做解密处理
      System.out.println("开始解密");
      int da = mInteKey.Da(LawConfig.officeHtml + fileVO.getFileName(), LawConfig.officeHtml + fileVO.getFileName());
      System.out.println("解密结果:" + da);
     }
     
     FileInputStream fis = null;
     OutputStream os = null;
     try {
      fis = new FileInputStream(LawConfig.officeHtml + fileVO.getFileName());
      os = response.getOutputStream();
      int count = 0;
      while ((count = fis.read(buffer)) != -1) {
       os.write(buffer, 0, count);
       os.flush();
      }
     } catch (Exception e) {
      e.printStackTrace();
     }
     try {
      fis.close();
      os.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
   

你可能感兴趣的:(java操作office,java,ios)