最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot 策略模式實(shí)現(xiàn)切換上傳文件模式

 更新時(shí)間:2023年11月20日 12:03:48   作者:草木物語(yǔ)  
策略模式是指有一定行動(dòng)內(nèi)容的相對(duì)穩(wěn)定的策略名稱(chēng),這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下

SpringBoot 策略模式

策略模式是指有一定行動(dòng)內(nèi)容的相對(duì)穩(wěn)定的策略名稱(chēng)。

  • 我們定義一個(gè)接口(就比如接下來(lái)要實(shí)現(xiàn)的文件上傳接口)
  • 我們定義所需要實(shí)現(xiàn)的策略實(shí)現(xiàn)類(lèi) A、B、C、D(也就是項(xiàng)目中所使用的四種策略阿里云Oss上傳、騰訊云Cos上傳、七牛云Kodo上傳、本地上傳)
  • 我們通過(guò)策略上下文來(lái)調(diào)用策略接口,并選擇所需要使用的策略

策略接口

public interface UploadStrategy {
    /**
     * 上傳文件
     *
     * @param file     文件
     * @param filePath 文件上傳露肩
     * @return {@link String} 文件上傳的全路徑
     */
    String uploadFile(MultipartFile file, final String filePath);  

策略實(shí)現(xiàn)類(lèi)內(nèi)部實(shí)現(xiàn)

@Getter
@Setter
public abstract class AbstractUploadStrategyImpl implements UploadStrategy {
    @Override
    public String uploadFile(MultipartFile file, String filePath) {
        try {
            //region 獲取文件md5值 -> 獲取文件后綴名 -> 生成相對(duì)路徑
            String fileMd5 = XcFileUtil.getMd5(file.getInputStream());
            String extName = XcFileUtil.getExtName(file.getOriginalFilename());
            String fileRelativePath = filePath + fileMd5 + extName;
            //endregion
            //region 初始化
            initClient();
            //endregion
            //region 檢測(cè)文件是否已經(jīng)存在,不存在則進(jìn)行上傳操作
            if (!checkFileIsExisted(fileRelativePath)) {
                executeUpload(file, fileRelativePath);
            }
            //endregion
            return getPublicNetworkAccessUrl(fileRelativePath);
        } catch (IOException e) {
            throw new XcException("文件上傳失敗");
        }
    }
    /**
     * 初始化客戶端
     */
    public abstract void initClient();
    /**
     * 檢查文件是否已經(jīng)存在(文件MD5值唯一)
     *
     * @param fileRelativePath 文件相對(duì)路徑
     * @return true 已經(jīng)存在  false 不存在
     */
    public abstract boolean checkFileIsExisted(String fileRelativePath);
    /**
     * 執(zhí)行上傳操作
     *
     * @param file             文件
     * @param fileRelativePath 文件相對(duì)路徑
     * @throws IOException io異常信息
     */
    public abstract void executeUpload(MultipartFile file, String fileRelativePath) throws IOException;
    /**
     * 獲取公網(wǎng)訪問(wèn)路徑
     *
     * @param fileRelativePath 文件相對(duì)路徑
     * @return 公網(wǎng)訪問(wèn)絕對(duì)路徑
     */
    public abstract String getPublicNetworkAccessUrl(String fileRelativePath);
}  

本地上傳策略具體實(shí)現(xiàn)

@Slf4j
@Getter
@Setter
@RequiredArgsConstructor
@Service("localUploadServiceImpl")
public class LocalUploadStrategyImpl extends AbstractUploadStrategyImpl {
    /**
     * 本地項(xiàng)目端口
     */
    @Value("${server.port}")
    private Integer port;
    /**
     * 前置路徑 ip/域名
     */
    private String prefixUrl;
    /**
     * 構(gòu)造器注入bean
     */
    private final ObjectStoreProperties properties;
    @Override
    public void initClient() {
        try {
            prefixUrl = ResourceUtils.getURL("classpath:").getPath() + "static/";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new XcException("文件不存在");
        }
        log.info("initClient Init Success...");
    }
    @Override
    public boolean checkFileIsExisted(String fileRelativePath) {
        return new File(prefixUrl + fileRelativePath).exists();
    }
    @Override
    public void executeUpload(MultipartFile file, String fileRelativePath) throws IOException {
        File dest = checkFolderIsExisted(fileRelativePath);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            throw new XcException("文件上傳失敗");
        }
    }
    @Override
    public String getPublicNetworkAccessUrl(String fileRelativePath) {
        try {
            String host = InetAddress.getLocalHost().getHostAddress();
            if (StringUtils.isEmpty(properties.getLocal().getDomainUrl())) {
                return String.format("http://%s:%d%s", host, port, fileRelativePath);
            }
            return properties.getLocal().getDomainUrl() + fileRelativePath;
        } catch (UnknownHostException e) {
            throw new XcException("HttpCodeEnum.UNKNOWN_ERROR");
        }
    }
    /**
     * 檢查文件夾是否存在,若不存在則創(chuàng)建文件夾,最終返回上傳文件
     *
     * @param fileRelativePath 文件相對(duì)路徑
     * @return {@link  File} 文件
     */
    private File checkFolderIsExisted(String fileRelativePath) {
        File rootPath = new File(prefixUrl + fileRelativePath);
        if (!rootPath.exists()) {
            if (!rootPath.mkdirs()) {
                throw new XcException("文件夾創(chuàng)建失敗");
            }
        }
        return rootPath;
    }
}  

