[Java] 纯文本查看 复制代码
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestFastDFS {
//上传文件
@Test
public void testUpload(){
try {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取Stroage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建stroageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
//向stroage服务器上传文件
//本地文件的路径
String filePath = "d:/logo.png";
//上传成功后拿到文件Id
String fileId = storageClient1.upload_file1(filePath, "png", null);
System.out.println(fileId);
//group1/M00/00/01/wKhlQVuhU3eADb4pAAAawU0ID2Q159.png
} catch (Exception e) {
e.printStackTrace();
}
}
//下载文件
@Test
public void testDownload(){
try {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取Stroage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建stroageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
//下载文件
//文件id
String fileId = "group1/M00/00/01/wKhlQVuhU3eADb4pAAAawU0ID2Q159.png";
byte[] bytes = storageClient1.download_file1(fileId);
//使用输出流保存文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("c:/logo.png"));
fileOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
}