mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-06-06 22:06:00 +08:00
m
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
# Spring Boot Examples
|
||||
|
||||
## [Spring Boot 精选达人课程,欢迎关注!](http://gitbook.cn/gitchat/column/59f5daa149cd4330613605ba)
|
||||
|
||||
Demo website:http://www.ityouknow.com/
|
||||
|
||||
[参与贡献](https://github.com/ityouknow/spring-boot-examples/issues) | [English](README_EN.md)
|
||||
@@ -23,8 +25,6 @@ Spring boot使用的各种示例,以最简单、最实用为标准
|
||||
- [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-multi-mongodb):spring boot和mongodb多数据源的使用
|
||||
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):spring-boot打包成war包示例
|
||||
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):springboot 整合shiro rbac示例
|
||||
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):springboot 集成上传文件示例
|
||||
- [spring-boot-fastDFS](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-fastDFS):springboot 集成FastDFS示例
|
||||
|
||||
**[Favorites-web](https://github.com/cloudfavorites/favorites-web):云收藏(springboot实战开源项目)**
|
||||
|
||||
@@ -51,7 +51,7 @@ Spring boot使用的各种示例,以最简单、最实用为标准
|
||||
|
||||
> 如果大家想了解关于springboot的其它方面应用,也可以以[issues](https://github.com/ityouknow/spring-boot-examples/issues)的形式反馈给我,我后续来完善。
|
||||
|
||||
|
||||
|
||||
关注公众号:纯洁的微笑,回复"springboot"进群交流
|
||||
|
||||

|
||||
|
||||
@@ -24,7 +24,5 @@ Spring Boot Examples, Use the simplest and most useful scene demo.
|
||||
- [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-multi-mongodb):Spring Boot + multiMongodb
|
||||
- [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war):Spring Boot package war
|
||||
- [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):spring boot shiro rbac demo
|
||||
- [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):Spring Boot file upload example
|
||||
- [spring-boot-fastDFS](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-fastDFS):springboot use FastDFS example
|
||||
|
||||
**[Favorites-web](https://github.com/cloudfavorites/favorites-web):Open source projects developed using Spring Boot**
|
||||
|
||||
Binary file not shown.
@@ -1,52 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.neo</groupId>
|
||||
<artifactId>spring-boot-fastDFS</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.7.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.csource</groupId>
|
||||
<artifactId>fastdfs-client-java</artifactId>
|
||||
<version>1.27-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.apache.coyote.http11.AbstractHttp11Protocol;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class FastDFSApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(FastDFSApplication.class, args);
|
||||
}
|
||||
|
||||
//Tomcat large file upload connection reset
|
||||
@Bean
|
||||
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
|
||||
|
||||
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
|
||||
|
||||
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
|
||||
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
|
||||
//-1 means unlimited
|
||||
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
|
||||
}
|
||||
});
|
||||
|
||||
return tomcat;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.neo.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Configurations {
|
||||
|
||||
@Value("${fastdfs.base.url}")
|
||||
private String fdfsUrl;
|
||||
|
||||
public String getFdfsUrl() {
|
||||
return fdfsUrl;
|
||||
}
|
||||
|
||||
public void setFdfsUrl(String fdfsUrl) {
|
||||
this.fdfsUrl = fdfsUrl;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
//https://jira.spring.io/browse/SPR-14651
|
||||
//4.3.5 supports RedirectAttributes redirectAttributes
|
||||
@ExceptionHandler(MultipartException.class)
|
||||
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
|
||||
|
||||
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
|
||||
return "redirect:/uploadStatus";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import com.neo.config.Configurations;
|
||||
import com.neo.fastdfs.FastDFSClient;
|
||||
import com.neo.fastdfs.FastDFSFile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Controller
|
||||
public class UploadController {
|
||||
private static Logger logger = LoggerFactory.getLogger(UploadController.class);
|
||||
@Autowired
|
||||
private Configurations configuration;
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "upload";
|
||||
}
|
||||
|
||||
@PostMapping("/upload") // //new annotation since 4.3
|
||||
public String singleFileUpload(@RequestParam("file") MultipartFile file,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (file.isEmpty()) {
|
||||
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
|
||||
return "redirect:uploadStatus";
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the file and save it somewhere
|
||||
String name=file.getOriginalFilename();
|
||||
String ext = name.substring(name.lastIndexOf(".") + 1);
|
||||
String path=saveFile(file,name,ext);
|
||||
redirectAttributes.addFlashAttribute("message",
|
||||
"You successfully uploaded '" + file.getOriginalFilename() + "'");
|
||||
|
||||
redirectAttributes.addFlashAttribute("path",
|
||||
"file path url '" + path + "'");
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("upload file failed",e);
|
||||
}
|
||||
|
||||
return "redirect:/uploadStatus";
|
||||
}
|
||||
|
||||
@GetMapping("/uploadStatus")
|
||||
public String uploadStatus() {
|
||||
return "uploadStatus";
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String saveFile(MultipartFile multipartFile, String fileName, String ext){
|
||||
String path="";
|
||||
String[] fileAbsolutePath={};
|
||||
try {
|
||||
byte[] file_buff = null;
|
||||
InputStream inputStream=multipartFile.getInputStream();
|
||||
if(inputStream!=null){
|
||||
int len1 = inputStream.available();
|
||||
file_buff = new byte[len1];
|
||||
inputStream.read(file_buff);
|
||||
}
|
||||
inputStream.close();
|
||||
FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
|
||||
try {
|
||||
fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if (fileAbsolutePath==null) {
|
||||
System.out.println("upload file failed,please upload again!");
|
||||
}
|
||||
path=configuration.getFdfsUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package com.neo.fastdfs;
|
||||
|
||||
import org.csource.common.NameValuePair;
|
||||
import org.csource.fastdfs.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
|
||||
public class FastDFSClient {
|
||||
private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
|
||||
private static TrackerClient trackerClient;
|
||||
private static TrackerServer trackerServer;
|
||||
private static StorageClient storageClient;
|
||||
private static StorageServer storageServer;
|
||||
|
||||
static {
|
||||
try {
|
||||
Resource resource = new ClassPathResource("fdfs_client.conf");
|
||||
File file = resource.getFile();
|
||||
String configFile = file.getAbsolutePath();
|
||||
|
||||
ClientGlobal.init(configFile);
|
||||
trackerClient = new TrackerClient();
|
||||
trackerServer = trackerClient.getConnection();
|
||||
storageServer = trackerClient.getStoreStorage(trackerServer);
|
||||
storageClient = new StorageClient(trackerServer, storageServer);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("FastDFS Client Init Fail!");
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] upload(FastDFSFile file) {
|
||||
logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);
|
||||
|
||||
NameValuePair[] meta_list = new NameValuePair[1];
|
||||
meta_list[0] = new NameValuePair("author", file.getAuthor());
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
String[] uploadResults = null;
|
||||
try {
|
||||
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
|
||||
} catch (IOException e) {
|
||||
logger.error("IO Exception when uploadind the file:" + file.getName(), e);
|
||||
} catch (Exception e) {
|
||||
logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);
|
||||
}
|
||||
logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");
|
||||
|
||||
if (uploadResults == null) {
|
||||
logger.error("upload file fail, error code:" + storageClient.getErrorCode());
|
||||
}
|
||||
String groupName = uploadResults[0];
|
||||
String remoteFileName = uploadResults[1];
|
||||
|
||||
logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);
|
||||
System.out.println("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);
|
||||
|
||||
return uploadResults;
|
||||
}
|
||||
|
||||
public static FileInfo getFile(String groupName, String remoteFileName) {
|
||||
try {
|
||||
return storageClient.get_file_info(groupName, remoteFileName);
|
||||
} catch (IOException e) {
|
||||
logger.error("IO Exception: Get File from Fast DFS failed", e);
|
||||
} catch (Exception e) {
|
||||
logger.error("Non IO Exception: Get File from Fast DFS failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static InputStream downFile(String groupName, String remoteFileName) {
|
||||
try {
|
||||
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
|
||||
InputStream ins = new ByteArrayInputStream(fileByte);
|
||||
return ins;
|
||||
} catch (IOException e) {
|
||||
logger.error("IO Exception: Get File from Fast DFS failed", e);
|
||||
} catch (Exception e) {
|
||||
logger.error("Non IO Exception: Get File from Fast DFS failed", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void deleteFile(String groupName, String remoteFileName)
|
||||
throws Exception {
|
||||
int i = storageClient.delete_file(groupName, remoteFileName);
|
||||
System.out.println("delete file successfully!!!" + i);
|
||||
}
|
||||
|
||||
public static StorageServer[] getStoreStorages(String groupName)
|
||||
throws IOException {
|
||||
return trackerClient.getStoreStorages(trackerServer, groupName);
|
||||
}
|
||||
|
||||
public static ServerInfo[] getFetchStorages(String groupName,
|
||||
String remoteFileName) throws IOException {
|
||||
return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.neo.fastdfs;
|
||||
|
||||
public class FastDFSFile {
|
||||
private String name;
|
||||
|
||||
private byte[] content;
|
||||
|
||||
private String ext;
|
||||
|
||||
private String md5;
|
||||
|
||||
private String author;
|
||||
|
||||
public FastDFSFile(String name, byte[] content, String ext, String height,
|
||||
String width, String author) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
this.ext = ext;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public FastDFSFile(String name, byte[] content, String ext) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
this.ext = ext;
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(byte[] content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getExt() {
|
||||
return ext;
|
||||
}
|
||||
|
||||
public void setExt(String ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public String getMd5() {
|
||||
return md5;
|
||||
}
|
||||
|
||||
public void setMd5(String md5) {
|
||||
this.md5 = md5;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
|
||||
#search multipart
|
||||
spring.http.multipart.max-file-size=10MB
|
||||
spring.http.multipart.max-request-size=10MB
|
||||
|
||||
fastdfs.base.url=http://192.168.53.85:8080/
|
||||
@@ -1,9 +0,0 @@
|
||||
connect_timeout = 2
|
||||
network_timeout = 30
|
||||
charset = UTF-8
|
||||
http.tracker_http_port = 8080
|
||||
http.anti_steal_token = no
|
||||
http.secret_key = 123456
|
||||
|
||||
tracker_server = 192.168.53.85:22122
|
||||
tracker_server = 192.168.53.86:22122
|
||||
@@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.web" level="error" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
|
||||
<logger name="com.neo" level="debug" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -1,13 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<h1>Spring Boot file upload example</h1>
|
||||
|
||||
<form method="POST" action="/upload" enctype="multipart/form-data">
|
||||
<input type="file" name="file" /><br/><br/>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,16 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<h1>Spring Boot - Upload Status</h1>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
</div>
|
||||
|
||||
<div th:if="${path}">
|
||||
<h2 th:text="${path}"/>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,48 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.neo</groupId>
|
||||
<artifactId>spring-boot-file-upload</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.7.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.neo;
|
||||
|
||||
import org.apache.coyote.http11.AbstractHttp11Protocol;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class FileUploadWebApplication {
|
||||
|
||||
private int maxUploadSizeInMb = 10 * 1024 * 1024; // 10 MB
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(FileUploadWebApplication.class, args);
|
||||
}
|
||||
|
||||
//Tomcat large file upload connection reset
|
||||
@Bean
|
||||
public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
|
||||
|
||||
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
|
||||
|
||||
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
|
||||
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
|
||||
//-1 means unlimited
|
||||
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
|
||||
}
|
||||
});
|
||||
|
||||
return tomcat;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
//https://jira.spring.io/browse/SPR-14651
|
||||
//4.3.5 supports RedirectAttributes redirectAttributes
|
||||
@ExceptionHandler(MultipartException.class)
|
||||
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
|
||||
|
||||
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
|
||||
return "redirect:/uploadStatus";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.neo.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Controller
|
||||
public class UploadController {
|
||||
|
||||
//Save the uploaded file to this folder
|
||||
private static String UPLOADED_FOLDER = "E://temp//";
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "upload";
|
||||
}
|
||||
|
||||
@PostMapping("/upload") // //new annotation since 4.3
|
||||
public String singleFileUpload(@RequestParam("file") MultipartFile file,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
|
||||
if (file.isEmpty()) {
|
||||
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
|
||||
return "redirect:uploadStatus";
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// Get the file and save it somewhere
|
||||
byte[] bytes = file.getBytes();
|
||||
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
|
||||
Files.write(path, bytes);
|
||||
|
||||
redirectAttributes.addFlashAttribute("message",
|
||||
"You successfully uploaded '" + file.getOriginalFilename() + "'");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "redirect:/uploadStatus";
|
||||
}
|
||||
|
||||
@GetMapping("/uploadStatus")
|
||||
public String uploadStatus() {
|
||||
return "uploadStatus";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
#http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
|
||||
#search multipart
|
||||
spring.http.multipart.max-file-size=2MB
|
||||
spring.http.multipart.max-request-size=10MB
|
||||
@@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.web" level="error" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
|
||||
<logger name="com.neo" level="debug" additivity="false">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -1,13 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<h1>Spring Boot file upload example</h1>
|
||||
|
||||
<form method="POST" action="/upload" enctype="multipart/form-data">
|
||||
<input type="file" name="file" /><br/><br/>
|
||||
<input type="submit" value="Submit" />
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,12 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<h1>Spring Boot - Upload Status</h1>
|
||||
|
||||
<div th:if="${message}">
|
||||
<h2 th:text="${message}"/>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user