fastFDS测试
非springboot整合jar包-fastdfs_client_v1.20地址:
首先准备一张测试图片:
fdfs_client.conf:
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 127.0.0.1:22122
上传例子:
//上传文件
@Test
public void testUpload() {
try {
ClientGlobal.init(conf_filename);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
NameValuePair nvp [] = new NameValuePair[]{
new NameValuePair("item_id", "100010"),
new NameValuePair("width", "80"),
new NameValuePair("height", "90")
};
String fileIds[] = storageClient.upload_file(local_filename, null,
nvp);
System.out.println("组名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
效果:
下载例子:
//客户端配置文件
public String conf_filename = "F:\\fastdfsClient\\src\\cn\\itcast\\fastdfs\\cliennt\\fdfs_client.conf";
//本地文件,要上传的文件
public String local_filename = "F:\\image\\yangzhenyu.png";
//下载文件
@Test
public void testDownload() {
try {
ClientGlobal.init(conf_filename);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
byte[] b = storageClient.download_file("group1",
"M00/00/00/CjMgJV8OoLqAJMN0AAFo6gRUELQ074.png");
if(b !=null){
saveFile(b, "F:\\image\\temp\\", UUID.randomUUID().toString()+".jpg");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//将字节流写到磁盘生成文件
private void saveFile(byte[] b, String path, String fileName) {
File file = new File(path+fileName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream= new FileOutputStream(file);
fileOutputStream.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
效果: