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

效果

靜態(tài)資源訪(fǎng)問(wèn)

application.yml
設(shè)置靜態(tài)文件存儲(chǔ)路徑
custom:
upload:
avatar_dir: ${user.dir}/avatar_dir/
avatar_dir_name: avatar_dir
FileUploadConfig
application.yml 信息讀取配置類(lèi)
@Data
@Configuration
@ConfigurationProperties(prefix = "custom.upload")
public class FileUploadConfig {
private String avatarDir;
private String avatarDirName;
}
靜態(tài)資源訪(fǎng)問(wèn)配置類(lè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) {
// 用戶(hù)頭像的文件名唯一,并保存在了數(shù)據(jù)庫(kù)中,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基礎(chǔ)之淺談hashCode()和equals()
今天給大家?guī)?lái)的是關(guān)于Java基礎(chǔ)的相關(guān)知識(shí),文章圍繞著hashCode()和equals()展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
java如何實(shí)現(xiàn)自動(dòng)生成數(shù)據(jù)庫(kù)設(shè)計(jì)文檔
以前我們還需要手寫(xiě)數(shù)據(jù)庫(kù)設(shè)計(jì)文檔、現(xiàn)在可以通過(guò)引入screw核心包來(lái)實(shí)現(xiàn)Java?數(shù)據(jù)庫(kù)文檔一鍵生成。本文將具體介紹一下如何通過(guò)java自動(dòng)生成數(shù)據(jù)庫(kù)設(shè)計(jì)文檔,需要的朋友可以參考下2021-11-11
如何使用@AllArgsConstructor和final 代替 @Autowired
這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring之spring-context-indexer依賴(lài)詳解
這篇文章主要介紹了Spring之spring-context-indexer依賴(lài)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中關(guān)于字典樹(shù)的算法實(shí)現(xiàn)
字典樹(shù),又稱(chēng)單詞查找樹(shù),Trie樹(shù),是一種樹(shù)形結(jié)構(gòu),哈希表的一個(gè)變種。用于統(tǒng)計(jì),排序和保存大量的字符串,本文針對(duì)字典樹(shù)給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09
聊聊Java 成員變量賦值和構(gòu)造方法誰(shuí)先執(zhí)行的問(wèn)題
這篇文章主要介紹了聊聊Java 成員變量賦值和構(gòu)造方法誰(shuí)先執(zhí)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
java模擬發(fā)送form-data的請(qǐng)求方式
這篇文章主要介紹了java模擬發(fā)送form-data的請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
spring-boot項(xiàng)目啟動(dòng)遲緩異常排查解決記錄
這篇文章主要為大家介紹了spring-boot項(xiàng)目啟動(dòng)遲緩異常排查解決記錄,突然在本地啟動(dòng)不起來(lái)了,表象特征就是在本地IDEA上運(yùn)行時(shí),進(jìn)程卡住也不退出,應(yīng)用啟動(dòng)時(shí)加載相關(guān)組件的日志也不輸出2022-02-02

