@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 实现文件上传下载的更多相关文章

  1. spring boot实现文件上传下载

    spring boot 引入”约定大于配置“的概念,实现自动配置,节约了开发人员的开发成本,并且凭借其微服务架构的方式和较少的配置,一出来就占据大片开发人员的芳心.大部分的配置从开发人员可见变成了相对 ...

  2. Spring Boot 教程 - 文件上传下载

    在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...

  3. Spring Boot入门——文件上传与下载

    1.在pom.xml文件中添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  4. spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)

    一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...

  5. spring mvc注解文件上传下载

    需要两个包: 包如何导入就不介绍了,前端代码如下(一定要加enctype="multipart/form-data"让服务器知道是文件上传): <form action=&q ...

  6. Spring Boot—04文件上传

    package com.smartmap.sample.ch1.controller.view; import java.io.File; import java.io.IOException; im ...

  7. spring mvc 实现文件上传下载

    /** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...

  8. spring boot Tomcat文件上传找不到零时文件夹

    springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...

  9. Spring Boot RestTemplate文件上传

    @ResponseBody @RequestMapping(value = "/upload.do", method = RequestMethod.POST) public St ...

  10. Spring boot设置文件上传大小限制

    原文:https://blog.csdn.net/lizhangyong1989/article/details/78586421 Spring boot1.0版本的application.prope ...

随机推荐

  1. Maven settings配置中的mirrorOf

    原文地址:http://blog.csdn.net/isea533/article/details/21560089 使用maven时,从来没仔细注意过setting配置节点的作用,直到今天配置总是不 ...

  2. ios之如何读取plist

    - (NSDictionary*)contactsInfoFromPlistNamed:(NSString*)plistName { NSString *path = [[NSBundle mainB ...

  3. secureCRT在Windows上面的安装过程

    参考这篇文章: https://www.cnblogs.com/yjd_hycf_space/p/7729796.html

  4. hdu 3660 Alice and Bob's Trip(树形DP)

    Alice and Bob's Trip Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. IOS开发之自定义UITabBarController

    UITabBarController是开发中经常会用到的一个视图控制器,但是默认的UITabBarController经常不能够完全满足我们的需求,所以我们经常需要自定义一个UITabBarContr ...

  6. NSURLSession下载

    ios7引入了NSURLSession,通过它可以支持后台相关的网络操作的新特性: 1. 后台上传下载: 2.不需要通过nsoperation,直接用nsurlsession的api就可以做到网络操作 ...

  7. 在centos服务器上启用ipv6地址

    随着互联网世界日新月异的发展,ipv6好像已经成为一种必不可少的趋势,但是当前国内机房大部分还不能支持ipv6,腾讯云亦如此.同时,现在有部分程序在服务器上运行的时候,需要服务器能监听一个ipv6地址 ...

  8. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十八):kafka0.10.1 内置性能测试API用法示例

    消费者测试: ./kafka-consumer-perf-test..com.cn:,vm10..com.cn:,vm10..com.cn: --group test-teg1 --messages ...

  9. 基于ARM的模拟器

    ARM的ARMulator: ARMulator 是一个在 ARM 公司推出的集成开发环境 ADS (ARM Developer Suite)中提供的指令集模拟器.它与运行在通用计算机(通常是x86体 ...

  10. Logcat用法初探

    首先定位到adb所在的目录 将手机连接上电脑. 在命令行运行: adb devices 这个命令可以列出所有连上的移动设备. 在命令行运行: adb logcat 可以显示日志.   以下是例子截图: ...