欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot3生成本地文件url的實現(xiàn)示例

 更新時間:2024年01月10日 09:07:33   作者:燈籠只能來教室體驗生活  
本文主要介紹了springboot3生成本地文件url的實現(xiàn)示例,從而提供一種高效的文件管理方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

流程

  • avatar_dir:請求圖片在服務端的存放路徑
  • user.dir:項目根目錄

在這里插入圖片描述

效果

在這里插入圖片描述

靜態(tài)資源訪問

在這里插入圖片描述

application.yml
設置靜態(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中的異常處理(上)

    帶你了解Java中的異常處理(上)

    這篇文章主要介紹了Java異常處理的相關(guān)資料,幫助大家更好的理解和學習java,感興趣的朋友可以了解下
    2020-08-08
  • 使用Spring Boot集成FastDFS的示例代碼

    使用Spring Boot集成FastDFS的示例代碼

    本篇文章主要介紹了使用Spring Boot集成FastDFS的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 詳解Java中二分法的基本思路和實現(xiàn)

    詳解Java中二分法的基本思路和實現(xiàn)

    二分法是一個非常高效的算法,它常常用于計算機的查找過程中。本文將通過示例為大家詳細講講二分法的基本思路和實現(xiàn),感興趣的可以了解一下
    2022-08-08
  • java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題

    java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題

    這篇文章主要介紹了java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Kotlin內(nèi)存陷阱inline使用技巧示例詳解

    Kotlin內(nèi)存陷阱inline使用技巧示例詳解

    這篇文章主要為大家介紹了Kotlin內(nèi)存陷阱inline使用技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • 基于spring boot實現(xiàn)一個全局異常處理器

    基于spring boot實現(xiàn)一個全局異常處理器

    在項目開發(fā)中,我們可以基于spring boot提供的切面特性,來很輕松的實現(xiàn)全局異常的處理,所以本文主要為大家介紹了如何基于spring boot實現(xiàn)一個全局異常處理器,有需要的可以參考下
    2023-09-09
  • nacos配置在代碼中引用的方法講解

    nacos配置在代碼中引用的方法講解

    這篇文章主要介紹了nacos配置在代碼中如何引用,如果主配置中配置的內(nèi)容和拓展配置的內(nèi)容重復則按主配置的配置 ,如果拓展配置中的內(nèi)容和另一個拓展配置中的內(nèi)容重復,則按下標大的配置作為最終的配置,對nacos配置代碼引用相關(guān)知識感興趣朋友一起看看吧
    2022-12-12
  • JWT原理與java操作jwt驗證詳解

    JWT原理與java操作jwt驗證詳解

    這篇文章主要介紹了JWT原理與java操作jwt驗證,詳細分析了JWT的基本概念、原理與java基于JWT進行token驗證的相關(guān)操作技巧,需要的朋友可以參考下
    2023-06-06
  • Java順序表實現(xiàn)圖書管理系統(tǒng)

    Java順序表實現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Java順序表實現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • MyBatis?實現(xiàn)動態(tài)排序的多表查詢

    MyBatis?實現(xiàn)動態(tài)排序的多表查詢

    本文將展示如何在 Java 項目中結(jié)合 MyBatis 實現(xiàn)動態(tài)排序,尤其是在涉及多表查詢的情況下,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05

最新評論