接口:
/**
* 上传任意文件
*/
@PostMapping("/img")
public Object addimg(MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
long size = file.getSize();
System.out.println(size);
// 1kb == 1024b
// 限制文件大小 10兆 10485760 43054875springboot
if(size >= 10485760){
return "文件太大了,不允许上传";
}
InputStream in = new ByteArrayInputStream(bytes);
FileOutputStream fos = new FileOutputStream("D://"+file.getOriginalFilename());
byte[] b = new byte[1024];
int nRead = 0;
while ((nRead = in.read(b)) != -1) {
fos.write(b, 0, nRead);
}
fos.flush();
fos.close();
in.close();
System.out.println("上传成功");
return "上传成功";
}
springboot 配置文件 可以指定 文件上传大小:
# 单文件上传的大小
spring.servlet.multipart.max-file-size=100MB
# 单次请求 上传文件的总大小
spring.servlet.multipart.max-request-size=300MB