html 代码 js部分
window.location.href= this.Baseurl+'/plan/down?file='+filename;

  

spring  boot 后台代码
@GetMapping("/down")
public HttpServletResponse down(HttpServletResponse response,
HttpServletRequest request,@RequestParam("file") String files ) {
String fileName = files;// 设置文件名,根据业务需要替换成要下载的文件名
if (fileName != null) {
//设置文件路径
String pahre=System.getProperty("user.dir")+ "\\report\\";
File file = new File(pahre , 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);
}
} 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;
}

文件的默认位置在当前运行目录的report文件下

个人公众号

spring boot 实现文件下载的更多相关文章

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

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

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

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

  3. [Spring boot] web应用返回jsp页面

    同事创建了一个spring boot项目,上传到svn.需要我来写个页面.下载下来后,始终无法实现在Controller方法中配置直接返回jsp页面. 郁闷了一下午,终于搞定了问题.在此记录一下. 目 ...

  4. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  5. java代码自动下载Spring Boot用户手册

    本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载 pom.xml <?xml version="1.0" encoding="UTF- ...

  6. Spring Boot 单元测试详解+实战教程

    Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...

  7. Spring Boot 文件上传与下载

    原文地址: https://www.cnblogs.com/studyDetail/p/7003253.html 1.在pom.xml文件中添加依赖 <project xmlns="h ...

  8. spring boot整合mybatis+mybatis-plus

    Spring boot对于我来说是一个刚接触的新东西,学习过程中,发现这东西还是很容易上手的,Spring boot没配置时会默认使用Spring data jpa,这东西可以说一个极简洁的工具,可是 ...

  9. Spring Boot入门——文件上传与下载

    1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

随机推荐

  1. 使用 Azure 门户创建 Windows 虚拟机

    可以通过 Azure 门户创建 Azure 虚拟机. 此方法提供一个基于浏览器的用户界面,用于创建和配置虚拟机和所有相关的资源. 本快速入门介绍了如何创建虚拟机并在 VM 上安装 webserver. ...

  2. [SQLSERVER] [GPO] Add the Log on as a service Right to an Account

    Add the Log on as a service Right to an Account Updated: August 8, 2008 Applies To: Windows Server 2 ...

  3. RHEL7.3安装mysql5.7

    RHEL7.3 install mysql5.7 CentOS7默认安装MariaDB而不是MySQL,而且yum服务器上也移除了MySQL相关的软件包.因为MariaDB和MySQL可能会冲突,需先 ...

  4. GitKraken 团队项目使用教程

    前期准备:刚下载好要配置些东西,这些跟着教程走就好了,现在主要是讲团队项目的使用,创建分支 和Fork项目到个人仓库,请参考这篇博客的前两个点 1.将项目克隆到本地 打开GitKraken 点击Fil ...

  5. 2.Linux环境下配置Solr4.10.3

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.准备阶段 操作系统:CentOS 6.8 安装包:/home/test solr-4.10.3.tgz.t ...

  6. python第二十七课——os模块

    演示os模块中常用的属性和函数: 1.os模块: 作用:管理文件和目录 属性: os.name:返回系统类型 常用的windows系统 --> nt os.environ:返回当前系统所有的环境 ...

  7. Nodejs学习资源汇总

    Node.js v6.3.1 Documentation https://nodejs.org/dist/latest-v6.x/docs/api/​ npm官网  https://www.npmjs ...

  8. 协程运行原理猜测: async/await

    1.根据await调用链寻找最终的生产者或服务提供者: 2.请求服务: 3.进行执行环境切换,跳出顶层函数(第一个无await修饰的函数),执行后面的语句: 4.服务完成,将服务数据复制给最底层的aw ...

  9. CentOS7+Nginx设置Systemctl restart nginx.service服务

    centos 7上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.关于 ...

  10. trycatche

    <?phpheader("Content-type: text/html; charset=utf-8"); try{$aaa = 0;if ($aaa == 9) {ech ...