上传文件的大小设置
2022-09-22 22:48:24
环境
- VUE
- spring boot项目
- Nginx
文件大小限制
- application.yml配置
1 | spring: |
- Nginx配置
vi /usr/local/nginx/conf/nginx.conf
修改http下的client_max_body_size为5M(你所允许的最大文件大小)
- VUE代码限制
1 | //使用的是ElementUI的组件 |
- Controller代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class UploadController {
private String xxImg;
private String service;
public CommonResult<Object> uploadImg(MultipartFile file) {
Calendar now = Calendar.getInstance();
String year = Integer.toString(now.get(Calendar.YEAR));
String month = Integer.toString(now.get(Calendar.MONTH)+1);
String savePath = xxImg+ "Rectify/" + year + "/" + month + "/";
InputStream is = null;
OutputStream os = null;
try {
String suffix = file.getOriginalFilename();
String fileName = Utils.getUUID() + suffix.substring(suffix.lastIndexOf("."));
System.out.println(fileName);
File tempFile = new File(savePath);
// 判断路径是否存在,不存在就创建一个
if (!tempFile.exists()) {
tempFile.mkdirs();
}
is = file.getInputStream();
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
byte[] bs = new byte[1024];
int len = 0;
while ((len = is.read(bs)) != -1) {
// 写入输出文件
os.write(bs, 0, len);
}
String resUrl = service + savePath + fileName;
return CommonResult.success(resUrl);
} catch (IOException e) {
e.printStackTrace();
return CommonResult.forbidden(null);
} finally {
// 完毕,关闭所有链接
try {
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}