还不清楚SpringBoot整合fastDFS过程的,一定得看看!

新建SpringBoot项目

项目依赖


        
        
            com.github.tobato
            fastdfs-client
            1.26.4
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    

配置文件

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

文件服务配置类

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
{
}

文件上传测试

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

文件上传运行

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

文件下载测试

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

文件下载运行

文件删除测试

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);
    }
}

文件删除运行

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


最后

感谢你看到这里,文章有什么不足还请指正,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

你可能感兴趣的:(还不清楚SpringBoot整合fastDFS过程的,一定得看看!)