策略上下文實(shí)現(xiàn)

@Component
@RequiredArgsConstructor
public class UploadStrategyContext {
    private final Map<String, UploadStrategy> uploadStrategyMap;
    /**
     * 執(zhí)行上傳策略
     *
     * @param file     文件
     * @param filePath 文件上傳路徑前綴
     * @return {@link String} 文件上傳全路徑
     */
    public String executeUploadStrategy(MultipartFile file, final String filePath, String uploadServiceName) {
        // 執(zhí)行特定的上傳策略
        return uploadStrategyMap.get(uploadServiceName).uploadFile(file, filePath);
    }
}

上傳測(cè)試

參考資料:

文章來(lái)源:https://mp.weixin.qq.com/s/Bi7tFfKHXpBkXNpbTMR2lg

案例代碼:https://gitcode.net/nanshen__/store-object

到此這篇關(guān)于SpringBoot 策略模式 切換上傳文件模式的文章就介紹到這了,更多相關(guān)SpringBoot 策略模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中try-with-resources使用教程

    Java中try-with-resources使用教程

    try-with-resources是Java7引入的一種資源管理機(jī)制,用于自動(dòng)關(guān)閉實(shí)現(xiàn)了AutoCloseable接口的資源,避免資源泄漏,提升代碼安全性和簡(jiǎn)潔性,下面就來(lái)介紹一下使用小結(jié),感興趣的可以了解一下
    2026-01-01
  • mybatis@insert?注解如何判斷insert或是update

    mybatis@insert?注解如何判斷insert或是update

    這篇文章主要介紹了mybatis@insert?注解如何判斷insert或是update,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • springboot使用注解實(shí)現(xiàn)鑒權(quán)功能

    springboot使用注解實(shí)現(xiàn)鑒權(quán)功能

    這篇文章主要介紹了springboot使用注解實(shí)現(xiàn)鑒權(quán)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Java中使用注解的實(shí)例詳解

    Java中使用注解的實(shí)例詳解

    注解(Annotation)是放在Java源碼的類(lèi)、方法、字段、參數(shù)前的一種特殊“注釋”,這篇文章主要介紹了Java中如何使用注解,需要的朋友可以參考下
    2023-06-06
  • SpringBoot讀寫(xiě)操作yml配置文件方法

    SpringBoot讀寫(xiě)操作yml配置文件方法

    之前一直用的application.properties配置文件,只能是KV結(jié)構(gòu),后來(lái)的yml配置文件更像是樹(shù)狀結(jié)構(gòu),支持層級(jí),比properties更靈活
    2023-01-01
  • Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式

    Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式

    這篇文章主要介紹了Vue中computed計(jì)算屬性和data數(shù)據(jù)獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • @TableField(typeHandler = FastjsonTypeHandler.class)使用及說(shuō)明

    @TableField(typeHandler = FastjsonTypeHandler.clas

    文章總結(jié)個(gè)人前端上傳經(jīng)驗(yàn),提出了一些優(yōu)化上傳方法的建議,旨在提高上傳效率和用戶體驗(yàn),希望能為開(kāi)發(fā)者提供參考
    2026-04-04
  • 使用Spring提高接口吞吐量的常見(jiàn)方法

    使用Spring提高接口吞吐量的常見(jiàn)方法

    吞吐量衡量系統(tǒng)性能的關(guān)鍵指標(biāo),通常指單位時(shí)間內(nèi)系統(tǒng)可以處理的請(qǐng)求數(shù)量,面對(duì)高并發(fā),我們?cè)撊绾卫?nbsp;Spring 構(gòu)建一個(gè)高吞吐、高可用的接口系統(tǒng)?本文系統(tǒng)地分享在 Spring 中提升接口吞吐量的幾種常見(jiàn)手段,需要的朋友可以參考下
    2025-06-06
  • mybatis中的test語(yǔ)句失效處理方式

    mybatis中的test語(yǔ)句失效處理方式

    這篇文章主要介紹了mybatis中的test語(yǔ)句失效處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java-文件File簡(jiǎn)單實(shí)用方法(分享)

    Java-文件File簡(jiǎn)單實(shí)用方法(分享)

    下面小編就為大家?guī)?lái)一篇Java-文件File簡(jiǎn)單實(shí)用方法(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08

最新評(píng)論

德惠市| 黄骅市| 东方市| 耿马| 科技| 南漳县| 邢台市| 罗源县| 连平县| 德惠市| 德兴市| 弋阳县| 高邮市| 浦北县| 东乡| 大宁县| 双鸭山市| 公主岭市| 石棉县| 沾益县| 凤冈县| 盐亭县| 棋牌| 改则县| 洱源县| 正阳县| 明星| 内江市| 肇州县| 屏东市| 聊城市| 大连市| 尼玛县| 枣强县| 武冈市| 维西| 洛宁县| 广安市| 镶黄旗| 灵宝市| 旺苍县|