当前位置:网站首页>fastdfs-client使用
fastdfs-client使用
2022-08-09 17:18:00 【wfsm】
官方:https://github.com/tobato/FastDFS_Client
springboot集成:https://blog.csdn.net/wzl19870309/article/details/74049204
主要接口包括
TrackerClient - TrackerServer接口
GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
配置文件:
fdfs:
connect-timeout: 60000
so-timeout: 60000
tracker-list: xxx
pool:
## 连接池最大数量
max-total: 200
## 每个tracker地址的最大连接数
max-total-per-key: 50
## 连接耗尽时等待获取连接的最大毫秒数
max-wait-millis: 60000
web-server-url: xxx
package com.cj.fdfs;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.StandardCharsets;
@Component
public class FastDFSClient {
private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
/** * 上传文件 * @param file */
public String uploadFile(MultipartFile file) throws IOException {
System.out.println(file.getOriginalFilename());
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
// FilenameUtils
return getResAccessUrl(storePath);
}
public String uploadFile(File file) throws FileNotFoundException {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return getResAccessUrl(storePath);
}
/** * 将一段字符串 生成一个 文件 上传 * @param content * @param fileExtension */
public String uploadFile(String content,String fileExtension){
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
StorePath storePath = storageClient.uploadFile(inputStream, bytes.length, fileExtension, null);
return getResAccessUrl(storePath);
}
/** * 获取上传后的 url * @param storePath */
public String getResAccessUrl(StorePath storePath){
String fileUrl = fdfsWebServer.getWebServerUrl()+ "/" + storePath.getFullPath();
return fileUrl;
}
/** * 删除文件 * @param fileUrl */
public void deleteFile(String fileUrl){
if (StringUtils.isEmpty(fileUrl)){
return;
}
try {
// 存储文件的路径信息
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(),storePath.getPath());
} catch (Exception e) {
logger.warn(e.getMessage());
}
}
}
测试:
@Test
void contextLoads() throws IOException {
Path path = Paths.get("F:\\1.png");
File file = new File(path.toUri());
MockMultipartFile multipartFile = new MockMultipartFile(file.getName(),file.getName(),null, new FileInputStream(file));
String s = fastDFSClient.uploadFile(multipartFile);
System.out.println("s = " + s);
}
遇到的问题:
- 怎么模拟一个
MultipartFileMockMultipartFile
引用:https://blog.csdn.net/qq_37157160/article/details/104512457
边栏推荐
- LINE Verda Programming Contest (AtCoder Beginner Contest 263) A~E 题解
- What platform is EPIC?
- 混动产品助力,自主SUV市场格局迎来新篇章
- Discuz! Forum program installation + template configuration tutorial
- MySQL数据指令
- win10 uwp 装机必备应用 含源代码
- 我不写单元测试,被批了
- 【工业数字化大讲堂 第二十期】制造业数字化能力建设分享,特邀制造业高级咨询顾问 李东老师分享
- 测试开发是什么,为什么现在这么吃香?
- [Pycharm easy to use function]
猜你喜欢

华为发布「国产Copilot内核」PanGu-Coder,而且真的能用中文哦!

Tan Zhongyi: Do you know who the "Queen of Open Source" is?

逻辑越权和水平垂直越权支付篡改,验证码绕过,接口

100+开箱即用的AI工具箱;程序员150岁长寿指南;『地理空间数据科学』课程资料;Graphic数据可视化图表库;前沿论文 | ShowMeAI资讯日报

论文解读:Deep-4MCW2V:基于序列的预测指标,以鉴定大肠杆菌中的N4-甲基环胞嘧啶位点

JSDN博客系统

【Pycharm好用功能】

方舟开服务器Vmware虚拟机安装不上?

approach和method的区别

有什么好的开源自动化测试框架可以推荐?
随机推荐
使用mysql:5.6和 owncloud 镜像,构建一个个人网盘
从事软件测试一年,只会基础的功能测试,怎么进一步学习?
JSDN博客系统
Volatile:JVM 我警告你,我的人你别乱动
对数学直观、感性的认知是理解数学、喜爱数学的必经之路,这本书做到了!
InfluxDB语法
Cortex-A7 MPCore Architecture
The most complete architect knowledge map in history
Discuz! Forum program installation + template configuration tutorial
How tall is the B+ tree of the MySQL index?
JVM:(八)运行时数据区之方法区
动态RDLC报表(三)
混动产品助力,自主SUV市场格局迎来新篇章
d中简单禁止垃集
毕昇编译器优化:Lazy Code Motion
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
自学软件测试,学到什么程度可以出去找工作啊?
那些关于DOM的常见Hook封装(二)
搭建Zabbix监控系统
How to play with container local storage through open-local? | Dragon Lizard Technology