Foreword
The previous section, we explain the use of
模版引擎
realization of the front page rendering to perform the functions of dynamic web pages, but also made compatiblejsp
solutions projects. Since it began to explain web development, and we will then continue towardsweb
this direction continue it. In general, we are doingweb
the development, met such as attachment upload, upload picture upload pictures and other documents demand is also perfectly normal. So, we have to say something about us todaySpringBoot
under文件上传
implementation functions.
A little knowledge
We know that when the front end of uploading files, the most common is the use of Form
a form of fashion uploading files, and set upload enctype
to multipart/form-data
, or directly use jquery
some upload plug-in implementation. The back-end, at jsp+Servlet
times, the most common is to use smartupload
upload component. In the SpringMvc
era of which provides MultipartFile
package type, the binary stream that contains files and associated attributes (file name, size, etc.). Therefore, SpringBoot
also in this class package file upload.
File Upload
When it comes to the above,
springmvc
it is useMultipartFile
for file upload. AndMultipartFile
is the interface class, its implementation class hasCommonsMultipartFile
andStandardMultipartFile
. Here a brief description follows:
StandardMultipartFile : is based on the j2ee
uploaded file parsing comes, that the use Servlet3.0
provided javax.servlet.http.Part
uploads.
CommonsMultipartFile : is based on apache fileupload
the resolution.
So when we normally use, but also the underlying relationship is which way do not need to file upload process, SpringMvc
will we do the appropriate conversion.
Demonstration here in two ways.
Based on J2EE own way
In this way, in fact, without any configuration. Just normal web development project can be integrated.
0.pom dependence
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
|
1. The configuration file (optional), normally only need to set the file size to upload
1
2
3
4
|
# 最大支持文件大小 即单个文件大小
spring.http.multipart.max-file-size=1Mb
# 最大支持请求大小 即一次性上传的总文件大小
spring.http.multipart.max-request-size=10Mb
|
Configuration information of other configurations can be directly refer to the official website: https://docs.spring.io/spring-boot/docs/1.5.15.RELEASE/reference/htmlsingle/#common-application-properties
2. Write control class
FileUploadController.java:
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
|
@RestController
@Slf4j
public
class
FileUploadController {
@PostMapping
(
"/upload"
)
public
String upload(
@RequestParam
MultipartFile file)
throws
IllegalStateException, IOException {
// 判断是否为空文件
if
(file.isEmpty()) {
return
"上传文件不能为空"
;
}
// 文件类型
String contentType = file.getContentType();
// springmvc处理后的文件名
String fileName = file.getName();
log.info(
"服务器文件名:"
+ fileName);
// 原文件名即上传的文件名
String origFileName = file.getOriginalFilename();
// 文件大小
Long fileSize = file.getSize();
// 保存文件
// 可以使用二进制流直接保存
// 这里直接使用transferTo
file.transferTo(
new
File(
"d://okong-"
+ origFileName));
return
String.format(file.getClass().getName() +
"方式文件上传成功!\n文件名:%s,文件类型:%s,文件大小:%s"
, origFileName, contentType,fileSize);
}
}
|
3. Start the application, and then use the postman
api calls can be carried out, of course, we can also use the template engine technology on learning the lesson, write a file upload form.
FileUpload package based on the way
In the original
Springmvc
, we usually useCommonsMultipartResolver
for file upload handler class configuration, such as maximum upload file size. And based on theSpringBoot
lower, slightly different.
0.pom join fileUpload
dependence
1
2
3
4
5
|
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>
1.3
.
3
</version>
</dependency>
|
1. Preparation of a configuration class
FileUploadConfig.java
1
2
3
4
5
6
7
8
|
@Configuration
public
class
FileUploadConfig {
@Bean
public
MultipartResolver custom() {
return
new
CommonsMultipartResolver();
}
}
|
2. As spring.http.multipart.enabled
default true
, the loaded class MultipartAutoConfiguration
will automatically load, so it will fileUpload anomaly. Therefore, there are two ways Normal: In this configuration classes class does not start the automatic loading
1
|
@EnableAutoConfiguration
(exclude = {MultipartAutoConfiguration.
class
})
|
Another is, directly spring.http.multipart.enabled
to false
.
For this example, two kinds of switches can be used for a second.
3. Write control layer: and this J2EE
way is the same, there is not posted.
4. Start the application, using the following postman visit:
Relevant information
to sum up
This chapter is mainly explained two ways to upload files. Here a brief introduction of the two ways of configuration and few notes, were like when the file size is too large to catch the exception, you can use the global exception class to capture, capture only
MultipartException
an exception to, here is an example of a relative it is rather simple.
At last
Currently on the Internet has a lot of heavyweights
SpringBoot
series of tutorials, any similarity, please forgive me up. This paper is a single word in front of the computer knock, every step is practice. If something incorrect in the text, but also look made, thank you.
Original Address: http: //www.importnew.com/29795.html