小言_互联网的博客

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

367人阅读  评论(0)

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

}


转载:https://blog.csdn.net/pengjunlee/article/details/100971359
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场