Skip to content

Commit 03cb1f2

Browse files
committed
spring-boot-file-upload
1 parent fef36e2 commit 03cb1f2

File tree

10 files changed

+219
-1
lines changed

10 files changed

+219
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Spring boot使用的各种示例,以最简单、最实用为标准
2323
- [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-multi-mongodb):spring boot和mongodb多数据源的使用
2424
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):spring-boot打包成war包示例
2525
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):springboot 整合shiro rbac示例
26+
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):springboot 集成上传文件示例
2627

2728
**[Favorites-web](https://github.com/cloudfavorites/favorites-web):云收藏(springboot实战开源项目)**
2829

README_EN.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ Spring Boot Examples, Use the simplest and most useful scene demo.
2424
- [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-multi-mongodb):Spring Boot + multiMongodb
2525
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):Spring Boot package war
2626
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):spring boot shiro rbac demo
27-
- [Favorites-web](https://github.com/cloudfavorites/favorites-web):Open source projects developed using Spring Boot
27+
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):Spring Boot file upload example
28+
29+
30+
**[Favorites-web](https://github.com/cloudfavorites/favorites-web):Open source projects developed using Spring Boot**

spring-boot-file-upload/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.neo</groupId>
8+
<artifactId>spring-boot-file-upload</artifactId>
9+
<packaging>jar</packaging>
10+
<version>1.0</version>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.7.RELEASE</version>
16+
</parent>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-devtools</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.neo;
2+
3+
import org.apache.coyote.http11.AbstractHttp11Protocol;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
7+
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
8+
import org.springframework.context.annotation.Bean;
9+
10+
@SpringBootApplication
11+
public class FileUploadWebApplication {
12+
13+
private int maxUploadSizeInMb = 10 * 1024 * 1024; // 10 MB
14+
15+
public static void main(String[] args) throws Exception {
16+
SpringApplication.run(FileUploadWebApplication.class, args);
17+
}
18+
19+
//Tomcat large file upload connection reset
20+
@Bean
21+
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
22+
23+
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
24+
25+
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
26+
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
27+
//-1 means unlimited
28+
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
29+
}
30+
});
31+
32+
return tomcat;
33+
34+
}
35+
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.neo.controller;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.multipart.MultipartException;
6+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
7+
8+
@ControllerAdvice
9+
public class GlobalExceptionHandler {
10+
11+
//https://jira.spring.io/browse/SPR-14651
12+
//4.3.5 supports RedirectAttributes redirectAttributes
13+
@ExceptionHandler(MultipartException.class)
14+
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
15+
16+
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
17+
return "redirect:/uploadStatus";
18+
19+
}
20+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.neo.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.multipart.MultipartFile;
8+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
9+
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
15+
@Controller
16+
public class UploadController {
17+
18+
//Save the uploaded file to this folder
19+
private static String UPLOADED_FOLDER = "E://temp//";
20+
21+
@GetMapping("/")
22+
public String index() {
23+
return "upload";
24+
}
25+
26+
@PostMapping("/upload") // //new annotation since 4.3
27+
public String singleFileUpload(@RequestParam("file") MultipartFile file,
28+
RedirectAttributes redirectAttributes) {
29+
30+
if (file.isEmpty()) {
31+
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
32+
return "redirect:uploadStatus";
33+
}
34+
35+
try {
36+
37+
// Get the file and save it somewhere
38+
byte[] bytes = file.getBytes();
39+
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
40+
Files.write(path, bytes);
41+
42+
redirectAttributes.addFlashAttribute("message",
43+
"You successfully uploaded '" + file.getOriginalFilename() + "'");
44+
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
49+
return "redirect:/uploadStatus";
50+
}
51+
52+
@GetMapping("/uploadStatus")
53+
public String uploadStatus() {
54+
return "uploadStatus";
55+
}
56+
57+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
2+
#search multipart
3+
spring.http.multipart.max-file-size=2MB
4+
spring.http.multipart.max-request-size=10MB
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<layout class="ch.qos.logback.classic.PatternLayout">
6+
<Pattern>
7+
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
8+
</Pattern>
9+
</layout>
10+
</appender>
11+
12+
<logger name="org.springframework.web" level="error" additivity="false">
13+
<appender-ref ref="STDOUT"/>
14+
</logger>
15+
16+
<logger name="com.neo" level="debug" additivity="false">
17+
<appender-ref ref="STDOUT"/>
18+
</logger>
19+
20+
<root level="error">
21+
<appender-ref ref="STDOUT"/>
22+
</root>
23+
24+
</configuration>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<body>
4+
5+
<h1>Spring Boot file upload example</h1>
6+
7+
<form method="POST" action="/upload" enctype="multipart/form-data">
8+
<input type="file" name="file" /><br/><br/>
9+
<input type="submit" value="Submit" />
10+
</form>
11+
12+
</body>
13+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<body>
4+
5+
<h1>Spring Boot - Upload Status</h1>
6+
7+
<div th:if="${message}">
8+
<h2 th:text="${message}"/>
9+
</div>
10+
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)