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 目录下查找上传的文件