ApiOperation(value = "下载文件", httpMethod = "GET", notes = "downloadFile", response = String.class)
public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "zabbix.txt";// 设置文件名
if (fileName != null) {
//设置文件路径
// String realPath = "D://zabbix_agentd.conf";
String realPath = "D:" + File.separator + "zabbix_agentd.conf";
File file = new File(realPath , fileName);
if (file.exists()) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("success"); InetAddress addr = InetAddress.getLocalHost();
String ip=addr.getHostAddress(); //获取本机ip
replacTextContent(ip);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
/**
* 替换文本文件中的 指定字符串
* @param path
* @throws IOException
*/
public void replacTextContent(String ip){
try {
File file = new File("D:zabbix_agentd.conf");
String content = FileUtils.readFileToString(file, "utf-8");
//原有的内容
String srcStr = "Hostname=192.168.1.177";
//要替换的内容
String replaceStr ="Hostname"+ ip;
// 读
FileReader in = new FileReader(content);
BufferedReader bufIn = new BufferedReader(in);
// 内存流, 作为临时流
CharArrayWriter tempStream = new CharArrayWriter();
// 替换
String line = null;
while ( (line = bufIn.readLine()) != null) {
// 替换每行中, 符合条件的字符串
line = line.replaceAll(srcStr, replaceStr);
// 将该行写入内存
tempStream.write(line);
// 添加换行符
tempStream.append(System.getProperty("line.separator"));
}
// 关闭 输入流
bufIn.close();
// 将内存中的流 写入 文件
FileWriter out = new FileWriter(file);
tempStream.writeTo(out);
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}

用Springboot实现文件下载功能的更多相关文章

  1. springboot成神之——spring文件下载功能

    本文介绍spring文件下载功能 目录结构 DemoApplication WebConfig TestController MediaTypeUtils 前端测试 本文介绍spring文件下载功能 ...

  2. SpringBoot之文件下载

    package org.springboot.controller; import org.springboot.constant.Constant; import org.springframewo ...

  3. JAVA文件下载功能问题解决日志

    今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...

  4. 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题

    问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...

  5. WebView实现文件下载功能

    WebView控制调用相应的WEB页面进行展示.安卓源码当碰到页面有下载链接的时候,点击上去是一点反应都没有的.原来是因为WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置W ...

  6. Spring Boot实现文件下载功能

    我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下: @Controller public class File_D ...

  7. Spring Boot入门(11)实现文件下载功能

      在这篇博客中,我们将展示如何在Spring Boot中实现文件的下载功能.   还是遵循笔者写博客的一贯风格,简单又不失详细,实用又能让你学会.   本次建立的Spring Boot项目的主要功能 ...

  8. ASP.NET网页中RAR、DOC、PDF等文件下载功能实例源代码

    以前做asp.net下载功能的时候都是采用:<a href="http://www.wang0214.com/wgcms">下载</a>的方式来实现下载. ...

  9. java web文件下载功能实现 (转)

    http://blog.csdn.net/longshengguoji/article/details/39433307 需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一 ...

随机推荐

  1. __x__(30)0908第五天__导航条的练习 <div>版本

    效果图:  html源代码: <!doctype html> <html> <head> <meta charset="utf-8" /& ...

  2. ECMA Script 6_函数的扩展

    ES6规定只要函数参数使用了默认值.解构赋值.或者扩展运算符, 那么函数内部就不能显式设定为严格模式,否则会报错 1. 参数的默认值 ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面 函数 ...

  3. sql基本

    SELECT: select * from table select 列名 from table select DISTINCT 列名 from table INSERT: insert into t ...

  4. CentOS6.5yum配置本地源

    进入repos.d目录 cd /etc/yum.repos.d 创建临时文件夹repo.bak(文件夹名随意起 使用root权限) 将以下文件移到repo.bak文件夹(以防备用) -rw-r--r- ...

  5. DjangoRestFramework 学习之restful规范 APIview 解析器组件 Postman等

    DjangoRestFramework学习一之restful规范.APIview.解析器组件.Postman等 本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析器组件 ...

  6. CGI的工作原理

    文章摘自https://blog.csdn.net/nyist327/article/details/41049699 CGI是Web服务器和外部程序之间的一个接口.利用CGI程序可以处理从Web上客 ...

  7. js对重复数组去重

    var arr=[1,1,1,1,2,2,2,3,3,4,1,4,5,7,8,7,7] let m = {} arr.filter(item => m[item] >= 1 ? false ...

  8. robotframework接口之上传图片

    python-requests及robotframework-RequestsLibrary实现multipart/form-data接口上传文件. 如Fiddle抓包截图: 实现如截图: 不要自己在 ...

  9. DCDC参数测量及方法

    此文章目的为补充知识,防止遗忘,记录DCDC相关的. 1.拿到一颗DCDC芯片应该测试哪些参数:纹波.电源效率和动态响应. 1)纹波测量方法:示波器偶合方式选择AC:示波器探头的接地也不能用鳄鱼夹,这 ...

  10. Modelsimobjects空白无显示问题和ISim仿真时出现空白

    困扰朕长达一周的问题得以解决!!!!! 发生这种情况的根源是win10自带的防火墙的问题.只有关闭防火墙,再重新打开软件进行仿真就能出现正常的仿真界面. 关闭防火墙的方法为:控制面板>>系 ...