FastDFS在SpringBoot的简单部署及用法

本文介绍了如何使用Spring Boot与FastDFS进行文件上传,配置TrackerList和Web Server URL,以及编写测试用例来验证上传和访问功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入依赖

 <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.7</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.yml配置文件

# 分布式文件系统FDFS配置
fdfs:
  so-timeout: 1500
  connect-timeout: 600
  #缩略图生成参数
  thumb-image:
    width: 150
    height: 150
  #TrackerList参数,支持多个
  tracker-list: 192.168.136.160:22122
  web-server-url: http://192.168.136.160:8888/

3.测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppServerApplication.class)
public class Test {

    //从调度服务器获取,一个目标存储服务器,上传
    @Autowired
    private FastFileStorageClient   fastFileStorageClient;

    // 获取存储服务器的请求URL

    @Autowired
    private FdfsWebServer webServer;

    @Test
    public void test() throws FileNotFoundException {
         //1. 指定文件
        File file = new File("F:\\a.jpg");
        //2. 文件上传
        StorePath path = client.uploadFile(new FileInputStream(file),
                file.length(), "jpg", null);
        //3. 拼接访问路径

        /*path.getFullPath()是指该文件所存放的路径信息和文件名,比如e/d/a.txt

       webServer.getWebServerUrl()是指获取配置文件中的web-server-url */


        String url = webServer.getWebServerUrl() + path.getFullPath();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值