SpringBoot整合fastDFS

1. 新建SpringBoot项目

SpringBoot整合fastDFS_第1张图片

2. 项目依赖

<dependencies>
        
        <dependency>
            <groupId>com.github.tobatogroupId>
            <artifactId>fastdfs-clientartifactId>
            <version>1.26.4version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
    dependencies>

3. 配置文件

fdfs:
  so-timeout: 3000
  connect-timeout: 1000
  thumb-image:
    width: 60
    height: 60
  tracker-list:
    - 121.199.58.42:22122

4. 文件服务配置类

package zw.springboot.config;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

/**
 * @ClassName FastDFSClientConfig
 * @Description 文件服务器配置类
 * @Author 周威
 * @Date 2020-08-01 - 21:43
 */
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastDFSClientConfig
{
}

5. 文件上传测试

package zw.springboot;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;

@SpringBootTest
class SpringbootApplicationTest
{
    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件上传
     */
    @Test
    public void uploadTest()
    {
        InputStream is = null;
        try
        {
            // 获取文件源
            File source = new File("D:\\util\\fstDFS.jpg");
            // 获取文件流
            is = new FileInputStream(source);
            // 进行文件上传
            StorePath storePath = storageClient.uploadFile(is, source.length(), FilenameUtils.getExtension(source.getName()), null);
            // 获得文件上传后访问地址
            String fullPath = storePath.getFullPath();
            // 打印访问地址
            System.out.println("fullPath = " + fullPath);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(is != null)
                {
                    // 关闭流资源
                    is.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

6. 文件上传运行

  • 控制台打印文件访问路径:fullPath = group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg
  • 浏览器输入地址访问:ip + port + fullPath

SpringBoot整合fastDFS_第2张图片

7. 文件下载测试

package zw.springboot;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;

@SpringBootTest
class SpringbootApplicationTest
{
    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件下载
     */
    @Test
    public void downloadTest()
    {
        // 文件访问地址
        String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg";
        // 分离文件分组
        String group = fullPath.substring(0, fullPath.indexOf("/"));
        // 分离文件路径
        String path = fullPath.substring(fullPath.indexOf("/") + 1);
        // 进行文件下载
        byte[] buffer = storageClient.downloadFile(group, path, new DownloadByteArray());
        // 创建输出文件源
        File target = new File("D://util", "target" + fullPath.substring(fullPath.indexOf(".")));
        OutputStream os = null;
        try
        {
            // 获取文件输出字节流
            os = new FileOutputStream(target);
            // 将字节数组内容写入文件源
            os.write(buffer);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                // 关闭流资源
                if (os != null)
                {
                    os.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

8. 文件下载运行

SpringBoot整合fastDFS_第3张图片

9. 文件删除测试

package zw.springboot;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;

@SpringBootTest
class SpringbootApplicationTest
{
    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件删除
     */
    @Test
    public void deleteTest()
    {
        // 文件访问地址
        String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg";
        // 分离文件分组
        String group = fullPath.substring(0, fullPath.indexOf("/"));
        // 分离文件路径
        String path = fullPath.substring(fullPath.indexOf("/") + 1);
        // 进行文件删除
        storageClient.deleteFile(group, path);
    }
}

10. 文件删除运行

  • 浏览器输入地址访问:ip + port + fullPath

SpringBoot整合fastDFS_第4张图片

你可能感兴趣的:(java,fastdfs,spring,boot,spring)