接收上传的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 |
接收上传的multi-file的文件(四)的更多相关文章
- 图片上传怎么用File接受文件
xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! - ...
- uni-app开发的应用(小程序,app,web等),使用Node+Koa2开发的后端程序接收上传文件的方法
uni-app使用使用Node+Koa2开发的后端程序接收上传的文件 通过gitbook浏览此随笔 通过其它客户端上传(h5,小程序等),接收方法一致 使用koa接收时,我们需安装一个中间件koa-b ...
- asp.net 一般处理程序接收上传文件的问题
在使用Html+ashx处理文件上传时,遇到上传文件超过4M的问题,首先HTML代码如下: <!DOCTYPE html> <html> <head> <me ...
- Spring Boot 在接收上传文件时,文件过大异常处理问题
Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...
- 表单多文件上传样式美化 && 支持选中文件后删除相关项
开发中会经常涉及到文件上传的需求,根据业务不同的需求,有不同的文件上传情况. 有简单的单文件上传,有多文件上传,因浏览器原生的文件上传样式及功能的支持度不算太高,很多时候我们会对样式进行美化,对功能进 ...
- 文件上传~Uploadify上传控件~续(多文件上传)
对于Uploadify文件上传之前已经讲过一次(文件上传~Uploadify上传控件),只不过没有涉及到多文件的上传,这回主要说一下多个文件的上传,首先,我们要清楚一个概念,多文件上传前端Upload ...
- ASP.NET Core WEB API 使用element-ui文件上传组件el-upload执行手动文件文件,并在文件上传后清空文件
前言: 从开始学习Vue到使用element-ui-admin已经有将近快两年的时间了,在之前的开发中使用element-ui上传组件el-upload都是直接使用文件选取后立即选择上传,今天刚好做了 ...
- 前端上传视频、图片、文件等大文件 组件Plupload使用指南
demo:https://blog.csdn.net/qq_30100043/article/details/78491993 Plupload上传插件中文帮助文档网址:http://www.phpi ...
- struts文件上传,获取文件名和文件类型
struts文件上传,获取文件名和文件类型 Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...
- 通过winForm控制webForm的上传控件file的值
文件上传是日常开发中经常遇到的,文件上传用的最多的当然是上传控件file了,一个form表单,其中有一点就是form表单的enctype属性设置为multipart/form-data,呵呵,这个在所 ...
随机推荐
- 使用lombok 找不到方法
在setting里面查找并设置就好了
- 重装win7系统并激活
备份 大白菜制作启动盘 下载大白菜软件UEFI版(新电脑使用uefi版本,装机版支持的主板多) 选择默认安装 选择默认模式开始制作 下载iso镜像文件,复制到u盘(手动复制) 设置bios ...
- JqueryValidate 表单验证插件
1.适用场景 表单 ( 支持自定义规则 ) 2.相关文章 jQuery Validate 3.实际问题 JqueryValidate表单相同Name不校验问题解决
- 从flask视角理解angular(三)ORM VS Service
把获取模型数据的任务重构为一个单独的服务,它将提供英雄数据,并把服务在所有需要英雄数据的组件间共享. @Injectable() export class HeroService { getHeroe ...
- 网络安装OS(配置文件)
●无人值守安装KS文件(redhat用,删除下面中文). #LinuxIParm Config File #Generated by LinuxIParm Configurator #Boot Mod ...
- java web 方面
1.Tomcat的优化经验. 2.http请求的GET与POST方式的区别. (1)get是从服务器上获取数据,post是向服务器传送数据. (2)get是把参数数据队列加到提交表单的ACTION属性 ...
- NGUI实现的一套不同大小 Item 的循环滚动代码
测试: 数据 & Item 的 Ctrl : using UnityEngine; public class ScrollViewItemData { public int index; p ...
- MySQL中如何建立主从复制
## 1 概述 ## MySQL内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves) ...
- Go语言学习之3 流程控制、函数
主要内容: 1. strings和strconv使用2. Go中的时间和日期类型3. 指针类型4. 流程控制5. 函数详解 1. strings和strconv使用 //strings . strin ...
- Windows的空格预览神器 | QuickLook
用惯了Mac,发现空格预览真的能极大的提升效率和手感. QuickLook,Windows版的空格预览神器,效果差一些,速度有点慢,勉强用吧.