_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. php接口安全设计浅谈

    接口的安全性主要围绕Token.Timestamp和Sign三个机制展开设计,保证接口的数据不会被篡改和重复调用,下面具体来看: (1)Token授权机制:(Token是客户端访问服务端的凭证)--用 ...

  2. Python 基础之循环结构for及break pass continue

    一.for 循环 #循环 变量 迭代 都是一个意思#把列表里面的元素意义的拿出来就是遍历listvar = ["one","two","three&q ...

  3. 131、Java面向对象之static关键字三(使用static定义方法)

    01.代码如下: package TIANPAN; class Book { // 描述的是同一个出版社的信息 private String title; private double price; ...

  4. 115、Java中String类之使用concat进行字符串连接

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  5. SQLite、MySQL和PostgreSQL 三种关系数据库哪个好?

    关系型数据库的使用已经有相当长的时间了.它们变得流行起来托了管理系统的福,关系模型被实现得相当的好,并且被证明是操作数据的好方法(特别是事务性强的应用). 在这篇DigitalOcean文章中,我们将 ...

  6. 列举mvc ActionResult的返回值

    8.列举ASP.NET MVC ActionResult的返回值有几种类型? 主要有View(视图).PartialView(部分视图).Content(内容).Json(Json字符串).Javas ...

  7. darknet简述

    概述 darknet官网:https://pjreddie.com/darknet/    https://github.com/AlexeyAB/darknet Darknet是一个比较小众的深度学 ...

  8. 基于IntelliJ IDEA的代码评审插件 Code Review Plugin

    一.阿里规范公约 1.左上角 File -> Settings -> Plugins -> 搜索:Alibaba Java Coding Guidelines,安装插件并重启IDEA ...

  9. 「React Native笔记」在React的 setState 中操作数组和对象的多种方法(合集)

    运用在React 中 setState的对象.数组的操作时是不能用类似array.push()等方法,因为push没有返回值,setState后会出现state变成Number,为了方便他人和自己查看 ...

  10. Redis使用守护进程启动sentinel并指定其日志目录

    正常redis-server可以通过配置文件来指定守护进程启动以及指定日志路径,但sentinel就不一样了.正常启动redis的sentinel时,进程会直接在前台跑,一退出sentinel进程就关 ...