• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    基于FastDFS實現分布式存儲的上傳下載等操作

    基于FastDFS實現分布式存儲的上傳下載等操作,并將程序封裝成工具類方便直接使用。整體項目實現分解成下文5個步驟。

    東西/原料

    • IDEA
    • FastDFS

    方式/步調

    1. 1

      1、在項目中添加依靠到Maven的pom文件

      <!-- fastdfs-client -->

      <dependency>

      <groupId>org.csource</groupId>

      <artifactId>fastdfs-client-java</artifactId>

      <version>1.27-SNAPSHOT</version>

      </dependency>

      <dependency>

      <groupId>commons-io</groupId>

      <artifactId>commons-io</artifactId>

      <version>2.4</version>

      </dependency>

    2. 2

      2、FastDFS客戶端東西類,已封裝如下(便利直接利用),

      本家兒要東西類方式:

      /**

      * @Description 文件存儲上傳客戶端

      * @author javaer

      * @time 2020年5月30日 上午9:27:26

      */

      public class FastDFSClient {

        /**

         * MultipartFile 上傳文件

         *

         * @param file  MultipartFile

         * @return 返回上傳當作功后的文件路徑

         */

        public static String uploadFileWithMultipart(MultipartFile file) throws BusinessException {

           return upload(file, null);

        }

        /**

         * 以附件形式下載文件

         *

         * @param filepath  文件路徑

         * @param response

         */

        public static void downloadFile(String filepath, HttpServletResponse response) throws BusinessException {

           download(filepath, null, null, response);

        }

      /**

      * 刪除文件

      * @param filepath 文件路徑

      * @return 刪除當作功返回 0, 掉敗返回其它

      */

      public static int deleteFile(String filepath) throws BusinessException {

      if (StringUtils.isBlank(filepath)) {

      throw new BusinessException(FileErrorCode.FILE_PATH_ISNULL.CODE);

      }

      TrackerServer trackerServer = TrackerServerPool.borrowObject();

      StorageClient1 storageClient = new StorageClient1(trackerServer, null);

      int success = 0;

      try {

      success = storageClient.delete_file1(filepath);

      if (success != 0) {

      throw new BusinessException(FileErrorCode.FILE_DELETE_FAILED.CODE);

      }

      } catch (IOException e) {

      e.printStackTrace();

      } catch (MyException e) {

      e.printStackTrace();

      throw new BusinessException(FileErrorCode.FILE_DELETE_FAILED.CODE);

      }

      // 返還對象

      TrackerServerPool.returnObject(trackerServer);

      return success;

      }

      }

    3. 3

      3、將fastdfs-client.properties設置裝備擺設文件放置于引用項目標resource目次下

    4. 4

      4、如下圖,在項目中可直接經由過程FastDFSClient東西類挪用所用的方式

    5. 5

      5、項目中上傳文件后利用的結果,由前端頁面上傳文件,至此項目實現分布式存儲。

      END
    • 發表于 2020-05-31 19:00
    • 閱讀 ( 922 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