当前位置:网站首页>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);
}
遇到的问题:
- 怎么模拟一个
MultipartFile
MockMultipartFile
引用:https://blog.csdn.net/qq_37157160/article/details/104512457
边栏推荐
- Jenkins deploys services to remote servers using pipelines
- win10 uwp 绑定静态属性
- [SUCTF 2019]CheckIn
- 偷偷盘点一下各大互联网公司的实习薪资
- 方舟:生存进化开服务器端口映射教程
- 国际土壤模型协会 International Soil Modeling Consortium-ISMC
- 动手学深度学习_风格迁移
- 我不写单元测试,被批了
- Wallys/QCA 9880/802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz advanced edition
- Can't install the Vmware virtual machine on the Ark Kai server?
猜你喜欢
JSDN blog system
IMX6ULL—Assembly LED Lights
Detailed explanation of JVM memory model and structure (five model diagrams)
What is test development and why is it so popular now?
使用mysql:5.6和 owncloud 镜像,构建一个个人网盘
动态RDLC报表(七)
读大学有用吗?
进行知识管理的好处有哪些?
史上最全架构师知识图谱
The most complete architect knowledge map in history
随机推荐
win10 uwp 让焦点在点击在页面空白处时回到textbox中
The strongest distributed lock tool: Redisson
史上最全架构师知识图谱
IMX6ULL—汇编LED灯
对数学直观、感性的认知是理解数学、喜爱数学的必经之路,这本书做到了!
我不写单元测试,被批了
Jenkins deploys services to remote servers using pipelines
一口气拿下6个大厂offer,是什么水平
The difference between approach and method
HarmonyOS - 基于ArkUI (JS) 实现图片旋转验证
URLError: <urlopen error [Errno 11004] getaddrinfo failed>调用seaborn-data无法使用
最强分布式锁工具:Redisson
CPU状态信息us,sy,ni,id,wa,hi,si,st含义
动态RDLC报表(一)
低代码平台和专业开发人员——完美搭档?
OpenCV 轮廓 —— 轮廓匹配
动态RDLC报表(六)
怎样选择一个好的SaaS知识库工具?
传统数据中台又贵又复杂?何不试一试永久免费的下一代数据中台
学长告诉我,大厂MySQL都是通过SSH连接的