SpringBoot(三):文件下载
在原来的SpringBoot–uploadfile项目基础上添加文件下载的Controller:
@RequestMapping(value = "/testDownload", method = RequestMethod.GET)
public void Download(HttpServletResponse res) {
String fileName = "1.png";
res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(new File("d://"
+ fileName)));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("success");
}
需要下载的文件放在D盘。
@RequestMapping(value = "/download", method = RequestMethod.GET)
public String Download() {
return "/fileDownload";
}
fileDownload.html:
<html>
<head>
<meta charset="UTF-8"/>
<title>文件下载示例</title>
</head>
<body>
<h2>文件下载示例</h2>
<hr/>
<a href="/testDownload">下载</a>
</body>
</html>
SpringBoot(三):文件下载的更多相关文章
- SpringBoot的文件下载
SpringBoot的文件下载 2017年11月29日 10:32:20 阅读数:3907 SpringBoot的文件下载方法有很多,此处只记录使用Spring的Resource实现类FileSyst ...
- SpringBoot/SpringMVC文件下载方式
本篇文章引用外网博客代码,共描述SpringMVC下三种文件下载方式,本人测试在SpringBoot(2.0以上版本)正常使用. 引用博客,强烈推荐https://www.boraji.com. pa ...
- spring-boot (三) spring data jpa
学习文章来自:http://www.ityouknow.com/spring-boot.html spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence ...
- SpringBoot之文件下载
package org.springboot.controller; import org.springboot.constant.Constant; import org.springframewo ...
- SpringBoot 三种方式配置 Druid(包括纯配置文件配置)
记录一下在项目中用纯 YML(application.yml 或者 application.properties)文件.Java 代码配置 Bean 和注解三种方式配置 Alibaba Druid 用 ...
- SpringBoot(三) -- SpringBoot与日志
一.日志的起源 现在假设一个开发人员在开发一个大型系统,由于这个系统过于庞大没在很多的地方将关键的数据使用System.out.println()打印,但是当我们在项目正式上线时又需要去除,在项目bu ...
- SpringBoot(三)SpringBoot自动配置
我们都知道SpringBoot帮助我们集成了许多组件和配置,那么SpringBoot是如何集成这些配置并在启动是自动进行配置呢.说到这就不得又需要回过头来看一下@SpringBootApplicati ...
- Java开发学习(三十六)----SpringBoot三种配置文件解析
一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...
- SpringBoot(三) - Slf4j+logback 日志,异步请求,定时任务
1.Slf4j+logback 日志 SpringBoot框架的默认日志实现:slf4j + logback: 默认日志级别:info,对应了实际生产环境日志级别: 1.1 日志级别 # 常见的日志框 ...
- springboot(三):Spring boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
随机推荐
- adb 切换android输入法
自动化测试执行时,使用了appium输入法,再手动使用时,需要进入设置里面进行切换.adb 也提供了一条命令使用命令切换 1.找出android里面有多少输入法:adb shell ime list ...
- 【Unity】12.1 基本概念
开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 导航网格(Navmesh)是世界坐标系中几何体的简化表示,被游戏代理用来进行全局导航.通常,代 ...
- 哈佛大学 Machine Learning
https://am207.github.io/2017/material.html https://am207.github.io/2017/topics.html https://am207.gi ...
- elasticsearch 索引延迟 一致性问题等
https://www.elastic.co/guide/en/elasticsearch/guide/current/near-real-time.html https://www.elastic. ...
- 未能为数据库 '*'中得对象'*'分配空间,因文件组'PRIMARY'已满
服务器使用mssqlserver2005,最近经常出现无法新增信息错误,查看日志,发现严重错误提示,内容大致为: 无法为数据库 'weixin_main' 中的对象 'dbo.wx_logs'.'PK ...
- [Windows Azure] Management REST API Reference
Management REST API Reference 27 out of 42 rated this helpful - Rate this topic The SQL Database Man ...
- debian8.5安装sublime text3
在官网www.sublimetext.com下载安装包 我这里用的是Ubuntu 64 bit版. 下载后使用su命令切换到root账户. 执行安装命令 dpkg -i sublime-text*.d ...
- psycopg事务
1.事务的特性 原子性,要么完成,要么都不完成 2.psycopg的事务特点 在psycopg中,事务是由connection处理的,当第一次一个命令发送给数据库时(开启数据库操作游标), 一个事务就 ...
- ASP.NET MVC@model多个对象
需求:同时查看一个产品信息和发布会员的信息 方案1:新建ViewModel 方案2:ViewData.ViewBag 方案3:Tuple<obj1,obj2> 在这里我们使用方案3来实现这 ...
- android 修改listview item view 的方法
具体的解答办法很简单: 代码如下 : 1.获取需要更新的view int visiblePosition = mListView.getFirstVisiblePosition(); View vie ...