1、配置.yml文件
spring:
servlet:
multipart:
enabled: true
max-file-size: 100MB
max-request-size: 1000MB
file:
path: C:/tmp/apk
2、controller:
@RestController
public class TestController {
@Autowired
private UploadService uploadService;
@PostMapping("/upload")
public String upload( @RequestParam("file") MultipartFile file){
return uploadService.uploadImg(file);
}
}
3、service:
@Service
public class UploadService {
@Value("${file.path}")
private String filePath;
public String uploadImg(MultipartFile multipartFile) {
File targetFile = new File(filePath);
try {
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File targetFileName = new File(targetFile, Objects.requireNonNull(multipartFile.getOriginalFilename()));
multipartFile.transferTo(targetFileName);
return "upload success";
} catch (IOException e) {
e.printStackTrace();
return "upload fail";
}
}
}
4、使用postman测试:

5、去C:/tmp/apk 目录下查找上传的文件
该博客介绍了如何在Spring Boot应用中配置文件上传,包括在YAML文件中设置最大文件大小和请求大小,以及在Controller和Service层处理文件上传。示例展示了使用MultipartFile接口和File类进行文件保存,并通过Postman进行测试。
21万+

被折叠的 条评论
为什么被折叠?



