微服务 教程
1085个小节阅读:195.4k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
由GitHub大牛tobato在原作者YuQing与yuqih发布的JAVA客户端基础上进行了大量重构工作,并于GitHub上发布了FastDFS-Client1.26.5。
xxxxxxxxxx
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
xpackage com.demo;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
//===================================================================
//获取带有连接池的FastDFS Java客户端
FdfsClientConfig.class) (
// 解决jmx重复注册bean的问题
registration = RegistrationPolicy.IGNORE_EXISTING) (
//===================================================================
public class FastdfsSpringbootDriverApplication {
public static void main(String[] args) {
SpringApplication.run(FastdfsSpringbootDriverApplication.class, args);
}
}
xxxxxxxxxx
===================================================================
# 分布式文件系统FDFS配置
===================================================================
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.66.100:22122
- 192.168.66.101:22122
xxxxxxxxxx
/**
* 测试springboot环境下的javaAPI对分布式文件系统的上传文件的操作
* @throws FileNotFoundException
*/
@Test
public void testUpload() throws FileNotFoundException {
//获取本地文件
File file = new File("G:\\图片\\mei.jpg");
//创建传输文件的输入流
FileInputStream fileInputStream = new FileInputStream(file);
//文件上传:参数一:传输文件内容的输入流;参数二:文件的size;参数三:文件扩展名;参数四:描述文件的元数据;返回值:上传文件在存储节点的唯一标识(卷名+文件名)
StorePath storePath = fastFileStorageClient.uploadFile(fileInputStream, file.length(), "jpg", null);
//将卷名与文件名一起打印
System.out.println(storePath.getFullPath());
//将卷名与文件名分别打印
System.out.println(storePath.getGroup()+" | "+storePath.getPath());
}
xxxxxxxxxx
/**
* 测试springboot环境下的javaAPI对分布式文件系统的下载文件的操作
* @throws IOException
*/
@Test
public void testDownload() throws IOException {
//下载文件:参数一:文件处于存储节点的卷名;参数二:文件在存储节点的文件名;参数三:下载的回调函数;返回值:文件内容的字节数组
byte[] bytes = fastFileStorageClient.downloadFile("group1", "M00/00/00/wKhCZWICJcqAftV0AAChCwXlPdE133.jpg", new DownloadByteArray());
//创建文件输出流,指定输出位置及文件名
FileOutputStream fileOutputStream = new FileOutputStream("G:\\mei.jpg");
//使用文件输出流将文件内容字节数组写出
fileOutputStream.write(bytes);
//刷新输出流
fileOutputStream.flush();
//关闭输出流
fileOutputStream.close();
}