FastDFS(三)Java实现文件上传下载
<dependencies>
<!--fastdfs的java客户端-->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
在resources下创建config目录,在config目录下创建 fastdfs-client.properties,内容如下:
##fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = IP:22122
文件上传
public class TestUpload {
public static void main(String[] args) {
try {
//加载配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//通过客户端获取tracker的连接服务并返回
TrackerServer trackerServer = trackerClient.getConnection();
//声明storage服务
StorageServer storageServer = null;
//定义文件元信息
NameValuePair[] list = new NameValuePair[1];
list[0] = new NameValuePair("fileName","1.png");
//定义storage客户端
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
String fileId = client.upload_appender_file1("E:\\daimashangchuan\\course-notes\\第七阶段\\FastDFS\\1.png", "png", list);
// group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png
/*
group1:一台服务器,就是一个组
M00: store_path0 ----> /home/fastdfs/fdfs_storage/data
00/00:两级数据目录
*/
System.out.println(fileId);
//关闭客户端
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
}
文件查询
public class TestQuery {
public static void main(String[] args) {
try {
//加载配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//通过客户端获取tracker的连接服务并返回
TrackerServer trackerServer = trackerClient.getConnection();
//声明storage服务
StorageServer storageServer = null;
//定义storage客户端
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
String string = "group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png";
//查询信息
FileInfo fileInfo = client.query_file_info1(string);
System.out.println("fileInfo:" + fileInfo);
//关闭客户端
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
}
文件下载
public class TestDownload {
public static void main(String[] args) {
try {
//加载配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//通过客户端获取tracker的连接服务并返回
TrackerServer trackerServer = trackerClient.getConnection();
//声明storage服务
StorageServer storageServer = null;
//定义storage客户端
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
String string = "group1/M00/00/00/ChcDu2ALonSEO3NvAAAAAIy3Q2g733.png";
//下载
byte[] bytes = client.download_file1(string);
//将字节数组转换为文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\11111.png"));
fileOutputStream.write(bytes);
//关闭流
fileOutputStream.close();
//关闭客户端
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
}