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


     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 }
  • 相关阅读:
    Combobox的使用
    章节十、7-Xpath---Xpath中绝对路径相对路径的区别
    章节十、6-CSS---用CSS 定位子节点
    章节十、5-CSS---用CSS 通配符定位元素
    章节十、4-CSS Classes---用多个CSS Classes定位元素
    章节十、3-CSS Selector---用CSS Selector
    章节十、2-用Linktext和PartialLinkText、ClassName、TagName定位元素
    章节十、1-用ID和XPath、name定位元素
    章节九、5-IE Driver
    章节九、4-ChromDriver介绍
  • 原文地址:https://www.cnblogs.com/dsh2018/p/9673839.html
Copyright © 2020-2023  润新知