1.文件上傳流程
1、客戶端訪問Tracker
2、Tracker 返回Storage的ip和端口
3、客戶端直接訪問Storage,把文件內容和元數據發送過去。
4、Storage返回文件存儲id。包含了組名和文件名
2.文件下載流程
1、client詢問tracker下載文件的storage,參數為文件標識(組名和文件名);
2、tracker返回一台可用的storage;
3、client直接和storage通訊完成文件下載。
3.JavaClinet
1、 添加依賴
<dependencies>
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
2、編寫配置文件
文件名:fdfs_client.conf,修改成自己的tracker服務器ip
connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
tracker_server = 192.168.93.10:22122
3、工具類
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
/**
* FastDFS分布式文件系統操作客戶端.
*/
public class FastDFSClient {
private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "fdfs_client.conf";
private static StorageClient storageClient = null;
/**
* 只加載一次.
*/
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param inputStream
* 上傳的文件輸入流
* @param fileName
* 上傳的文件原始名
* @return
*/
public static String[] uploadFile(InputStream inputStream, String fileName) {
try {
// 文件的元數據
NameValuePair[] meta_list = new NameValuePair[2];
// 第一組元數據,文件的原始名稱
meta_list[0] = new NameValuePair("file name", fileName);
// 第二組元數據
meta_list[1] = new NameValuePair("file length", inputStream.available()+"");
// 准備字節數組
byte[] file_buff = null;
if (inputStream != null) {
// 查看文件的長度
int len = inputStream.available();
// 創建對應長度的字節數組
file_buff = new byte[len];
// 將輸入流中的字節內容,讀到字節數組中。
inputStream.read(file_buff);
}
// 上傳文件。參數含義:要上傳的文件的內容(使用字節數組傳遞),上傳的文件的類型(擴展名),元數據
String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list);
return fileids;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
*
* @param file
* 文件
* @param fileName
* 文件名
* @return 返回Null則為失敗
*/
public static String[] uploadFile(File file, String fileName) {
FileInputStream fis = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
fis = new FileInputStream(file);
byte[] file_buff = null;
if (fis != null) {
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
String[] fileids = storageClient.upload_file(file_buff, getFileExt(fileName), meta_list);
return fileids;
} catch (Exception ex) {
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 根據組名和遠程文件名來刪除一個文件
*
* @param groupName
* 例如 "group1" 如果不指定該值,默認為group1
* @param remoteFileName
* 例如"M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg"
* @return 0為成功,非0為失敗,具體為錯誤代碼
*/
public static int deleteFile(String groupName, String remoteFileName) {
try {
int result = storageClient.delete_file(groupName == null ? "group1" : groupName, remoteFileName);
return result;
} catch (Exception ex) {
return 0;
}
}
/**
* 修改一個已經存在的文件
*
* @param oldGroupName
* 舊的組名
* @param oldFileName
* 舊的文件名
* @param file
* 新文件
* @param fileName
* 新文件名
* @return 返回空則為失敗
*/
public static String[] modifyFile(String oldGroupName, String oldFileName, File file, String fileName) {
String[] fileids = null;
try {
// 先上傳
fileids = uploadFile(file, fileName);
if (fileids == null) {
return null;
}
// 再刪除
int delResult = deleteFile(oldGroupName, oldFileName);
if (delResult != 0) {
return null;
}
} catch (Exception ex) {
return null;
}
return fileids;
}
/**
* 文件下載
*
* @param groupName 卷名
* @param remoteFileName 文件名
* @return 返回一個流
*/
public static InputStream downloadFile(String groupName, String remoteFileName) {
try {
byte[] bytes = storageClient.download_file(groupName, remoteFileName);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
return null;
}
}
public static NameValuePair[] getMetaDate(String groupName, String remoteFileName){
try{
NameValuePair[] nvp = storageClient.get_metadata(groupName, remoteFileName);
return nvp;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
/**
* 獲取文件后綴名(不帶點).
*
* @return 如:"jpg" or "".
*/
private static String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1); // 不帶最后的點
}
}
}
4、上傳文件
public class MyMain {
public static void main(String[] args) {
try {
// 上次文件
File file = new File("D:/b.png");
InputStream is = new FileInputStream(file);
String fileName = UUID.randomUUID().toString()+".png";
String[] result = FastDFSClient.uploadFile(is, fileName);
System.out.println(Arrays.toString(result));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
5.下載文件
public class MyMain {
public static void main(String[] args) {
try {
InputStream is = FastDFSClient.downloadFile("group1", "M00/00/00/wKg0gF3zAKCARs6kAAASjQVYlWA098.png");
OutputStream os = new FileOutputStream(new File("D:/jqk.png"));
int index = 0 ;
while((index = is.read())!=-1){
os.write(index);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}