springboot保存图片

本文最后更新于:2023年8月24日 晚上

关键代码

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
@Service
public class UserServiceImpl implements UserService {

@Value("${avatar.intUrl}")
private String intUrl;

@Value("${avatar.outUrl}")
private String outUrl;

@Override
public String uploadAvatar(MultipartFile multipartFile) {
// 获取file文件后缀名
String[] arrayString = multipartFile.getOriginalFilename().split("\\.");
String fileSuffix = "." + arrayString[arrayString.length - 1];
// 新生成文件名,创建File对象
String fileName = UUID.randomUUID().toString() + System.currentTimeMillis() + fileSuffix;
File file = new File(intUrl, fileName);
// 写入指定路径,抛出io异常
try {
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
return outUrl + fileName;
}
}

springboot保存图片
https://xin-fas.github.io/2022/08/19/springboot保存图片/
作者
Xin-FAS
发布于
2022年8月19日
更新于
2023年8月24日
许可协议