springboot2.2.x集成Fastdfs分布式文件系统

说明

  1. springboot启动类配置@ComponentScan(basePackages = {“xxx.xxxx”}),文件操作工具类包的相对路径和绝对路径都可
  2. springboot各服务项目需要配置文件上传的最大值 spring.servlet.multipart.max-file-size=10MB
  3. 若文件上传接口经过网关转发,网关也需要配置文件上传最大值 spring.servlet.multipart.max-file- size=10MB
  4. fastdfs文件服务器的搭建 点我查看

pom依赖

 		<!-- Fastdfs -->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.6</version>
        </dependency>
        <!--File操作-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>

application.yml配置文件

# 分布式文件系统FDFS配置
# 连接超时时间
fdfs.connect-timeout=30000
# 读取超时时间
fdfs.so-timeout=30000
# 缩略图的宽高
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
# tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
fdfs.tracker-list=111.231.86.179:22122
#从池中借出的对象的最大数目(配置为-1表示不限制)
fdfs.pool.max-total=-1
#获取连接时的最大等待毫秒数(默认配置为5秒)
fdfs.pool.max-wait-millis=30000
#每个key最大连接数
fdfs.pool.max-total-per-key=1000
#每个key对应的连接池最大空闲连接数
fdfs.pool.max-idle-per-key=500
#每个key对应的连接池最小空闲连接数
fdfs.pool.max_idle_per_key=20

#springboot支持文件上传最大值
spring.servlet.multipart.max-file-size=10MB

FastDFS文件操作核心类

package com.spin.common.fastdfs;

import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.spin.common.string.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
/**
 # 分布式文件系统FDFS配置
 # 连接超时时间 30S
 fdfs.connect-timeout=30000
 # 读取超时时间 30S
 fdfs.so-timeout=30000
 # 缩略图的宽高
 fdfs.thumb-image.height=150
 fdfs.thumb-image.width=150
 # tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
 fdfs.tracker-list=111.231.86.179:22122
 #从池中借出的对象的最大数目(配置为-1表示不限制)
 fdfs.pool.max-total=-1
 #获取连接时的最大等待毫秒数(默认配置为5秒)
 fdfs.pool.max-wait-millis=30000
 #每个key最大连接数
 fdfs.pool.max-total-per-key=1000
 #每个key对应的连接池最大空闲连接数
 fdfs.pool.max-idle-per-key=500
 #每个key对应的连接池最小空闲连接数
 fdfs.pool.max_idle_per_key=20
 * /
/**
 * @author OuYang Li
 * @version 1.0.0
 * @ClassName FastdfsUtils.java
 * @Description fastdfs文件上传类
 * 各服务调用方需要通过自身服务的资源文件配置springboot 2.x+版本标准的支撑FastFileStorageClient实例的相关属性
 * @createTime 2021-02-20  14:33:00
 */
@Component
public final class FastdfsUtils {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    /**
     * 上传
     * @param file
     * @return com.github.tobato.fastdfs.domain.fdfs.StorePath
     * @throws IOException
     */
    public StorePath upload(MultipartFile file) throws IOException {
        // 设置文件信息
        Set<MetaData> mataData = new HashSet<>();
        mataData.add(new MetaData("author", "fastdfs"));
        mataData.add(new MetaData("description",file.getOriginalFilename()));
        // 上传
        StorePath storePath = fastFileStorageClient.uploadFile(
                file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()),
                null);
        return storePath;
    }

    /**
     * 删除
     * @param path
     */
    public void delete(String path) {
        fastFileStorageClient.deleteFile(path);
    }

    /**
     * 删除
     * @param group
     * @param path
     */
    public void delete(String group,String path) {
        fastFileStorageClient.deleteFile(group,path);
    }

    /**
     * 文件下载接口(直接通过nginx代理下载也可以)
     * @param path 文件路径,例如:/group1/M00/00/00/itstyle.png
     * @param filename 下载的文件命名
     * @return
     */
    //@Synchronized
    public void download(String path, String filename, HttpServletResponse response) throws IOException {
        // 获取文件
        StorePath storePath = StorePath.parseFromUrl(path);
        if (StringUtils.isBlank(filename)) {
            filename = FilenameUtils.getName(storePath.getPath());
        }
        byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
        response.reset();
        response.setContentType("applicatoin/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值