实践环境 1
2
3
4
5
6
7
8
9
10
11
12
IDE
Intellij IDEA[ 优点:自动安装新加依赖库]
JAVA
java version "1.8.0_121"
Java( TM) SE Runtime Environment ( build 1.8.0_121-b13)
Java HotSpot( TM) 64-Bit Server VM ( build 25.121-b13, mixed mode)
Copy 实践项目初始化 我们以之前的项目为例,
地址:
1
2
3
4
https://github.com/durban89/spring-demo.git
tag: v1.0.1
Copy Maven配置 pom.xml添加下面的依赖库,主要是后面的页面以thymeleaf作为模板
1
2
3
4
5
6
7
8
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-thymeleaf</artifactId>
</dependency>
Copy 创建StorageService与存储层进行交互,如"文件系统" src/main/java/com/gowhich/springdemo/StorageService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.gowhich.springdemo.storage ;
import org.springframework.core.io.Resource ;
import org.springframework.web.multipart.MultipartFile ;
import java.nio.file.Path ;
import java.util.stream.Stream ;
public interface StorageService {
void init ();
void store ( MultipartFile file );
Stream < Path > loadAll ();
Path load ( String filename );
Resource loadAsResource ( String filename );
void deleteAll ();
}
Copy ***创建了存储层就要实现对接接口方法的实现,具体的可以拉取项目看下
创建上传文件的控制器 src/main/java/com/gowhich/springdemo/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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.gowhich.springdemo ;
import com.gowhich.springdemo.storage.StorageFileNotFoundException ;
import com.gowhich.springdemo.storage.StorageService ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.ResponseEntity ;
import org.springframework.stereotype.Controller ;
import org.springframework.ui.Model ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.multipart.MultipartFile ;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder ;
import org.springframework.core.io.Resource ;
import org.springframework.web.servlet.mvc.support.RedirectAttributes ;
import java.io.IOException ;
import java.util.stream.Collectors ;
@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 resource = storageService . loadAsResource ( filename );
return ResponseEntity . ok (). header ( HttpHeaders . CONTENT_DISPOSITION ,
"attachment; filename=\"" + resource . getFilename () + "\"" ). body ( resource );
}
@PostMapping ( "/" )
public String handleFileUpload ( @RequestParam ( "file" ) MultipartFile file , RedirectAttributes redirectAttributes ) {
storageService . store ( file );
redirectAttributes . addFlashAttribute ( "message" , "成功上传了文件 " + file . getOriginalFilename () + "!" );
return "redirect:/" ;
}
@ExceptionHandler ({ StorageFileNotFoundException . class })
public ResponseEntity <?> handleStorageFileNotFound ( StorageFileNotFoundException ex ) {
return ResponseEntity . notFound (). build ();
}
}
Copy 创建一个HTML模板 src/main/java/com/gowhich/springdemo/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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
< html lang = "en" xmlns:th = "http://www.thymeleaf.org" >
< head >
< meta charset = "UTF-8" >
< title > UploadForm</ title >
</ head >
< 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 = "上传" />
</ td >
</ tr >
</ table >
</ form >
</ div >
< div >
< ul >
< li th:each = "file: ${files}" >
< a th:href = "${file}" th:text = "${file}" />
</ li >
</ ul >
</ div >
</ body >
</ html >
Copy 调整文件上传限制 src/main/resources/application.properties中添加
1
2
3
4
5
6
7
8
spring.servlet.multipart.max-file-size= 128KB
spring.servlet.multipart.max-request-size= 128KB
spring.servlet.multipart.enabled= true
spring.http.encoding.charset= UTF-8
Copy Application配置 src/main/java/com/gowhich/springdemo/SpringDemoApplication.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
package com.gowhich.springdemo ;
import com.gowhich.springdemo.storage.StorageProperties ;
import com.gowhich.springdemo.storage.StorageService ;
import org.springframework.boot.CommandLineRunner ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.context.annotation.Bean ;
@SpringBootApplication
@EnableConfigurationProperties ( StorageProperties . class )
public class SpringDemoApplication {
public static void main ( String [] args ) {
SpringApplication . run ( SpringDemoApplication . class , args );
}
@Bean
CommandLineRunner init ( StorageService storageService ) {
return ( args ) -> {
storageService . deleteAll ();
storageService . init ();
};
}
}
Copy 启动项目,我在Intellij IDEA直接可以操作启动,很方便
项目地址
1
2
3
4
https://github.com/durban89/spring-demo.git
tag: v1.0.2
Copy