目录
1、配置文件上传所需的依赖
文件上传的依赖,pom.xml
内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--@Slf4j日志组件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、配置文件上传的文件大小限制
application.properties
配置文件添加:
# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB
3、单文件上传示例
创建UploadController控制类,内容如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;
@Slf4j
@RestController
@RequestMapping
public class UploadController {
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("uploadFile") MultipartFile uploadFile) {
if (uploadFile.isEmpty()) {
return "上传文件为空,请选择文件";
}
String fileName = uploadFile.getOriginalFilename();
String filePath = "D:/uploadfiles/"; //本地测试路径
File dest = new File(filePath + fileName);
try {
//使用组件上传
uploadFile.transferTo(dest);
log.info("文件:{},上传成功", fileName);
return "上传成功!";
} catch (IOException e) {
log.error(e.toString(), e);
}
return "上传失败!";
}
}
使用postman测试接口
4、多文件上传示例
多文件上传只是比单文件上传多了File选择的Input而已,UploadController控制类的变动如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController
@RequestMapping
public class UploadController {
// 方式一:使用HttpServletRequest接收参数
@PostMapping("/multiUpload")
@ResponseBody
public String multiUpload(HttpServletRequest request) {
List<MultipartFile> uploadFiles = ((MultipartHttpServletRequest) request).getFiles("uploadFile");
String filePath = "D:/uploadfiles/";
for (int i = 0; i < uploadFiles.size(); i++) {
MultipartFile file = uploadFiles.get(i);
int index = i; //记录上传的文件
if (file.isEmpty()) {
return "上传第" + (++index) + "个文件失败";
}
String fileName = file.getOriginalFilename();
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
log.info("第" + (++index) + "个文件上传成功");
} catch (IOException e) {
log.error(e.toString(), e);
return "上传第" + (++index) + "个文件失败";
}
}
return "上传成功!";
}
// 方式二:直接使用MultipartFile[]数组接收参数
@PostMapping("/import")
public String multiUpload2(@RequestParam("uploadFile") MultipartFile[] files) throws Exception {
String filePath = "D:/uploadfiles/";
List<MultipartFile> multipartFiles = Arrays.asList(files);
//使用stream不方便跟踪具体文件信息
multipartFiles.stream().forEach((file) -> {
if (file.isEmpty()) {
throw new RuntimeException("文件内容为空!");
}
String fileName = file.getOriginalFilename();
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
} catch (IOException e) {
throw new RuntimeException(e); //只能抛出RuntimeException
}
});
return "文件上传成功!";
}
}
使用postman测试接口
查看本地上传结果:
参考文章:
Spring Boot教程(十三):Spring Boot文件上传_蝈蝈的博客-CSDN博客_springboot文件上传