一:用idea 创建 springboot 项目:
详情请参考:《使用IDEA创建一个springboot项目》
二:具体代码内容:
1:代码结构
FileUploadController
package com.alancode.springboot.controller; /** * @author Alan_liu * @Create 2021/2/16 15:51 */ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * @program: SpringBootAddFileUpload *@description: SpringBoot 文件上传 *@author: Alan_Liu *@create: 2021-02-16 15:51 */ @RestController /***使用该注解:表示该使用的java类下的所有的方法返回值都默认做jason转换;其作用相当于@Controller+@ResponseBody ****/ public class FileUploadController { /** *处理附件上传 * *@Param: ` *@return *@throws *@author Alan_Liu *@date 2021/2/16 15:56 * **/ @RequestMapping("/fileUploadController") public Map<String,Object> fileUpload(MultipartFile filename) throws IOException { System.out.println(filename.getOriginalFilename()); filename.transferTo(new File("F:/SoftCode/Idea202103_WorkSpace/SpringBootAddFileUpload/src/main/resources/static/UpLoadFile/"+filename.getOriginalFilename())); Map<String,Object> map=new HashMap<String,Object>(); map.put("msg","ok"); return map; } }
DemoApplication
package com.alancode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.util.unit.DataSize; import javax.servlet.MultipartConfigElement; /** * @author Alan_Liu */ @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } //@Configuration @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize(DataSize.parse("1024MB")); //KB,MB //设置总上传数据总大小 factory.setMaxRequestSize(DataSize.parse("1024MB")); return factory.createMultipartConfig(); } }
ServletInitializer
package com.alancode.springboot; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } }
Upload.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>springboot 文件上传</title> </head> <body> <form action="fileUploadController" method="post" enctype="multipart/form-data" value="107341824000">> 上传文件:<input type="file" name="filename" /><input type="submit"> </form> </body> </html>
application.properties
spring.http.multipart.maxFileSize = -1 spring.http.multipart.maxRequestSize=-1
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alanCode.springBoot</groupId> <artifactId>SpringBootAddFileUpload</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo</name> <description>springboot 上传附件</description> <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-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动项目:
上传附件:
上传附件路径: