• 用Springboot实现文件下载功能


     1 ApiOperation(value = "下载文件", httpMethod = "GET", notes = "downloadFile", response = String.class)
     2     public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
     3        String fileName = "zabbix.txt";// 设置文件名
     4             if (fileName != null) {
     5                 //设置文件路径
     6                // String realPath = "D://zabbix_agentd.conf";
     7                 String realPath = "D:" + File.separator + "zabbix_agentd.conf";
     8                 File file = new File(realPath , fileName);
     9                 if (file.exists()) {
    10                     response.setContentType("application/force-download");// 设置强制下载不打开
    11                     response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
    12                     byte[] buffer = new byte[1024];
    13                     FileInputStream fis = null;
    14                     BufferedInputStream bis = null;
    15                     try {
    16                         fis = new FileInputStream(file);
    17                         bis = new BufferedInputStream(fis);
    18                         OutputStream os = response.getOutputStream();
    19                         int i = bis.read(buffer);
    20                         while (i != -1) {
    21                             os.write(buffer, 0, i);
    22                             i = bis.read(buffer);
    23                         }
    24                         System.out.println("success");
    25                         
    26                         InetAddress addr = InetAddress.getLocalHost();  
    27                         String ip=addr.getHostAddress(); //获取本机ip
    28                         replacTextContent(ip);
    29                     } catch (Exception e) {
    30                         e.printStackTrace();
    31                     } finally {
    32                         if (bis != null) {
    33                             try {
    34                                 bis.close();
    35                             } catch (IOException e) {
    36                                 e.printStackTrace();
    37                             }
    38                         }
    39                         if (fis != null) {
    40                             try {
    41                                 fis.close();
    42                             } catch (IOException e) {
    43                                 e.printStackTrace();
    44                             }
    45                         }
    46                     }
    47                 }
    48             }
    49             return null;
    50     }
    51     /**
    52      * 替换文本文件中的 指定字符串
    53      * @param path
    54      * @throws IOException
    55      */
    56     public void replacTextContent(String ip){
    57         try {
    58             File file = new File("D:zabbix_agentd.conf");
    59             String content = FileUtils.readFileToString(file, "utf-8");
    60             //原有的内容
    61             String srcStr = "Hostname=192.168.1.177";        
    62             //要替换的内容
    63             String replaceStr ="Hostname"+ ip;     
    64             //
    65             FileReader in = new FileReader(content);  
    66             BufferedReader bufIn = new BufferedReader(in);  
    67             // 内存流, 作为临时流  
    68             CharArrayWriter  tempStream = new CharArrayWriter();  
    69             // 替换  
    70             String line = null;  
    71             while ( (line = bufIn.readLine()) != null) {  
    72                 // 替换每行中, 符合条件的字符串  
    73                 line = line.replaceAll(srcStr, replaceStr);  
    74                 // 将该行写入内存  
    75                 tempStream.write(line);  
    76                 // 添加换行符  
    77                 tempStream.append(System.getProperty("line.separator"));  
    78             }  
    79             // 关闭 输入流  
    80             bufIn.close();  
    81             // 将内存中的流 写入 文件  
    82             FileWriter out = new FileWriter(file);  
    83             tempStream.writeTo(out);  
    84             out.close();  
    85         }catch (Exception e) {
    86             e.printStackTrace();
    87         }
    88     }
  • 相关阅读:
    强化学习的基本迭代方法
    基于文本描述的事务聚类
    学习强化学习之前需要掌握的3种技能
    其它 华硕 ASAU S4100U 系统安装 win10安装 重装系统 Invalid Partition Table 解决
    数据分析 一些基本的知识
    Python 取样式的内容 合并多个文件的样式 自定义样式
    电商 Python 生成补单公司需要的评论格式3
    SpringBlade 本地图片上传 生成缩略图
    SQL Server 字符串截取
    SpringBlade 本地图片上传
  • 原文地址:https://www.cnblogs.com/fenghh/p/10058306.html
Copyright © 2020-2023  润新知