当前位置:   article > 正文

fastdfs-client使用

fastdfs-client

官方:https://github.com/tobato/FastDFS_Client
springboot集成:https://blog.csdn.net/wzl19870309/article/details/74049204

主要接口包括
TrackerClient - TrackerServer接口
GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)

<dependency>
   <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.27.2</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

配置文件:

fdfs:
  connect-timeout: 60000
  so-timeout: 60000
  tracker-list: xxx
  pool:
    ## 连接池最大数量
    max-total: 200
    ## 每个tracker地址的最大连接数
    max-total-per-key: 50
    ## 连接耗尽时等待获取连接的最大毫秒数
    max-wait-millis: 60000
  web-server-url: xxx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
package com.cj.fdfs;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;

@Component
public class FastDFSClient {
    private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    /**
     * 上传文件
     * @param file
     */
    public String uploadFile(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);

//        FilenameUtils
        return getResAccessUrl(storePath);
    }


    public String uploadFile(File file) throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(storePath);
    }

    /**
     * 将一段字符串 生成一个 文件 上传
     * @param content
     * @param fileExtension
     */
    public String uploadFile(String content,String fileExtension){
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        StorePath storePath = storageClient.uploadFile(inputStream, bytes.length, fileExtension, null);
        return getResAccessUrl(storePath);
    }

    /**
     * 获取上传后的 url
     * @param storePath
     */
    public String getResAccessUrl(StorePath storePath){
        String fileUrl = fdfsWebServer.getWebServerUrl()+ "/" + storePath.getFullPath();
        return  fileUrl;
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public void deleteFile(String fileUrl){
        if (StringUtils.isEmpty(fileUrl)){
            return;
        }

        try {
            // 存储文件的路径信息
            StorePath storePath = StorePath.parseFromUrl(fileUrl);

            storageClient.deleteFile(storePath.getGroup(),storePath.getPath());
        } catch (Exception e) {
           logger.warn(e.getMessage());
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

测试:

  @Test
    void contextLoads() throws IOException {
        Path path = Paths.get("F:\\1.png");
        File file = new File(path.toUri());
        MockMultipartFile multipartFile = new MockMultipartFile(file.getName(),file.getName(),null, new FileInputStream(file));

        String s = fastDFSClient.uploadFile(multipartFile);
        System.out.println("s = " + s);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

遇到的问题:

  • 怎么模拟一个MultipartFile
    MockMultipartFile
    引用:https://blog.csdn.net/qq_37157160/article/details/104512457
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/169868
推荐阅读
相关标签
  

闽ICP备14008679号