使用FastDfs作檔案伺服器
阿新 • • 發佈:2018-11-27
使用FastDfs作檔案伺服器
簡介
FastDfs是一款優秀的分散式開原始檔儲存系統,以下就此係統在java專案中的應用做簡單介紹。
FastDfs在CentOS7系統中的部署
Java專案中的應用
pom.xml中新增依賴
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
resources目錄下新增配置檔案fdfs_client.conf
connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9999 http.anti_steal_token = no http.secret_key = FastDFS1234567890 tracker_server = 120.0.0.1:22122
封裝FastDfs檔案類FastDfsFile.java
import java.io.Serializable;
import lombok.Data;
/**
* FastDfs檔案封裝
* @author: Iffie
* @date: 2018年11月21日
*/
@Data
public class FastDfsFile implements Serializable{
private static final long serialVersionUID = 1888532385762169306L;
private String name;
private byte[] content;
private String ext;
private String md5;
private String author;
public FastDfsFile(String name, byte[] content, String ext) {
super();
this.name = name;
this.content = content;
this.ext = ext;
}
public FastDfsFile() {
super();
}
}
封裝FastDfs操作工具類FastDfsClient.java
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
/**
* FastDfs工具類
* @author: Iffie
* @date: 2018年11月21日
*/
@Slf4j
public class FastDfsClient {
private static TrackerClient trackerClient;
private static TrackerServer trackerServer;
private static StorageServer storageServer;
private static StorageClient storageClient;
static {
try {
String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
ClientGlobal.init(filePath);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);
} catch (Exception e) {
log.error("FastDfsClient Init Fail!",e);
}
}
/**
* 上傳
*/
public static String[] upload(FastDfsFile file) throws Exception {
log.info("上傳檔案開始,檔名:{}",file.getName());
NameValuePair[] metaList = new NameValuePair[1];
metaList[0] = new NameValuePair("author", file.getAuthor());
long startTime = System.currentTimeMillis();
String[] uploadResults = null;
storageClient = new StorageClient(trackerServer, storageServer);
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
log.info("上傳檔案耗時:{}毫秒",(System.currentTimeMillis() - startTime));
if(uploadResults != null) {
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];
log.info("上傳檔案成功,groupName:{},remoteFileName:{}",groupName,remoteFileName);
return uploadResults;
}
log.error("上傳檔案失敗,錯誤碼:{}",storageClient.getErrorCode());
throw new Exception("上傳檔案失敗");
}
/**
* 根據groupName和檔名獲取檔案資訊
*/
public static FileInfo getFile(String groupName, String remoteFileName) {
try {
storageClient = new StorageClient(trackerServer, storageServer);
return storageClient.get_file_info(groupName, remoteFileName);
}catch (Exception e) {
log.error("獲取檔案資訊異常", e);
}
return null;
}
/**
* 下載
*/
public static InputStream downFile(String groupName, String remoteFileName) {
try {
storageClient = new StorageClient(trackerServer, storageServer);
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
InputStream ins = new ByteArrayInputStream(fileByte);
return ins;
}catch (Exception e) {
log.error("下載檔案異常", e);
}
return null;
}
/**
* 刪除
*/
public static void deleteFile(String groupName, String remoteFileName)
throws Exception {
storageClient = new StorageClient(trackerServer, storageServer);
int i = storageClient.delete_file(groupName, remoteFileName);
log.info("刪除檔案成功" + i);
}
/**
* MultipartFile上傳
*/
public static String saveFile(MultipartFile multipartFile) throws Exception {
String[] fileAbsolutePath={};
String fileName=multipartFile.getOriginalFilename();
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
byte[] fileBuff = null;
InputStream inputStream=multipartFile.getInputStream();
if(inputStream!=null){
int len1 = inputStream.available();
fileBuff = new byte[len1];
inputStream.read(fileBuff);
}
inputStream.close();
FastDfsFile file = new FastDfsFile(fileName, fileBuff, ext);
fileAbsolutePath = FastDfsClient.upload(file);
if(fileAbsolutePath!=null) {
return fileAbsolutePath[0]+ "/"+fileAbsolutePath[1];
}
log.error("上傳失敗");
throw new Exception("上傳失敗");
}
}