服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - springboot 中文件上传下载实例代码

springboot 中文件上传下载实例代码

2021-02-05 12:03yshy JAVA教程

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。这篇文章主要介绍了springboot 中文件上传下载实例代码,需要的朋友可以参考下

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

Spring Boot特点

1. 创建独立的Spring应用程序

2. 嵌入的Tomcat,无需部署WAR文件

3. 简化Maven配置

4. 自动配置Spring

5. 提供生产就绪型功能,如指标,健康检查和外部配置

6. 绝对没有代码生成和对XML没有要求配置[

springboot 实现文件上传下载实例代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@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));
 }
}

总结

以上所述是小编给大家介绍的springboot 中文件上传下载实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/yshyee/p/7839101.html

延伸 · 阅读

精彩推荐