SpringBoot整合fastDFS

1. 新建SpringBoot项目

在这里插入图片描述

2. 项目依赖

<dependencies>
        <!-- fastDFS-client -->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.4</version>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </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

在这里插入图片描述

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. 文件下载运行

在这里插入图片描述

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

在这里插入图片描述

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于如何将Spring BootFastDFS集成的一般步骤。以下是大致步骤: 1. 添加FastDFS客户端依赖 在您的Spring Boot项目的pom.xml文件中添加FastDFS客户端依赖,例如: ```xml <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.27.1</version> </dependency> ``` 2. 配置FastDFS客户端 在您的Spring Boot项目的application.properties或application.yml文件中添加FastDFS客户端的配置,例如: ```properties fdfs.trackerList=tracker_server1_ip:tracker_server1_port,tracker_server2_ip:tracker_server2_port ``` 3. 配置文件上传服务 创建一个文件上传服务类,例如: ```java @Service public class FileUploadService { @Autowired private FastFileStorageClient fastFileStorageClient; public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null); return storePath.getFullPath(); } } ``` 4. 使用文件上传服务 在您的Spring Boot项目的控制器中使用文件上传服务,例如: ```java @RestController public class FileUploadController { @Autowired private FileUploadService fileUploadService; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { return fileUploadService.uploadFile(file); } } ``` 这是一个基本的Spring BootFastDFS集成的示例,您可以根据您的需求进行修改和定制。希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值