springboot fastdfs封装

        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.27.3</version>
        </dependency>

该包从 https://github.com/tobato/FastDFS_Client 1.27.3-SNAPSHOT版本
修改关键信息日志级别并修改 FdfsClientConstants 配置前缀
fdfs -> fastdfs


pom.xml

    <dependencies>
        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.27.3</version>
        </dependency>
    </dependencies>

FastDfsService.java

public interface FastDfsService extends FastFileStorageClient {
    String uploadFile(String group, File file, String fileExtName);
}

FastDfsServiceImpl.java

@Slf4j
@Service
public class FastDfsServiceImpl extends DefaultFastFileStorageClient implements FastDfsService {
    @Value("${fastdfs.default-group:group0}")
    private String defaultGroup;

    @Override
    public String uploadFile(String group, File file, String fileExtName) {
        if (StrUtil.isEmpty(fileExtName)) {
            fileExtName = getExtension(FileUtil.getName(file));
        }
        if (StrUtil.isEmpty(group)) {
            group = defaultGroup;
        }
        BufferedInputStream inputStream = FileUtil.getInputStream(file);
        try (inputStream) {
            StorePath storePath = uploadFile(group, inputStream, FileUtil.size(file), fileExtName);
            return storePath.getFullPath();
        } catch (IOException e) {
            throw new RuntimeException("上传失败[]" + e.getMessage());
        }
    }

    @Override
    public void deleteFile(String filePath) {
        try {
            super.deleteFile(filePath);
        } catch (FdfsServerException e) {
            if (e.getErrorCode() == ErrorCodeConstants.ERR_NO_ENOENT) {
                log.info("文件已删除[{}]", filePath);
            } else {
                throw e;
            }
        }
    }

    private String getExtension(String fileName) {
        if (fileName != null && fileName.length() > 0 && fileName.indexOf(".") > -1) {
            int i = fileName.indexOf(".");
            String end = fileName.substring(i + 1);
            if (end.length() > 6) {
                return end.substring(0, 6);
            } else {
                return end;
            }
        }
        return "";
    }
}

增强 - 重试

@Slf4j
@Service
public class FastDfsRetryServiceImpl implements FastDfsRetryService {
    @Recover
    public Object recover(RuntimeException e) {
        log.error("重试{}次,依然失败[{}]", 5, e.getMessage());
        return null;
    }

    @Override
    @Retryable(value = RuntimeException.class, maxAttempts = 5, backoff = @Backoff(delay = 3000L, multiplier = 1.5))
    public Object around(ProceedingJoinPoint joinPoint) throws RuntimeException {
        try {
            return joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}

FastDfsAop.java

@Slf4j
@Aspect
@Component
public class FastDfsAop {
    @Resource
    private FastDfsRetryService fastDfsRetryService;

    @Pointcut("execution(* com.guotie.dpc.fastdfs.service.FastDfsService.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        try {
            return fastDfsRetryService.around(joinPoint);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }
}

yml

fastdfs:
  so-timeout: 1501
  connect-timeout: 601
  default-group: group0
  tracker-list:            #TrackerList参数,支持多个
    - 10.2.2.168:22122
    - 10.2.2.169:22122
  pool:
    #从池中借出的对象的最大数目(配置为-1表示不限制)
    max-total: -1
    #获取连接时的最大等待毫秒数(默认配置为5秒)
    max-wait-millis: 5000
    #每个key最大连接数
    max-total-per-key: 50
    #每个key对应的连接池最大空闲连接数
    max-idle-per-key: 10
    #每个key对应的连接池最小空闲连接数
    min-idle-per-key: 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值