SpringBoot实现上传下载(二)
这篇写下载。
**1.实现思路**
上一篇的数据库设计中,我们有一个字段始终没有用到fileName,这是用来给Layer对象存储文件名的,以此来完成文件与对象的对应,

预览:

**2.Code**
View层:
首先是加载数据表格异步的时候 我们就获取到了fileName,然后通过获取当前行,来获取当前的fileName文件名。
```
table.on('tool(test)', function(obj) {
var data = obj.data; //获得当前行数据
var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
var tr = obj.tr; //获得当前行 tr 的DOM对象
$ = layui.jquery;
if (layEvent === 'download') { //删除
var fileName = data.fileName;
window.location="layer/download?fileName="+fileName;
}
});
```
然后不论是上传下载使用ajax都不推荐(上传下载都属于获取资源请求,a标签等即可实现,而ajax是js异步的封装请求,两者实现目标不一样)
这里使用window.location来实现对文件的请求。
然后是Controller层:
```
//下载
@RequestMapping(value = "Index/layer/download")
@ResponseBody
public Map downloadOne(HttpServletRequest req,HttpServletResponse response) throws IOException{
String fileName = req.getParameter("fileName");// 设置文件名,根据业务需要替换成要下载的文件名
// String layerId = req.getParameter("layerId");
String downloadDir = req.getSession().getServletContext().getRealPath("/") +"upload/";
// String savePath = req.getSession().getServletContext().getRealPath("/") +"download/";
downloadDir=downloadDir.substring(0,downloadDir.length()-1);
downloadDir=downloadDir+"\\";//下载目录
String realPath=downloadDir+fileName;//
File file = new File(realPath);//下载目录加文件名拼接成realpath
if(file.exists()){ //判断文件父目录是否存在
// response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = null; //文件输入流
BufferedInputStream bis = null;
OutputStream os = null; //输出流
try {
os = response.getOutputStream();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while(i != -1){
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------file download" + fileName);
try {
bis.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return api.returnJson(2,"fail"+realPath+fileName);
}
```
主要就是调IO流,然后下载罢了,拼一下路径和文件名即可
**3.效果一览**

觉得有用就给颗小
SpringBoot实现上传下载(二)的更多相关文章
- springboot简易上传下载
1.导入上传下载依赖: <dependency> <groupId>commons-fileupload</groupId> <artifactId>c ...
- springboot文件上传下载简单使用
springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- FastDFS的安装及上传下载(二)
百度云:所有附件的地址 一 安装前的检查 检查Linux上是否安装了 gcc.libevent.libevent-devel,执行如下yum命令检查: [root@node02 ~]# yum lis ...
- springboot 文件上传下载
关键点: 1,使用 POST 请求2,consumes=MediaType.MULTIPART_FROM_DATA_VALUE3,@RequestParm 里面的字符串和前端 input 控件的 na ...
- springboot文件上传下载,转载的
Spring Boot入门——文件上传与下载 原文来自:https://www.cnblogs.com/studyDetail/articles/7003253.html 1.在pom.xml文件中添 ...
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- salesforce 零基础学习(四十二)简单文件上传下载
项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文 ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
随机推荐
- mybatis的<用<![CDATA[]] 忽略解析
1 CDATA 术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data). 在 XML 元素中,"<" 和 &quo ...
- 快速禁止Chrome浏览器缓存
在前端的开发中,最麻烦的莫过于浏览器的缓存,经常需要清理缓存文件,导致开发效率较低. 但Chrome可以一键禁止浏览器缓存,并且在后续的操作中,无论相同的资源请求多少次,都不会缓存到本地,一起来体验下 ...
- Read-Only Tables 只读表
Put a table into read-only mode,which prevents DDL or DML changes during table maintenance Put the ...
- 如何查看Codeforces的GYM中比赛的数据
前置条件:黄名(rating >= 2100) 或者 紫名(rating >= 1900)并且打过30场计分的比赛. 开启:首先打开GYM的界面,如果符合要求会在右边展示出一个Coach ...
- 1146. Topological Order (25)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- objectMaaper 反序列化json字段多于或少于实体处理
两种方式 1 忽略json上未知的字段 设置实体类注解 @JsonIgnoreProperties(ignoreUnknown = true) public class Foo { ... } 2 配 ...
- mysql数据同步到Elasticsearch
1.版本介绍 Elasticsearch: https://www.elastic.co/products/elasticsearch 版本:2.4.0 Logstash: https://www ...
- alert(1) to win 10
- hibernate插入数据测试无异常,但数据库没有数据
解决方法: spring test测试默认会将事务回滚,如果想阻止spring transactional回滚,在test方法上加注解@Rollback(false)即可. Hibernate hql ...
- 大数据分析:hadoop工具
一.hadoop工具 Hadoop介绍: Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储 ...