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. rabbitmq应用

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: zengchunyun ""& ...

  2. java常用的逻辑

    /** * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). * <p> * Licensed under th ...

  3. centos7安装python3 以及tab补全功能

    1.安装python3 1.1下载python源码包 网址:https://www.python.org/downloads/release/python-362/ 下载地址:https://www. ...

  4. linux学习:特殊符号,数学运算,图像与数组与部分终端命令用法整理

    指令:let.expr.array.convert.tput.date.read.md5.ln.apt.系统信息 一:特殊符号用法整理 系统变量 $# 是传给脚本的参数个数 $0 是脚本本身的名字 $ ...

  5. JMeter参数化中存在逗号的解决方法

    在Jmeter中通过CSV Data Set Config进行参数化时,如果参数化数据中存在逗号(,)我们可以通过一下方式进行设置 如何存在中文乱码,可以设置file encoding:gb2312

  6. JavaScript基础知识(Date 的方法)

    Date 的方法 console.log(typeof Date);// "function" Date的实例 console.log(typeof new Date()); // ...

  7. Linux系统安装tomcat

    1.首先下载tomcat:http://tomcat.apache.org/download-60.cgi 2.解压缩tar.gz文件: tar -xzvf xxxxxxx/apache-tomcat ...

  8. SourceTree安装跳过登录

    安装 SourceTree 时,需要使用atlassian授权,因为各种原因无法完成授权,现提供跳过 atlassian账号 授权方法. 安装之后,转到用户本地文件夹下的 SourceTree 目录, ...

  9. about this blog

    这个博客大概是被我用来整理模板的吧╮(╯▽╰)╭ 因为本小盆友巨懒,99.9%是不会写什么题解或者学习笔记什么的

  10. Jmeter设置默认中文页面

    方法一(从网上看到的) 启动Jmeter找到 options >choose language >chinese(简体繁体自己选). 这样设置后界面就变成了中文,但是当我们下次打开时又恢复 ...