1. 程式人生 > >根據第三方提供的圖片或pdf的url進行預覽

根據第三方提供的圖片或pdf的url進行預覽

nts span ext puts html online utils text charset

 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進行預覽