spring-boot 实现文件上传下载
@Controller
public class FileUploadCtrl {
@Value("${file.upload.dir}")
private String path; /**
* 实现文件上传
* */
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> fileUpload(@RequestParam("fileName") MultipartFile file){
Map<String,Object> map = new HashMap<String, Object>();
int no = 0;
String msg = "上传失败!"; if(!file.isEmpty()){
String fileName = file.getOriginalFilename(); File dest = new File(path + "/" + fileName); if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
dest.getParentFile().mkdir();
} try {
file.transferTo(dest); //保存文件
no = 1;
msg = "上传成功!";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} map.put("no",no);
map.put("msg", msg); return map;
} @RequestMapping(
value = "/fileDownload",
method = RequestMethod.GET
)
public ResponseEntity<?> getGwFileContent(@RequestParam String fileName,@RequestParam int flag) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
String filepath = path+"/"+fileName;; InputStream is = null; try {
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(fileName.getBytes("GBK"), "ISO8859-1"))); if(flag==0){//表示获取缩略图
File file = new File(filepath); filepath = path+"/xx"+fileName;
File xxFile = new File(filepath);
if(!xxFile.exists()){//不存在就生成缩略图
Thumbnails.of(file).scale(0.25f).toFile(xxFile);
}
} is = new FileInputStream(new File(filepath));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} headers.add("Pragma", "no-cache");
headers.add("Expires", "0"); return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(is));
} }
spring-boot 实现文件上传下载的更多相关文章
- spring boot实现文件上传下载
spring boot 引入”约定大于配置“的概念,实现自动配置,节约了开发人员的开发成本,并且凭借其微服务架构的方式和较少的配置,一出来就占据大片开发人员的芳心.大部分的配置从开发人员可见变成了相对 ...
- Spring Boot 教程 - 文件上传下载
在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...
- Spring Boot入门——文件上传与下载
1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)
一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...
- spring mvc注解文件上传下载
需要两个包: 包如何导入就不介绍了,前端代码如下(一定要加enctype="multipart/form-data"让服务器知道是文件上传): <form action=&q ...
- Spring Boot—04文件上传
package com.smartmap.sample.ch1.controller.view; import java.io.File; import java.io.IOException; im ...
- spring mvc 实现文件上传下载
/** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...
- spring boot Tomcat文件上传找不到零时文件夹
springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...
- Spring Boot RestTemplate文件上传
@ResponseBody @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public St ...
- Spring boot设置文件上传大小限制
原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...
随机推荐
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- python测试开发django-23.admin列表页优化和排序
前言 列表页优化和排序 ModelAdmin django的options.py里面 ModelAdmin类定义的参数可以设置admin后台列表页面,相关的参数如下 class ModelAdmin( ...
- linux内核netfilter模块分析之:HOOKs点的注册及调用
转自;http://blog.csdn.net/suiyuan19840208/article/details/19684883 -1: 为什么要写这个东西?最近在找工作,之前netfilter 这一 ...
- Java中使用正则表达式获取网页中所有图片的路径
public static List<String> getImageSrc(String htmlCode) { List<String> imageSrcList = ne ...
- vanilla
In information technology, vanilla (pronounced vah-NIHL-uh ) is an adjective meaning plain or basic. ...
- PHP读写INI文件
读INI文件 public function readini($name) { if (file_exists(SEM_PATH.'init/'.$name)){ $data = parse_ini_ ...
- [转]如何将PHP作为Shell脚本语言使用
From : http://www.linuxfly.org/post/559/ 我们都知道,PHP是一种非常好的动态网页开发语言(速度飞快,开发周期短……).但是只有很少数的人意识到PHP也可以很好 ...
- sql server 复制,镜像,日志传输及故障转移集群区别
一, 数据库复制 SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份.这里的同步备份指的是备份服务器与主服务器进行 实时数据同步, ...
- jQuery EasyUI Datagrid性能优化专题(转)
jQuery EasyUI的Datagrid组件功能算是很强大了,不过性能确实不怎么乐观,而对于性能问题,网络上几乎也找不到相关的优化资料,所谓的牛人们可能 都望而却步了.本博客以后会带着分析Dat ...
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...