SpringBoot --web Application Development File Upload

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 compatible jspsolutions projects. Since it began to explain web development, and we will then continue towards webthis direction continue it. In general, we are doing webthe 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 today SpringBootunder 文件上传implementation functions.

A little knowledge

We know that when the front end of uploading files, the most common is the use of Forma form of fashion uploading files, and set upload enctypeto multipart/form-data, or directly use jquerysome upload plug-in implementation. The back-end, at jsp+Servlettimes, the most common is to use smartuploadupload component. In the SpringMvcera of which provides MultipartFilepackage type, the binary stream that contains files and associated attributes (file name, size, etc.). Therefore, SpringBootalso in this class package file upload.

File Upload

When it comes to the above, springmvcit is use MultipartFilefor file upload. And MultipartFileis the interface class, its implementation class has CommonsMultipartFileand StandardMultipartFile. Here a brief description follows:


StandardMultipartFile : is based on the j2eeuploaded file parsing comes, that the use Servlet3.0provided javax.servlet.http.Partuploads.

CommonsMultipartFile : is based on apache fileuploadthe resolution.


So when we normally use, but also the underlying relationship is which way do not need to file upload process, SpringMvcwill 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
Other configurations

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 postmanapi calls can be carried out, of course, we can also use the template engine technology on learning the lesson, write a file upload form.

J2EE way

FileUpload package based on the way

In the original Springmvc, we usually use CommonsMultipartResolverfor file upload handler class configuration, such as maximum upload file size. And based on the SpringBootlower, slightly different.

0.pom join fileUploaddependence

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.enableddefault true, the loaded class MultipartAutoConfigurationwill 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.enabledto false.
For this example, two kinds of switches can be used for a second.

3. Write control layer: and this J2EEway is the same, there is not posted.
4. Start the application, using the following postman visit:

fileupload

Relevant information

  1. https://docs.spring.io/spring-boot/docs/1.5.15.RELEASE/reference/htmlsingle/

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 MultipartExceptionan exception to, here is an example of a relative it is rather simple.

At last

Currently on the Internet has a lot of heavyweights SpringBootseries 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

Guess you like

Origin www.cnblogs.com/jpfss/p/10967861.html