根据springMvc教程,改写的springboot文件上传功能,实际上很简单,核心使用的是MultipartFile类:
org.springframework.web.multipart.MultipartFile
代码如下:
package com.kuang.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@RestController
public class FileController {
@RequestMapping("/upload")
@ResponseBody
public String upload(){
return "上传";
}
@PostMapping("/uploadfile")
public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws IOException {
//上传路径保存设置
//String path = req.getServletContext().getRealPath("/upload");
String path = "E:/py/java/upload";
//可以使用UUID(唯一通用识别码)来保证文件名的统一
String uuidFileName = UUID.randomUUID().toString();
path = path + "/" + uuidFileName;
File realPath = new File(path);
if (!realPath.exists()) {
System.out.println(realPath.mkdir());
}
//上传文件地址
System.out.println("上传文件保存地址:" + realPath);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
return "redirect:/test.jsp";
}
}
在springMVC教程中,使用的是CommonsMultipartFile类:
org.springframework.web.multipart.commons.CommonsMultipartFile
该类继承了commons-fileupload
org.apache.commons.fileupload
但springboot需要使用MultipartFile
Spring Boot教程(十三):Spring Boot文件上传_蝈蝈的博客-CSDN博客_springboot文件上传
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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kuang</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
其中commons-fileupload似乎不是必要的。
application.yaml配置:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis2?useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
servlet:
multipart:
max-file-size: 100MB # file-size 总文件大小 MB 大写
max-request-size: 100MB # request-size 单个文件大小 MB 大写
enabled: true # 是否支持 multipart 上传文件
mybatis:
type-aliases-package: com.kuang.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
server:
port: 8081
其中multipart与文件上传相关,而且可以不配置。
在springboot中配置为CommonsMultipartFile会报类型无法转换的错误。
Spring Boot教程(十三):Spring Boot文件上传_蝈蝈的博客-CSDN博客_springboot文件上传