3.3.1 添加依赖
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
3.3.2 工具类编写
package com.juanji.download.util;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
String path = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(),"UTF-8");
System.out.println(path);
path=path.substring(6);
conf = conf.replace("classpath:",URLDecoder.decode(path,"UTF-8"));
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
public String uploadFile(String fileName, String extName, NameValuePair[] metas) {
String result=null;
try {
result = storageClient.upload_file1(fileName, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
public String uploadFile(String fileName) {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) {
return uploadFile(fileName, extName, null);
}
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {
String result=null;
try {
result = storageClient.upload_file1(fileContent, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) {
return uploadFile(fileContent, extName, null);
}
public int download_file(String path,BufferedOutputStream output) {
int result=-1;
try {
byte[] b = storageClient.download_file1(path);
try{
if(b != null){
output.write(b);
result=0;
}
}catch (Exception e){}
finally {
if (output != null){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public byte[] download_bytes(String path) {
byte[] b=null;
try {
b = storageClient.download_file1(path);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return b;
}
public Integer delete_file(String group ,String storagePath){
int result=-1;
try {
result = storageClient.delete_file(group, storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
public Integer delete_file(String storagePath){
int result=-1;
try {
result = storageClient.delete_file1(storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
public FileInfo getFile(String groupName,String remoteFileName){
try {
return storageClient.get_file_info(groupName, remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3.3.3 单元测试
package com.juanji.app.liveserver;
import com.juanji.app.util.FastDFSClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FastDFSTest {
@Test
public void fdfsUpload() throws Exception{
String confUrl=this.getClass().getClassLoader().getResource("fdfs_client.properties").getPath();
FastDFSClient fastDFSClient = new FastDFSClient(confUrl);
String filePath = fastDFSClient.uploadFile("C:\\upload\\xx\\20190306\\183118.png");
System.out.println(filePath);
}
@Test
public void del() throws Exception{
String confUrl=this.getClass().getClassLoader().getResource("fdfs_client.properties").getPath();
FastDFSClient fastDFSClient = new FastDFSClient(confUrl);
int i = fastDFSClient.delete_file("group1/M00/00/00/CmVkLVzSTt-AVds1AAWuezrCZFM986.png");
System.out.println(i);
}
}
3.3.4 上传测试效果展示