下载操作实际通过url就可以直接下载:一般通过在storage服务器上安装nginx来访问
搭建过程:https://blog.csdn.net/qq_41813208/article/details/103286576
其中
配置文件信息的部分截图:安装文章前面找一下
和前面类似分四步:
- 初始化配置文件
- 通过配置文件 和 trackerClient 连接tracker获取trackerServer
- 通过trackerServer来获取StorageClient对象
- 文件操作(查询,上传,下载)
记得要导入对应的包
@Test
void testDownload() {
try {
//1.初始化fastdfs客户端配置文件
ClientGlobal.initByProperties("fastdfs/fdfs.properties");// 加载properties配置文件
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");//输出properties中配置的参数,检测properties文件是否生效
System.out.println("charset=" + ClientGlobal.g_charset);
// 2.获取trackerServer
TrackerClient tracker = new TrackerClient();// 创建tracker客户端对象
TrackerServer trackerServer = tracker.getConnection();//获得tracker连接对象
//3.获取storageClient(通过trackerServer 和 storageServer:null)
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);//通过tracker服务器返回storage服务器对象(客户端)
//4.下载操作
//方式一 文件id下载
byte[] bytes = client.download_file1("group1/M00/00/00/wKgByl3hXDiAR662AAFLbNXkMmY992.jpg");//记得要传文件id返回字节流
File file=new File("/home/angel/test.jpg");// 给定文件路径 和 名称
FileOutputStream fileOutputStream=new FileOutputStream(file);//窗机输出流
fileOutputStream.write(bytes);//写入数据
fileOutputStream.close();//关闭输出流
//方式二 组名+文件路径
byte[] bytes2 = client.download_file("group1","M00/00/00/wKgByl3hXDiAR662AAFLbNXkMmY992.jpg");//记得修改
File file2=new File("/home/angel/test2.jpg");// 给定文件路径 和 名称
FileOutputStream fileOutputStream2=new FileOutputStream(file2);//窗机输出流
fileOutputStream2.write(bytes);//写入数据
fileOutputStream2.close();//关闭输出流
trackerServer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
其它操作在我的fastdfs专栏找一下https://blog.csdn.net/qq_41813208/category_9535310.html