转:SpringBoot实现MultipartFile文件上传 – 云+社区 – 腾讯云 (tencent.com)
1、SpringBoot采用FileUpload组件实现上传处理,在控制器中可以使用MultipartFile类进行接收。
1 <?xml version="1.0" encoding="UTF-8"?>
2 ="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5 https://maven.apache.org/xsd/maven-4.0.0.xsd">
6 >4.0.0</modelVersion>
7 >
8 >org.springframework.boot</groupId>
9 >spring-boot-starter-parent</artifactId>
10 >2.3.5.RELEASE</version>
11 /> <!-- lookup parent from repository -->
12 </parent>
13 >com.example</groupId>
14 >demo</artifactId>
15 >0.0.1-SNAPSHOT</version>
16 >demo</name>
17 >Demo project for Spring Boot</description>
18
19 >
20 .version>1.8</java.version>
21 -jar-plugin.version>3.1.1</maven-jar-plugin.version>
22 </properties>
23
24 >
25 >
26 >org.springframework.boot</groupId>
27 >spring-boot-starter-web</artifactId>
28 </dependency>
29
30 >
31 >org.springframework.boot</groupId>
32 >spring-boot-starter-test</artifactId>
33 >test</scope>
34 >
35 >
36 >org.junit.vintage</groupId>
37 >junit-vintage-engine</artifactId>
38 </exclusion>
39 </exclusions>
40 </dependency>
41 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
42 >
43 >org.hibernate</groupId>
44 >hibernate-validator</artifactId>
45 >6.1.0.Final</version>
46 </dependency>
47 >
48 >org.springframework.boot</groupId>
49 >spring-boot-starter-thymeleaf</artifactId>
50 </dependency>
51 </dependencies>
52
53 >
54 >
55 >
56 >org.springframework.boot</groupId>
57 >spring-boot-maven-plugin</artifactId>
58 </plugin>
59 </plugins>
60 >
61 >
62 >src/main/resources</directory>
63 >
64 >**
创建控制器类UploadController,利用MultipartFile将上传文件保存在本地磁盘。
1 package com.demo.controller;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.UUID;
8
9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestMethod;
12 import org.springframework.web.bind.annotation.RequestParam;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 import org.springframework.web.context.request.RequestAttributes;
15 import org.springframework.web.context.request.RequestContextHolder;
16 import org.springframework.web.context.request.ServletRequestAttributes;
17 import org.springframework.web.multipart.MultipartFile;
18
19
在src/main/resources/templates下面创建upload.html文件。
1 <!DOCTYPE html>
2 ="http://www.thymeleaf.org"></html>
3 >
4 ="UTF-8">
5 >搞事情啊!!!</title>
6 </head>
7 >
8 >搞事情啊!!!</h1>
9
10 :action="@{/upload}" method="post" enctype="multipart/form-data">
11 姓名:="text" name="name"/>/>
12 <!-- 切记,这里的name="multipartFile"必须和控制层的参数一致性MultipartFile multipartFile -->
13 照片:="file" name="multipartFile"/>/>
14 ="submit" value="上传"/>/>
15 </form>
16
17 </body>
18 </html>
控制层类UploadController接收到此请求信息后,如果有上传文件存在,则会直接返回上传信息(开发者也可以根据情况选择将文件保存)。
演示效果,如下所示:
2、Springboot上传文件限制,在实际项目开发中,需要对用户上传文件的大小进行限制,这样才可以保证服务器的资源不被浪费。修改application.properties配置文件,如下所示:
1 # 启用http上传
2 spring.servlet.multipart.enabled=true
3 # 设置支持的单个上传文件的大小限制
4 spring.servlet.multipart.max-file-size=10MB
5 # 设置最大的请求文件大小,设置总体大小请求
6 spring.servlet.multipart.max-request-size=20MB
7 # 当上传文件达到指定配置量的时候,将文件内容写入磁盘
8 spring.servlet.multipart.file-size-threshold=512KB
9 # 设置上传的临时目录
10 spring.servlet.multipart.location=/
对于Springboot上传文件的上传限制,也可以利用Bean实现同样的效果,实现代码,如下所示:
1 package com.demo.config;
2
3 import javax.servlet.MultipartConfigElement;
4
5 import org.springframework.boot.web.servlet.MultipartConfigFactory;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8 import org.springframework.util.unit.DataSize;
9
10 @Configuration
11 public class UploadConfig {
12
13 @Bean
14 public MultipartConfigElement getMultipartConfig() {
15 MultipartConfigFactory config = new MultipartConfigFactory();
16
3、Springboot上传多个文件如果要进行多个文件的上传,需要通过MultipartHttpServletRequest进行文件接收。首先修改upload.html页面,定义多个文件上传控件。
1 <!DOCTYPE html>
2 ="http://www.thymeleaf.org"></html>
3 >
4 ="UTF-8">
5 >搞事情啊!!!</title>
6 </head>
7 >
8 >搞事情啊!!!</h1>
9
10 :action="@{/upload}" method="post" enctype="multipart/form-data">
11 姓名:="text" name="name"/>/>
12 <!-- 切记,这里的name="multipartFile"必须和控制层的参数一致性MultipartFile multipartFile -->
13 文件一:="file" name="multipartFile"/>/>
14 文件二:="file" name="multipartFile"/>/>
15 文件三:="file" name="multipartFile"/>/>
16 ="submit" value="上传"/>/>
17 </form>
18
19 </body>
20 </html>
修改UploadController.java控制器,实现多个文件上传,此时可以根据接受参数的不同,方法可以有不同的实现 ,如果接受参数是这样的,@RequestParam(value = “multipartFile”) MultipartFile[] multipartFile,实现代码,如下所示:
1 package com.demo.controller;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.UUID;
8
9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestMethod;
12 import org.springframework.web.bind.annotation.RequestParam;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 import org.springframework.web.context.request.RequestAttributes;
15 import org.springframework.web.context.request.RequestContextHolder;
16 import org.springframework.web.context.request.ServletRequestAttributes;
17 import org.springframework.web.multipart.MultipartFile;
18
19
如果接受参数是HttpServletRequest request,代码是这样的,明显没有第一种方便,如下所示:
1 package com.demo.controller;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.UUID;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.springframework.stereotype.Controller;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.RequestParam;
18 import org.springframework.web.bind.annotation.ResponseBody;
19 import org.springframework.web.context.request.RequestAttributes;
20 import org.springframework.web.context.request.RequestContextHolder;
21 import org.springframework.web.context.request.ServletRequestAttributes;
22 import org.springframework.web.multipart.MultipartFile;
23 import org.springframework.web.multipart.MultipartHttpServletRequest;
24
25
Original: https://www.cnblogs.com/wangle1001986/p/15910763.html
Author: wangle100
Title: (转)SpringBoot实现MultipartFile文件上传
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/539636/
转载文章受原作者版权保护。转载请注明原作者出处!