_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的更多相关文章

  1. Springboot Download file

    @RequestMapping(value = "/downloadSvt") public ResponseEntity<FileSystemResource> ex ...

  2. 优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download

    优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download TTFB全称Time To First Byte,是指网络请求被发起到从服务器接收到地 ...

  3. IDEA快速创建Maven+SpringBoot项目时:Cannot download https://start.spring.io;Status:403

    先展示一下我遇到的问题: 用浏览器搜索是有页面的,但是但是但是呢,用IDEA快速构建的时候就报403 咳咳!巴格虐我万千遍,我待技术如初恋... 我看到的解决办法有以下两种,当然,我只想说:" ...

  4. springboot Serving Web Content with Spring MVC

    Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...

  5. Springboot数据访问,棒棒哒!

    Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...

  6. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、属性配置、多环境配置

    1 InteliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到官 ...

  7. Jrebel热部署配置完整教程(IntelliJ IDEA、Jrebel、spring boot、springboot、eclipse、Tomcat)

    标签:IntelliJ IDEA.Jrebel.spring boot.springboot.eclipse.Tomcat1.安装插件并激活插件安装参考:http://blog.csdn.net/u0 ...

  8. SpringBoot入坑-项目搭建

    对于学过三大框架的小童鞋,从今天开始给大家带来一套新的框架学习,相信对于做程序的小童鞋一定有所耳闻,作为下一代java开发框架springboot,减去了繁琐的xml配置,相信用过spring.sta ...

  9. springboot之集成mybatis mongo shiro druid redis jsp

    闲来无事,研究一下spingboot  发现好多地方都不一样了,第一个就是官方默认不支持jsp  于是开始狂找资料  终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...

随机推荐

  1. <audio>音频标签

    <audio ref="audio" @canplay="ready" @error="error"  @timeupdate=&qu ...

  2. eclispe javaw.exe in your current path的解决方法

    https://blog.csdn.net/bd2569/article/details/70576666 问题背景:解压缩下面文件夹后打开eclipse软件(是安装包,解压即可使用的那种)弹出“.. ...

  3. 清除编译缓存DerivedDate

    当多次重构工程造成代码没有错误却编译失败时,可以尝试删除DerivedData目录.DerivedData目录是Xcode的编译缓存,路径是~/Library/Developer/Xcode/Deri ...

  4. JS监听手机物理返回键,返回到指定页面

    pushHistory(); window.addEventListener("popstate", function(e) { window.location = data.in ...

  5. 字符串替换 (replace)

    将文本文件中指定的字符串替换成新字符串. 由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束.end后面有两个字符串,要求用第二个字符串替 ...

  6. JavaWeb开发:从购买服务器到简单demo运行

    写这篇文章的目的: 一个是为了记录实施过程,方便自己日后查阅: 另一个是给项目组成员提供一个参考,方便他们以后搭建自己的项目环境: 当然若能帮助到更多的朋友,那就再好不过了:D 需要注意: 我本身也是 ...

  7. ThreadLocal的原理和在框架中的应用

    ThreadLocal的原理和在框架中的应用 博客分类: java基础 框架多线程SpringthreadDAO  概述      我们知道Spring通过各种DAO模板类降低了开发者使用各种数据持久 ...

  8. asp.net获取时间日期插入数据库

    //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...

  9. Lesson 7 Bats

    In what way does echo-location in bats play a utilitarian role? Not all sounds made by animals serve ...

  10. elk基本配置

    https://www.cnblogs.com/zsl-find/articles/10730458.html https://www.cnblogs.com/mylovelulu/p/1053000 ...