当前位置:网站首页>fastdfs-client使用

fastdfs-client使用

2022-08-09 17:18:00 wfsm

官方: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>

配置文件:

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
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());
        }

    }
}

测试:

  @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);
    }

遇到的问题:

  • 怎么模拟一个MultipartFile
    MockMultipartFile
    引用:https://blog.csdn.net/qq_37157160/article/details/104512457
原网站

版权声明
本文为[wfsm]所创,转载请带上原文链接,感谢
https://waterkid.blog.csdn.net/article/details/126222378