依赖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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot</artifactId> <groupId>com.baizhi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springbootfile</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.13.RELEASE</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.31</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.1.13.RELEASE</version> </dependency> </dependencies> <build> <plugins> <!--启动插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.3.RELEASE</version> <configuration> <fork>true</fork> <jvmArguments>-Dfile.encoding=utf-8</jvmArguments> </configuration> </plugin> </plugins> </build> </project>
前端页面file.jsp
<html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <body> <!--文件上传的form表单必须限制属性method="post" enctype="multipart/form-data"--> <form action="/springbootfile/file/upload" method="post" enctype="multipart/form-data"> file: <input type="file" name="pic" > <br> <input type="submit" value="提交"> </form> </body> </html>
application.yml(配置)
server:
port: 8989
servlet:
context-path: /springbootfile
spring:
mvc:
view:
prefix: /
suffix: .jsp
servlet:
multipart:
max-file-size: 20MB #单个文件上传最大20mb
max-request-size: 50MB #一次请求最大传输50mb
FileController
package com.baihzhi.controller; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * @Package: com.baihzhi.controller * @ClassName: FileController * @Author: alex * @CreateTime: 2020/11/9 11:18 * @Description: */ @Controller @RequestMapping("/file") public class FileController { @RequestMapping("/upload") public String upload(MultipartFile pic, HttpServletRequest request) throws IOException { String realPath = request.getServletContext().getRealPath("/"); System.out.println("realPath = " + realPath); File absoluteFile = new File("./").getAbsoluteFile(); System.out.println("absoluteFile = " + absoluteFile.getAbsolutePath()); //完成文件的复制(保存) FileCopyUtils.copy(pic.getInputStream(), new FileOutputStream("F:/88888888/" + pic.getOriginalFilename())); return "uploadsuccess"; } }
springbootfileApplication(启动入口类)
package com.baihzhi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Package: com.baihzhi * @ClassName: springbootfileApplication * @Author: alex * @CreateTime: 2020/11/9 14:07 * @Description: */ @SpringBootApplication public class springbootfileApplication { public static void main(String[] args) { SpringApplication.run(springbootfileApplication.class,args); } }