码迷,mamicode.com
首页 > Web开发 > 详细

根据第三方提供的图片或pdf的url进行预览

时间:2018-09-19 12:23:10      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:重要   component   stream   col   pki   puts   new   ons   project   

 1 package com.hailian.modules.credit.common.controller;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.io.IOException;
 7 import java.io.OutputStream;
 8 import java.io.UnsupportedEncodingException;
 9 import java.net.URL;
10 
11 import javax.servlet.http.HttpServletResponse;
12 
13 import com.hailian.component.base.BaseProjectController;
14 import com.hailian.jfinal.component.annotation.ControllerBind;
15 import com.hailian.modules.credit.utils.FileTypeUtils;
16 import com.jfinal.kit.PropKit;
17 @ControllerBind(controllerKey = "/credit/commonFile")
18 public class FileViewAndUpController extends BaseProjectController{
19     public void previewFileOnline() {
20         HttpServletResponse response = getResponse();
21         BufferedInputStream bis = null;
22         OutputStream os = null;
23         try {
24             String path = getPara("url");//url地址
25             path=new String(getPara("url").getBytes("iso8859-1"),"utf-8");
26             String type = getPara("type");//获取类型
27             response.reset(); // 非常重要
28             response.setContentType("text/html; charset=UTF-8");
29                 if(!FileTypeUtils.isImg(type)){
30                     response.setContentType("application/pdf");
31                 }else{
32                     response.setContentType("image/jpeg");
33                 }
34                 URL url =new URL(path);
35                     bis = new BufferedInputStream(url.openStream());
36                 os = response.getOutputStream();
37                 int count = 0;
38                 byte[] buffer = new byte[1024 * 1024];
39                 while ((count =bis.read(buffer)) != -1){
40                     os.write(buffer, 0,count);
41                 }
42                 os.flush();
43         }catch (Exception e) {
44             e.printStackTrace();
45             if (os !=null){
46                 try {
47             os.close();
48         } catch (IOException e2) {
49             e2.printStackTrace();
50         }
51             }
52             if (bis !=null){
53                 try {
54             bis.close();
55         } catch (IOException e2) {
56             e2.printStackTrace();
57         }
58             }
59         } finally {
60                 if (os !=null){
61                     try {
62                 os.close();
63             } catch (IOException e) {
64                 e.printStackTrace();
65             }
66                 }
67                 if (bis !=null){
68                     try {
69                 bis.close();
70             } catch (IOException e) {
71                 e.printStackTrace();
72             }
73                 }
74             }
75     }
76 }

 

根据第三方提供的图片或pdf的url进行预览

标签:重要   component   stream   col   pki   puts   new   ons   project   

原文地址:https://www.cnblogs.com/dsh2018/p/9673839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!