当前位置: 首页 > news >正文

企业级 SpringBoot 教程 (十七)上传文件

这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件。

构建工程

为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖。为例能够上传文件在服务器,你需要在web.xml中加入标签做相关的配置,但在sringboot 工程中,它已经为你自动做了,所以不需要你做任何的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

  

创建文件上传controller

直接贴代码:

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
@Controller
public class FileUploadController {
private final StorageService storageService;
@Autowired
public FileUploadController(StorageService storageService) {
this .storageService = storageService;
}
@GetMapping ( "/" )
public String listUploadedFiles(Model model) throws IOException {
model.addAttribute( "files" , storageService
.loadAll()
.map(path ->
MvcUriComponentsBuilder
.fromMethodName(FileUploadController. class , "serveFile" , path.getFileName().toString())
.build().toString())
.collect(Collectors.toList()));
return "uploadForm" ;
}
@GetMapping ( "/files/{filename:.+}" )
@ResponseBody
public ResponseEntity<Resource> serveFile( @PathVariable String filename) {
Resource file = storageService.loadAsResource(filename);
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" +file.getFilename()+ "\"" )
.body(file);
}
@PostMapping ( "/" )
public String handleFileUpload( @RequestParam ( "file" ) MultipartFile file,
RedirectAttributes redirectAttributes) {
storageService.store(file);
redirectAttributes.addFlashAttribute( "message" ,
"You successfully uploaded " + file.getOriginalFilename() + "!" );
return "redirect:/" ;
}
@ExceptionHandler (StorageFileNotFoundException. class )
public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
return ResponseEntity.notFound().build();
}
}

  

这个类通过@Controller注解,表明自己上一个Spring mvc的c。每个方法通过
@GetMapping 或者@PostMapping注解表明自己的 http方法。

  • GET / 获取已经上传的文件列表
  • GET /files/{filename} 下载已经存在于服务器的文件
  • POST / 上传文件给服务器

创建一个简单的 html模板

为了展示上传文件的过程,我们做一个界面:
在src/main/resources/templates/uploadForm.html

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
<html xmlns:th= "http://www.thymeleaf.org" >
<body>
<div th: if = "${message}" >
<h2 th:text= "${message}" />
</div>
<div>
<form method= "POST" enctype= "multipart/form-data" action= "/" >
<table>
<tr><td>File to upload:</td><td><input type= "file" name= "file" /></td></tr>
<tr><td></td><td><input type= "submit" value= "Upload" /></td></tr>
</table>
</form>
</div>
<div>
<ul>
<li th:each= "file : ${files}" >
<a th:href= "${file}" th:text= "${file}" />
</li>
</ul>
</div>
</body>
</html>

  

上传文件大小限制

如果需要限制上传文件的大小也很简单,只需要在springboot 工程的src/main/resources/application.properties 加入以下:

1
2
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

  

架构代码如下 :

资料和源码来源地址

Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:一七九一七四三三八零

转载于:https://juejin.im/post/5c80dfddf265da2de661333d

相关文章:

  • bboss v5.5.3 发布,Elasticsearch Rest Client
  • 4.Git文件系统
  • ios监听键盘删除事件
  • 秒懂正则表达式
  • 怎么把GPU上训练的模型转到TPU或者CPU上去?DeepMind发布新工具支招
  • 互联网项目中mysql应该选什么事务隔离级别
  • 转载【阿里员工排查问题的工具清单,总有一款适合你】
  • 一、图书管理系统
  • 来自Google资深工程师的API设计最佳实践
  • grid布局基本概念
  • 论网站经营对一个企业的重要性
  • 持续交付基金会成立!Jenkins,Spinnaker等为首批捐赠项目
  • luogu P2634 [国家集训队]聪聪可可 点分治
  • link和@import的区别是什么 ?
  • 乞丐版的全栈实践
  • echarts花样作死的坑
  • ES10 特性的完整指南
  • Java,console输出实时的转向GUI textbox
  • java2019面试题北京
  • java多线程
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • mysql_config not found
  • Octave 入门
  • Redux 中间件分析
  • scrapy学习之路4(itemloder的使用)
  • Yeoman_Bower_Grunt
  • 讲清楚之javascript作用域
  • 每天一个设计模式之命令模式
  • 那些被忽略的 JavaScript 数组方法细节
  • 前端面试题总结
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​queue --- 一个同步的队列类​
  • #Z2294. 打印树的直径
  • $.ajax()参数及用法
  • (04)odoo视图操作
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (算法二)滑动窗口
  • (五)c52学习之旅-静态数码管
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (转)Linux整合apache和tomcat构建Web服务器
  • .apk文件,IIS不支持下载解决
  • .NET 4.0中的泛型协变和反变
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET 反射的使用
  • .net 验证控件和javaScript的冲突问题