controller代码
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@PostMapping("/uploadFile")
public BRB uploadVideo(@RequestParam("uploadFile") MultipartFile uploadFile) {
if (uploadFile == null) {
return new BRB(BRStatus.PARAS_ERROR);
}
String fileName = "/Users/mac/Documents/temp/" + uploadFile.getOriginalFilename();
File fileSource = new File(fileName);
try {
uploadFile.transferTo(fileSource);
} catch (IOException e) {
throw new CustomException("上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
}
return new BRB(BRStatus.SUCCESS, fileName);
}
}
postman模拟请求截图如下:
properties配置
文件大于10m时,需要修改项目配置,要不然会提示如下错误
项目中使用的SpringBoot版本是SpringBoot 2.0.3,properties需修改如下:
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=1000MB
Nginx大文件上传配置
如果服务器上使用Nginx做代理,修改Nginx配置如下,在http{}里面添加,如下
http {
client_max_body_size 100m;
}