zoukankan      html  css  js  c++  java
  • (Java)网路路径文件下载

    此篇时讲通过网路路径将文件以二进制流响应到浏览器,文件名称是前台所管控的。



    下面是下载文件方法的主体:

     1 import org.slf4j.Logger;
     2 import org.slf4j.LoggerFactory;
     3 import org.springframework.stereotype.Component;
     4 
     5 import javax.servlet.ServletOutputStream;
     6 import javax.servlet.http.HttpServletResponse;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 import java.net.HttpURLConnection;
    10 import java.net.URL;
    11 
    12 /**
    13  * 文件下载
    14  * Date: 2020/10/29
    15  * @author 时恰好那个i
    16  */
    17 @Component
    18 public class FileDownloadUtil {
    19 
    20     private static final Logger LOGGER = LoggerFactory.getLogger(FileDownloadUtil.class);
    21 
    22 
    23     /**
    24      * 从网络Url中下载文件
    25      * @param fileUrl   网路路径
    26      * @param fileName  文件名称
    27      */
    28     public void downLoadFromUrl(String fileUrl,String fileName, HttpServletResponse response) {
    29         ServletOutputStream outputStream = null;
    30         InputStream inputStream = null;
    31         try {
    32             URL url = new URL(fileUrl);
    33             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    34             //设置超时间为3秒
    35             conn.setConnectTimeout(3 * 1000);
    36             //防止屏蔽程序抓取而返回403错误
    37             conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
    38 
    39             //得到输入流
    40             inputStream = conn.getInputStream();
    41             response.setContentType("application/json;charset=UTF-8");
    42             // response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8") );
    43             response.setHeader("Cache-Control", "no-cache");
    44             outputStream = response.getOutputStream();
    45             byte[] b = new byte[1024];
    46             int len = 0;
    47             while((len = inputStream.read(b)) > 0) {
    48                 outputStream.write(b, 0, len);
    49             }
    50             outputStream.flush();
    51         } catch (IOException e) {
    52             LOGGER.info("文件下载失败");
    53             e.printStackTrace();
    54         } finally {
    55             if (outputStream != null) {
    56                 try {
    57                     outputStream.close();
    58                 } catch (IOException e) {
    59                     e.printStackTrace();
    60                 }
    61             }
    62             if (inputStream != null) {
    63                 try {
    64                     inputStream.close();
    65                 } catch (IOException e) {
    66                     e.printStackTrace();
    67                 }
    68             }
    69         }
    70     }
    71 
    72 }
  • 相关阅读:
    远程下载文件并设置进度显示
    python调用函数超时设置
    Ubuntu安装PostgreSQL
    sessionStatMap is full
    LdapTemplate忽略ssl证书
    MySQL5.6 Online DDL
    Mysql5.7编译调试(windows环境)
    Disruptor
    mybatis generator自定义文件后缀名
    maven占位符$变量无法替换
  • 原文地址:https://www.cnblogs.com/gdwm-tyf/p/14736687.html
Copyright © 2011-2022 走看看