使用pdfobject预览pdf文件

1.页面代码,引入js文件,根据文件id加载pdf预览文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head th:include="commonBase">
<title>文件预览</title>     
</head>
<link rel="stylesheet" th:href="@{/common/css/fileIcon.css}"/>                        
<script type="text/javascript" th:src="@{/common/js/pdfobject.js}"></script>     
 <body>   
    <div style="display:none">     
        <input class="mini-hidden" name="id" id="id" th:value="${id}"/>                              
    </div>                                                                                  
    <div id="pdf"></div>                                             
 </body>

 <script  type="text/javascript">         
 /*<![CDATA[*/  

	$(function(){    
	    mini.parse();
	    var id = mini.get("id").getValue();    
		var myPDF = new PDFObject({url:basePath+'scoreFile/previewFileById?id='+id}).embed("pdf");   
	})     
  
    /*]]>*/        
 </script>            
</html>

2.后台根据文件id返回文件流展示pdf文件

(1)controller层,根据文件id查询文件存在在minio中的位置信息

@RequestMapping(value = { "/previewFileById" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public void previewFileById(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setCharacterEncoding("UTF-8");
			String id = request.getParameter("id");
			ScoreFile scoreFile = scoreFileService.findById(id);
			String fileDirectory = scoreFile.getFileDirectory();
			String fileNameUuidPdf = scoreFile.getFileNewName();
			String fileName = scoreFile.getFileName();
			String fileNamePdf = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; // pdf文件名
			String path = scoreFile.getFilePath() + fileNameUuidPdf;
			FileUtils.previewfileById(fileDirectory, path, fileNamePdf, response); // 跟据存放在minio中目录得到转化好的PDF文件流
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

(2)根据存放位置信息加载文件,返回文件流,写在fileutils工具类中

		 public static void previewfileById(String fileDirectory, String path, String fileNamePdf,HttpServletResponse response) {
				InputStream in = null;
		        OutputStream out = null;
		        MinioClient minioClient = FileUtils.getInstance();
		        try
		        {
		          in = minioClient.getObject(fileDirectory, path);
		          int len = 0;
		          byte[] buffer = new byte[1024];
		          out = response.getOutputStream();
		          response.reset();   
		          String filename = getFileNameForISO8859(fileNamePdf);      //得到iso-8859-1编码的文件名
		          response.addHeader("Content-Disposition","inline;filename="+filename);
		          response.setContentType("application/pdf;charset=UTF-8");
		          while ((len = in.read(buffer)) > 0) {
		            out.write(buffer, 0, len);
		          } 
		        } catch (Exception e){
		        	e.printStackTrace();
		        } finally {
		          if (in != null) {
		            try
		            {
		              in.close();
		            }
		            catch (Exception e)
		            {
		              throw new RuntimeException(e);
		            }
		          }
		          if (out != null) {
		            try {
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
		          }
		        }
				
			}
			
			/**
			 * @Description 得到iso-8859-1编码的文件名
			 * @author [zhanglizeng] 
			 * @throws UnsupportedEncodingException 
			 * @createDate 2018年9月28日 下午6:46:41
			 */
			private static String getFileNameForISO8859(String fileNamePdf) throws UnsupportedEncodingException {
				Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");  //匹配中文
				if (fileNamePdf != null) {
				    Matcher aMatcher = pattern.matcher(fileNamePdf);
				    if(!aMatcher.find()) {
				    	fileNamePdf = "下载-"+fileNamePdf;
				    }
				    return new String(fileNamePdf.getBytes(),"iso-8859-1");
				}
				return "文件名为空";
			}

        //创建minio的链接
        public static MinioClient getInstance() {
            if (minioClient == null) {  
            synchronized (FileUtils.class) {  
                if (minioClient == null) {  
                	try {
                		//new FileUtils(); 
                		minioClient = new             MinioClient(minioUrl,minioUsername,minioPassword);
					} catch (Exception e) {
						e.printStackTrace();
					}
                }  
               }  
            }  
            return minioClient;  
        }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值