依赖(用的其他人传的老版本的):
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
上传
测试代码:
@SpringBootTest
class FastdfsdemoApplicationTests {
@Test
void contextLoads() throws IOException, MyException {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
NameValuePair[] pairs = null;
String fileId = storageClient1.upload_file1(
"C:\\cyrZkTcSj6-c0.jpg", "jpg", pairs);
System.out.println(fileId);
}
}
运行结果:
group1/M00/00/00/wKhPg2BOF0-AGBo3AAMQ7sedL08948.jpg
实际上传位置:
结合 Spring MultipartFile 进行上传:
public static String upload(MultipartFile file) {
String oldName = file.getOriginalFilename();
try {
return storageClient1.upload_file1(file.getBytes(), oldName.substring(oldName.lastIndexOf(".") + 1), null);
} catch (IOException e) {
log.error("xxx", e)
} catch (MyException e) {
log.error("xxx", e)
}
return null;
}
下载
@Test
void download() throws IOException, MyException {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKhPg2BOIdmAdmeRAAMQ7sedL08168.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\xxx\\Downloads\\test.jpg"));
fileOutputStream.write(bytes);
fileOutputStream.close();
}
结果:
获得文件信息
@Test
void testFileInfo() throws IOException, MyException {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
// 以下两种方法效果一样
System.out.println(storageClient1.get_file_info("group1", "M00/00/00/wKhPg2BTAsWAM_9gAAT2YBE-JU0174.jpg"));
System.out.println(storageClient1.get_file_info1("group1/M00/00/00/wKhPg2BTAsWAM_9gAAT2YBE-JU0174.jpg"));
}
输出:
source_ip_addr = 192.168.79.131, file_size = 325216, create_timestamp = 2020-02-18 15:35:33, crc32 = xx
source_ip_addr = 192.168.79.131, file_size = 325216, create_timestamp = 2020-02-18 15:35:33, crc32 = xx
获得文件 meta data
获得的是 NameValuePair
数组,其实就是上传文件的第三个参数。
NameValuePair[] metadata1 = storageClient1.get_metadata1("group1/M00/00/00/wKhPg2BTAsWAM_9gAAT2YBE-JU0174.jpg");
删除文件
返回 0 则为删除成功:
@Test
void testDeleteFile() throws IOException, MyException {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
System.out.println(storageClient1.delete_file1("group1/M00/00/00/wKhPg2BTAsWAM_9gAAT2YBE-JU0174.jpg"));
}
清除浏览器缓存后再通过 token 访问该图片,Nginx 会返回 404。