springboot3生成本地文件url的實(shí)現(xiàn)示例
流程
- avatar_dir:請(qǐng)求圖片在服務(wù)端的存放路徑
- user.dir:項(xiàng)目根目錄
效果
靜態(tài)資源訪問
application.yml
設(shè)置靜態(tài)文件存儲(chǔ)路徑
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/**") // 實(shí)際靜態(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)文件存儲(chǔ)路徑/文件名
String imageUrl = String.format( "http://%s:%d/%s", host, port, joinPaths( uploadConfig.getAvatarDirName(), avatar ) );
實(shí)現(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() 獲取請(qǐng)求 IP,如 localhost // currentUri.getPort() 獲取請(qǐng)求 端口號(hào),如 8080 return fileService.getImageUrl(user, currentUri.getHost(), currentUri.getPort()); } }
到此這篇關(guān)于springboot3生成本地文件url的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot3生成本地文件url內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java的MybatisPlus調(diào)用儲(chǔ)存過程的返回?cái)?shù)據(jù)問題
這篇文章主要介紹了java的MybatisPlus調(diào)用儲(chǔ)存過程的返回?cái)?shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12基于spring boot實(shí)現(xiàn)一個(gè)全局異常處理器
在項(xiàng)目開發(fā)中,我們可以基于spring boot提供的切面特性,來很輕松的實(shí)現(xiàn)全局異常的處理,所以本文主要為大家介紹了如何基于spring boot實(shí)現(xiàn)一個(gè)全局異常處理器,有需要的可以參考下2023-09-09Java順序表實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java順序表實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11MyBatis?實(shí)現(xiàn)動(dòng)態(tài)排序的多表查詢
本文將展示如何在 Java 項(xiàng)目中結(jié)合 MyBatis 實(shí)現(xiàn)動(dòng)態(tài)排序,尤其是在涉及多表查詢的情況下,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05