基于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
  • 阅读 ( 913 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
联系我们:uytrv@hotmail.com 问答工具