_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. linux命令系列-mv(移动-重命名)

    #常用命令选项 默认覆盖 -n 不覆盖 -i 交互 -f 不交互直接覆盖 -u 只移动新的文件 -v 显示详细信息 #移动覆盖3个文件到/tmp目录 mv a.txt b.txt c.txt /tmp ...

  2. 1 网页及浏览器内核&Web标准

    网页的组成: 主要由文字.图像和超链接等元素构成,还可以包含音频.视频以及flash. 浏览器内核: 浏览器内核分为两部分: 1 渲染引擎(layout engineer) 渲染引擎负责取得网页的内容 ...

  3. Hystrix熔断机制导致误报请求超时错误

    问题的过程如下: (1)前端向服务端请求往HBase插入1000条数据: (2)请求经路由网关Zuul传递给HBaseService,HBaseService执行插入操作: (3)插入操作需要的时间超 ...

  4. Python 关于super在多继承中的解析

    一.单继承情况: 解析: 创建B类的实例化对象时,执行初始化函数: 打印输出Enter B,当遇到super()调用父类初始化函数(此时是调用B类的父类A的__init__函数),输出Enter A. ...

  5. RCast 66: 射影几何与Rho演算

    Greg Meredith与Isaac DeFrain和Christian Williams一起讨论了射影几何及其在Rho演算中的作用. 原文链接及音频 https://blog.rchain.coo ...

  6. PyQt5点击菜单栏弹出新窗口,解决新窗口闪退的实现方法

    实现的功能为:当点击菜单中某个菜单时,会弹出一个新窗口,下面就列出部分代码 def mail_setting(self): log.debug("open mail settings&quo ...

  7. 通用dao的demo

          代码片段 1. [代码]整型映射工具 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package org.dave.common.databas ...

  8. Matplotlib 基本概念

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  9. 1_01_MSSQL课程_基础入门2

    1.数据库的迁移方案 ->分离 附加 ->权限问题: ->启用Administrator账号 ->把数据库文件放到默认的数据库文件存放目录. ->数据库文件兼容级别,设置 ...

  10. Elasticsearch的快速使用——Spring Boot使用Elastcisearch, 并且使用Logstash同步mysql和Elasticsearch的数据

    我主要是给出一些方向,很多地方没有详细说明.当时我学习的时候一直不知道怎么着手,花时间找入口点上比较多,你们可以直接顺着方向去找资源学习. 如果不是Spring Boot项目,那么根据Elastics ...