接收上传的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
|
<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
|
转载于:https://www.cnblogs.com/MaxElephant/p/10231949.html
接收上传的multi-file的文件(四)相关推荐
- php dw文件上传下载,使用PHP实现文件上传
这里使用PHP实现文件的上传,由在浏览器这边选择文件,上传到服务器.其中,在上传文件中,考虑到对上传文件大小的限制.类型限制等问题.(当然可以根据我们需要修改对上传的文件的限制,而一般而言php自身也 ...
- 电脑无法上传html文件类型,HTML5 - 限制文件上传(HTML5 - Restrict file upload)
HTML5 - 限制文件上传(HTML5 - Restrict file upload) 上面的代码也允许上传.txt或任何类型的文件. 如何限制 html5中的其他文件类型 ? Above code ...
- 文件上传至将File转换成MultiPartFile
在文件上传业务上需要将文件上传至将File转换成MultiPartFile的时候,我搜索得到采用MockMultipartFile这个类可以轻松的转换. 但是,当我准备使用的时候,坑出现了.它是spr ...
- Laravel 文件上传提示 “The file deos not exits ” ,但确实已经上传了文件
Laravel 文件上传提示 "The file "" deos not exits " ,但确实已经上传了文件 首先使用代码抛出异常 if($file-> ...
- 大文件上传服务器:支持超大文件HTTP断点续传的实现办法
点击上方蓝色"方志朋",选择"设为星标"回复"666"获取独家整理的学习资料! 来源:blog.csdn.net/ababab12345/a ...
- 文件上传~Uploadify上传控件~续(多文件上传)
对于Uploadify文件上传之前已经讲过一次(文件上传~Uploadify上传控件),只不过没有涉及到多文件的上传,这回主要说一下多个文件的上传,首先,我们要清楚一个概念,多文件上传前端Upload ...
- 大文件上传服务器、支持超大文件HTTP断点续传实践总结
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/ababab12345/ article/details/80490621 最近由于笔者所在的研发集团产品需要,需要支持高性能的 ...
- 微信小程序上传聊天窗中的文件
懒得删删减减了,全部拷过来.效果如下, 1.点击虚线框选中文件夹并且切换样式,展示文件名字, 2.选中后上传扭变色,然后点上传上传接口 1.html <!--推荐加盟--> <ima ...
- java web 文件上传_Javaweb学习笔记10—文件上传与下载
今天来讲javaweb的第10阶段学习.文件的上传与下载,今天主要说的是这个功能的实现,不用说了,听名字就是外行人也知道肯定很重要啦. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思 ...
- canvas生成图片上传服务器,网络图片生成图片文件
vue+canvas 生成签名图片并保存数据库 VUE+ElementUI 最近根据项目需求 在用户生成订单的时候需要附上自己的签名照片,因为考虑到不能上传图片所以使用canvas画图功能. 而目前后 ...
最新文章
- Java——线程安全的集合
- 使用shell脚本实现在liunx上进行svn的上传下载更新功能
- 解析Visual C# 7.2中的private protected访问修饰符
- android dropbox anr分析,Android如何分析排查ANR
- 关于Python局部变量和全局变量必须知道的几句话
- 【论文阅读】EMNLP 2018 基于自适应的多轮解码机制的神经机器翻译模型
- JAVA-幂等性(一):http幂等性
- 两种办法解决外网资源下载速度过慢的问题
- 怎样写毕业论文的开题报告和任务书?
- JavaScript学习手册一
- 2019Deecamp面试经验分享
- 信息安全常见名词解释
- s5p6818 Smart6818 nanopi3开发板,uboot启动linux,配置nfs文件系统,设置bootargs
- 实习僧招聘网站信息采集
- Windows一键清空回收站
- 【BZOJ3831】[Poi2014]Little Bird 单调队列
- HTML5期末作业:明星网站设计与实现——明星薛之谦介绍网页设计7个页面HTML+CSS+JavaScript
- android 距离右边距,APP界面设计中间距与边距的要点
- 腾讯云服务器TencentOS镜像操作系统评测及常见问题解答
- jmeter基础(二)-JMeter证书