springboot3生成本地文件url的實現(xiàn)示例
流程
- avatar_dir:請求圖片在服務(wù)端的存放路徑
- user.dir:項目根目錄

效果

靜態(tài)資源訪問

application.yml
設(shè)置靜態(tài)文件存儲路徑
custom:
upload:
avatar_dir: ${user.dir}/avatar_dir/
avatar_dir_name: avatar_dir
FileUploadConfig
application.yml 信息讀取配置類
@Data
@Configuration
@ConfigurationProperties(prefix = "custom.upload")
public class FileUploadConfig {
private String avatarDir;
private String avatarDirName;
}
靜態(tài)資源訪問配置類
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
FileUploadConfig uploadConfig;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
File file = new File(uploadConfig.getAvatarDir());
String path = "file:" + file + File.separator;
// 匹配 http://ip:port/avatar_dir/ 下的所有文件
registry.addResourceHandler("/avatar_dir/**")
// 實際靜態(tài)文件地址
.addResourceLocations(path);
}
}
Service
@Service
public interface FileService {
// 獲取圖像 Url
public Result<String> getImageUrl(User user, String host, int port);
// 路徑拼接
public String joinPaths(String... paths);
}
ServiceImpl
http://ip:port/靜態(tài)文件存儲路徑/文件名
String imageUrl = String.format( "http://%s:%d/%s", host, port, joinPaths( uploadConfig.getAvatarDirName(), avatar ) );
實現(xiàn)代碼
@Service
public class FileServiceImpl implements FileService {
@Autowired
FileUploadConfig uploadConfig;
@Autowired
IUserService userService;
// 路徑拼接
@Override
public String joinPaths(String... paths) {
Path resultPath = Paths.get("");
for (String path : paths) {
resultPath = resultPath.resolve(path);
}
return resultPath.toString();
}
// 判斷文件是否存在
private Boolean isUserAvatarExists(String avatar) {
String path = joinPaths(uploadConfig.getAvatarDir(), avatar);
File filePath = new File(path);
return filePath.exists();
}
// 獲取圖像 Url
@Override
public Result<String> getImageUrl(User user, String host, int port) {
// 用戶頭像的文件名唯一,并保存在了數(shù)據(jù)庫中,avatar = xxx.png
String avatar = this.userService.getById(user.getUserId()).getAvatar();
if (isUserAvatarExists(avatar)) {
String imageUrl = String.format("http://%s:%d/%s", host, port, joinPaths(uploadConfig.getAvatarDirName(), avatar));
return Result.successfulResult("獲取成功", imageUrl);
}
return Result.errorResult("文件丟失");
}
}
Controller
@Tag(name = "文件上傳接口")
@RestController
@RequestMapping("/sign/file")
public class FileUploadController {
@Autowired
FileService fileService;
@Operation(summary = "獲取圖片 URL")
@PostMapping("/image/get")
public Result<String> getImageUrl(@RequestBody User user) {
URI currentUri = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri();
// currentUri.getHost() 獲取請求 IP,如 localhost
// currentUri.getPort() 獲取請求 端口號,如 8080
return fileService.getImageUrl(user, currentUri.getHost(), currentUri.getPort());
}
}到此這篇關(guān)于springboot3生成本地文件url的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot3生成本地文件url內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題
這篇文章主要介紹了java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
基于spring boot實現(xiàn)一個全局異常處理器
在項目開發(fā)中,我們可以基于spring boot提供的切面特性,來很輕松的實現(xiàn)全局異常的處理,所以本文主要為大家介紹了如何基于spring boot實現(xiàn)一個全局異常處理器,有需要的可以參考下2023-09-09
MyBatis?實現(xiàn)動態(tài)排序的多表查詢
本文將展示如何在 Java 項目中結(jié)合 MyBatis 實現(xiàn)動態(tài)排序,尤其是在涉及多表查詢的情況下,具有一定的參考價值,感興趣的可以了解一下2024-05-05

