• SpringBoot之文件下载


     1 package org.springboot.controller;
     2 
     3 import org.springboot.constant.Constant;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import java.io.*;
    10 import java.net.URLEncoder;
    11 
    12 /**
    13  * @Auther:GongXingRui
    14  * @Date:2018/12/24
    15  * @Description:
    16  **/
    17 @Controller
    18 public class FileDownload {
    19 
    20         //实现Spring Boot 的文件下载功能,映射网址为/download
    21         @RequestMapping("/download")
    22         public String downloadFile(HttpServletRequest request,
    23                                    HttpServletResponse response) throws UnsupportedEncodingException {
    24 //            String picPath = "C:/Temp/pic";
    25             String picPath = Constant.CONFIG_PROPERTIES.getProperty("download.path");
    26 
    27             // 获取指定目录下的第一个文件
    28             File scFileDir = new File(picPath);
    29             File TrxFiles[] = scFileDir.listFiles();
    30             System.out.println(TrxFiles[0]);
    31             String fileName = TrxFiles[0].getName(); //下载的文件名
    32 
    33             // 如果文件名不为空,则进行下载
    34             if (fileName != null) {
    35                 //设置文件路径
    36                 File file = new File(picPath, fileName);
    37 
    38                 // 如果文件名存在,则进行下载
    39                 if (file.exists()) {
    40                     // 配置文件下载
    41                     response.setHeader("content-type", "application/octet-stream");
    42                     response.setContentType("application/octet-stream");
    43                     // 下载文件能正常显示中文
    44                     response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    45 
    46                     // 实现文件下载
    47                     byte[] buffer = new byte[1024];
    48                     FileInputStream fis = null;
    49                     BufferedInputStream bis = null;
    50                     try {
    51                         fis = new FileInputStream(file);
    52                         bis = new BufferedInputStream(fis);
    53                         OutputStream os = response.getOutputStream();
    54                         int i = bis.read(buffer);
    55                         while (i != -1) {
    56                             os.write(buffer, 0, i);
    57                             i = bis.read(buffer);
    58                         }
    59                         System.out.println("Download the file successfully!");
    60                     }
    61                     catch (Exception e) {
    62                         System.out.println("Download the file failed!");
    63                     }
    64                     finally {
    65                         if (bis != null) {
    66                             try {
    67                                 bis.close();
    68                             } catch (IOException e) {
    69                                 e.printStackTrace();
    70                             }
    71                         }
    72                         if (fis != null) {
    73                             try {
    74                                 fis.close();
    75                             } catch (IOException e) {
    76                                 e.printStackTrace();
    77                             }
    78                         }
    79                     }
    80                 }
    81             }
    82             return null;
    83         }
    84 
    85 
    86 }
  • 相关阅读:
    如何创建html新元素
    jqury如何一次性实现连贯的一系列不同动作?
    jquery论callback事件发生与并列事件发生的区别
    jquery论三种动画停止的区别
    jquery animate多个属性设置为toggle的叠加效果
    如何一个键实现控制一个元素的隐藏和显示?
    jquery中mouseenter,mouseleave与hover的区别用法
    Zabbix探索:工作时间的设置
    Zabbix探索:网络设备监控3
    Zabbix探索:Proxy没有回传任何数据
  • 原文地址:https://www.cnblogs.com/gongxr/p/10189256.html
Copyright © 2020-2023  润新知