SpringBoot重点详解--集成FastDFS(二)

一、Maven依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 引入fastdfs客户端依赖 -->
        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>fastdfs-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

二、application.yml配置

在核心配置文件中增加如下配置:

fdfs:
  connect-timeout: 2000
  so-timeout: 3000
  tracker-list:
    - 172.16.250.238:22122

三、Junit测试

import com.luhuiguo.fastdfs.domain.FileInfo;
import com.luhuiguo.fastdfs.domain.StorePath;
import com.luhuiguo.fastdfs.service.FastFileStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.imageio.stream.FileImageOutputStream;
import java.io.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FdfsTests {

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 文件上传测试
     */
    @Test
    public void testUpload() {

        File file = new File("C:\\1.jpg");
        try {
            StorePath storePath = storageClient.uploadFile(new FileInputStream(file), file.length(), file.getName(), null);
            System.out.println("文件上传成功,文件ID: " + storePath); // 文件ID: StorePath [group=group1, path=M00/00/00/rBD67l2BR5iABIjjAABB4RISex87.1.jpg]
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件下载测试
     */
    @Test
    public void testDownload() throws FileNotFoundException {
        try {
            byte[] bytes = storageClient.downloadFile("group1", "M00/00/00/rBD67l2BR5iABIjjAABB4RISex87.1.jpg");
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File("C:\\1_bak.jpg"));
            imageOutput.write(bytes, 0, bytes.length);
            imageOutput.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件删除测试
     */
    @Test
    public void testDelete() {
        try {
            FileInfo fileInfo = storageClient.queryFileInfo("group1", "M00/00/00/rBD67l2BR5iABIjjAABB4RISex87.1.jpg");
            System.out.println("文件大小(单位 byte): " + fileInfo.getFileSize());
            storageClient.deleteFile("group1", "M00/00/00/rBD67l2BR5iABIjjAABB4RISex87.1.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值