SpringBoot#Download
_amaze!
如果不使用fastdfs等分布式的文件存储,有时候还是需要上传文件到web应用所在的服务器的磁盘上,下载文件。下面是一个小demo,关于如何用控制器进行上传和下载。
-
@PostMapping(value="/upload",produces="application/json; charset=utf-8")
@ResponseBody
public String upload(@RequestParam(value="file", required=false) MultipartFile file, HttpServletRequest req) {
//String t = req.getContentType();
//System.out.println("content type :"+t); String filePath = "c:/upload/";
if (file == null) {
return "200";
} String fileName = file.getOriginalFilename();
File dest = new File(filePath + fileName); try {
file.transferTo(dest);
System.out.println("上传成功:"+dest.getAbsolutePath());
return "上传成功:"+dest.getAbsolutePath();
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
return "上传失败!"+e.getLocalizedMessage();
} @RequestMapping("/download/xxxx/{uid}")
public String downLoad2(HttpServletResponse response, @PathVariable long uid){
String filename=uid+"_front.png";
File file = xxxxService.xxxxMethod(uid);
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) {
e.printStackTrace();
}
System.out.println("----------file download:" + filename);
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
-
SpringBoot#Download的更多相关文章
- Springboot Download file
@RequestMapping(value = "/downloadSvt") public ResponseEntity<FileSystemResource> ex ...
- 优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download
优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download TTFB全称Time To First Byte,是指网络请求被发起到从服务器接收到地 ...
- IDEA快速创建Maven+SpringBoot项目时:Cannot download https://start.spring.io;Status:403
先展示一下我遇到的问题: 用浏览器搜索是有页面的,但是但是但是呢,用IDEA快速构建的时候就报403 咳咳!巴格虐我万千遍,我待技术如初恋... 我看到的解决办法有以下两种,当然,我只想说:" ...
- springboot Serving Web Content with Spring MVC
Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...
- Springboot数据访问,棒棒哒!
Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...
- SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、属性配置、多环境配置
1 InteliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到官 ...
- Jrebel热部署配置完整教程(IntelliJ IDEA、Jrebel、spring boot、springboot、eclipse、Tomcat)
标签:IntelliJ IDEA.Jrebel.spring boot.springboot.eclipse.Tomcat1.安装插件并激活插件安装参考:http://blog.csdn.net/u0 ...
- SpringBoot入坑-项目搭建
对于学过三大框架的小童鞋,从今天开始给大家带来一套新的框架学习,相信对于做程序的小童鞋一定有所耳闻,作为下一代java开发框架springboot,减去了繁琐的xml配置,相信用过spring.sta ...
- springboot之集成mybatis mongo shiro druid redis jsp
闲来无事,研究一下spingboot 发现好多地方都不一样了,第一个就是官方默认不支持jsp 于是开始狂找资料 终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...
随机推荐
- Python 基础之返回值与函数使用与局部变量和全局变量locals() 和 globals()
一.函数的返回值 return return: 自定义返回值,返回到哪里? 返回到函数的[调用处]1.return 后面可以跟上六个标准数据类型,除此之外,可以跟上 类对象,函数,如果不写return ...
- hdu 1599 find the mincost route floyd求无向图最小环
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 初识Prometheus
安装Prometheus Server Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖.用户只需要下载对应平台的二进制包,解压并且添加基本的配置即可正常启动Prom ...
- 页面的五种布局以及嵌套『Android系列八』
转自:http://blog.csdn.net/dazlly/article/details/7860125 因为学习比较晚,我用的相关版本为SDK4.1.eclipse4.2,而自己看的教材都是低版 ...
- Linux CentOS7 VMware 安装软件包的三种方法、rpm包介绍、rpm工具用法、yum工具用法、yum搭建本地仓库
一.安装软件包的三种方法 Linux下游三种安装方法,rpm工具.yum工具.源码包.rpm按装一个程序包时,有可能因为该程序包依赖另一个程序包而无法安装:yum工具,可以连同依赖的程序包一起安装. ...
- JDBC原理及常见错误分析
1.JDBC:Java DataBase Connectivity 可以为多种关系型数据库DBMS 提供统一的访问方式,用Java来操作数据库 2.JDBC API 主要功能: 三件事,具体是通过以下 ...
- 「CF10D」LCIS
传送门 Luogu 解题思路 首先考虑怎么求方案,这样才可能会输出方案. 考虑 \(\text{DP}\). 设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的 ...
- IOS 常用功能代码
1. 关闭/隐藏键盘 resignFirstResponder 响应view的方法 -(IBAction)fname:(id)sender{ [sender resignFirstResponder] ...
- 图的Prim,Kruskal,Dijkstra,Floyd算法
代码部分有点问题,具体算法没问题, 最近期末考,要过段时间才会修改 //邻接矩阵,具体情况看上一篇的图的实现template<class T>class MGraph {public: ...
- linux下FTP的工具和使用以及rpmReadSignature failed错误
安装rpm文件时提示rpmReadSignature failed 错误 2011-09-23 11:04 现象: [root@localhost share]# rpm -ivh syslog- ...