当前位置:网站首页>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
边栏推荐
猜你喜欢
ARM 汇编基础
总结篇4:redis 核心数据存储结构及核心业务模型实现应用场景
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
ASP.NET Core依赖注入之旅:针对服务注册的验证
Metasploit——辅助模块(Auxiliary)
The senior told me that the MySQL of the big factory is connected through SSH
手写flexible.js的原理实现,我终于明白移动端多端适配
An in-depth understanding of the implementation principle of Hybrid
我不写单元测试,被批了
基于AWS构建云上数仓第一步:云平台的基础概念
随机推荐
偷偷盘点一下各大互联网公司的实习薪资
从事软件测试一年,只会基础的功能测试,怎么进一步学习?
ARM 汇编基础
shared usage in d
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
The senior told me that the MySQL of the big factory is connected through SSH
虚拟修补:您需要知道的一切
d中简单禁止垃集
C#介绍及基本数据类型
如何在 PC 机上测试移动端的网页?
动态RDLC报表(三)
openEuler Xiong Wei: How do you view the SIG organization model in the open source community?
Ark Standalone/Administrator Special Item Command Codes
win10 uwp 手动锁Bitlocker
使用mysql:5.6和 owncloud 镜像,构建一个个人网盘
LeetCode做题小结
About the common Hook encapsulation of DOM (2)
秋招面试大厂总被刷下来,你这样做保准你事半功倍!
MySQL索引的B+树到底有多高?
艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季