1、maven項目中,在pom中添加 fastdfs_client 依賴
-
#添加依賴
-
<dependency>
-
<groupId>org.csource </groupId>
-
<artifactId>fastdfs-client-java </artifactId>
-
<version>5.0.4 </version>
-
</dependency>
-
-
#說明:
-
#由於fastdfs-client-java jar包沒有上傳到中央倉庫,所以需要下載源碼進行maven編譯再上傳到私服,然
-
#后通過私服下載或者通過jar包的形式進行下載
2、在src/main/resource 下創建 fdfs_client.conf 配置文件
-
#fdfs_client.conf 配置
-
connect_timeout = 10
-
network_timeout = 30
-
charset = UTF- 8
-
http.tracker_http_port = 8888
-
http.anti_steal_token = no
-
http.secret_key = FastDFS123456789 0
-
-
tracker_server = 100.98. 22.253: 22122
-
#如果有多台服務,指定集群的IP
-
#tracker_server = 192.168.10.250:22122
3、客戶端代碼實例
-
package util;
-
-
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.apache.log4j.Logger;
-
import org.csource.common.NameValuePair;
-
import org.csource.fastdfs.ClientGlobal;
-
import org.csource.fastdfs.StorageClient1;
-
import org.csource.fastdfs.StorageServer;
-
import org.csource.fastdfs.TrackerClient;
-
import org.csource.fastdfs.TrackerGroup;
-
import org.csource.fastdfs.TrackerServer;
-
-
/**
-
*
-
* <B>系統名稱:</B><BR>
-
* <B>模塊名稱:</B><BR>
-
* <B>中文類名:</B>FastDFS分布式文件系統操作客戶端<BR>
-
* <B>概要說明:</B>FastDFS分布式文件系統操作客戶端<BR>
-
* @author bhz
-
* @since 2013年11月10日
-
*/
-
public class FastDFSClientUtils {
-
-
private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource( "fastdfs_client.conf").getPath();
-
-
private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);
-
-
-
private static TrackerClient trackerClient;
-
-
-
//加載文件
-
static {
-
try {
-
ClientGlobal.init(CONF_FILENAME);
-
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
-
trackerClient = new TrackerClient(trackerGroup);
-
} catch (Exception e) {
-
logger.error(e);
-
}
-
}
-
-
/**
-
* <B>方法名稱:</B>上傳方法<BR>
-
* <B>概要說明:</B><BR>
-
* @param file 文件
-
* @param path 路徑
-
* @return 上傳成功返回id,失敗返回null
-
*/
-
public static String upload(File file, String path) {
-
TrackerServer trackerServer = null;
-
StorageServer storageServer = null;
-
StorageClient1 storageClient1 = null;
-
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);
-
}
-
-
trackerServer = trackerClient.getConnection();
-
if (trackerServer == null) {
-
logger.error( "getConnection return null");
-
}
-
storageServer = trackerClient.getStoreStorage(trackerServer);
-
storageClient1 = new StorageClient1(trackerServer, storageServer);
-
String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
-
-
return fileid;
-
} catch (Exception ex) {
-
logger.error(ex);
-
return null;
-
} finally{
-
if (fis != null){
-
try {
-
fis.close();
-
} catch (IOException e) {
-
logger.error(e);
-
}
-
}
-
if (storageServer != null){
-
try {
-
storageServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if (trackerServer != null){
-
try {
-
trackerServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
storageClient1 = null;
-
}
-
}
-
-
/**
-
* <B>方法名稱:</B>上傳方法<BR>
-
* <B>概要說明:</B><BR>
-
* @param data 數據
-
* @param path 路徑
-
* @return 上傳成功返回id,失敗返回null
-
*/
-
public static String upload(byte[] data, String extName) {
-
TrackerServer trackerServer = null;
-
StorageServer storageServer = null;
-
StorageClient1 storageClient1 = null;
-
try {
-
NameValuePair[] meta_list = null; // new NameValuePair[0];
-
-
trackerServer = trackerClient.getConnection();
-
if (trackerServer == null) {
-
logger.error( "getConnection return null");
-
}
-
storageServer = trackerClient.getStoreStorage(trackerServer);
-
storageClient1 = new StorageClient1(trackerServer, storageServer);
-
String fileid = storageClient1.upload_file1(data, extName, meta_list);
-
return fileid;
-
} catch (Exception ex) {
-
logger.error(ex);
-
return null;
-
} finally{
-
if (storageServer != null){
-
try {
-
storageServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if (trackerServer != null){
-
try {
-
trackerServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
storageClient1 = null;
-
}
-
}
-
-
/**
-
* <B>方法名稱:</B>下載方法<BR>
-
* <B>概要說明:</B>通過文件id進行下載<BR>
-
* @param fileId 文件id
-
* @return 返回InputStream
-
*/
-
public static InputStream download(String groupName, String fileId) {
-
TrackerServer trackerServer = null;
-
StorageServer storageServer = null;
-
StorageClient1 storageClient1 = null;
-
try {
-
trackerServer = trackerClient.getConnection();
-
if (trackerServer == null) {
-
logger.error( "getConnection return null");
-
}
-
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
-
storageClient1 = new StorageClient1(trackerServer, storageServer);
-
byte[] bytes = storageClient1.download_file1(fileId);
-
InputStream inputStream = new ByteArrayInputStream(bytes);
-
return inputStream;
-
} catch (Exception ex) {
-
logger.error(ex);
-
return null;
-
} finally {
-
if (storageServer != null){
-
try {
-
storageServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if (trackerServer != null){
-
try {
-
trackerServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
storageClient1 = null;
-
}
-
}
-
-
/**
-
* <B>方法名稱:</B>刪除方法<BR>
-
* <B>概要說明:</B>根據id來刪除一個文件<BR>
-
* @param fileId 文件id
-
* @return 刪除成功返回0,非0則操作失敗,返回錯誤代碼
-
*/
-
public static int delete(String groupName, String fileId) {
-
TrackerServer trackerServer = null;
-
StorageServer storageServer = null;
-
StorageClient1 storageClient1 = null;
-
try {
-
trackerServer = trackerClient.getConnection();
-
if (trackerServer == null) {
-
logger.error( "getConnection return null");
-
}
-
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
-
storageClient1 = new StorageClient1(trackerServer, storageServer);
-
int result = storageClient1.delete_file1(fileId);
-
return result;
-
} catch (Exception ex) {
-
logger.error(ex);
-
return 0;
-
} finally {
-
if (storageServer != null){
-
try {
-
storageServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if (trackerServer != null){
-
try {
-
trackerServer.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
storageClient1 = null;
-
}
-
}
-
-
/**
-
* <B>方法名稱:</B><BR>
-
* <B>概要說明:</B><BR>
-
* @param oldFileId 舊文件id
-
* @param file 新文件
-
* @param path 新文件路徑
-
* @return 上傳成功返回id,失敗返回null
-
*/
-
public static String modify(String oldGroupName, String oldFileId, File file, String path) {
-
String fileid = null;
-
try {
-
// 先上傳
-
fileid = upload(file, path);
-
if (fileid == null) {
-
return null;
-
}
-
// 再刪除
-
int delResult = delete(oldGroupName, oldFileId);
-
if (delResult != 0) {
-
return null;
-
}
-
} catch (Exception ex) {
-
logger.error(ex);
-
return null;
-
}
-
return fileid;
-
}
-
-
/**
-
* <B>方法名稱:</B>獲取文件后綴名<BR>
-
* <B>概要說明:</B>獲取文件后綴名<BR>
-
* @param fileName
-
* @return 如:"jpg"、"txt"、"zip" 等
-
*/
-
private static String getFileExt(String fileName) {
-
if (StringUtils.isBlank(fileName) || !fileName.contains( ".")) {
-
return "";
-
} else {
-
return fileName.substring(fileName.lastIndexOf( ".") + 1);
-
}
-
}
-
}
4、測試代碼
-
package util;
-
-
import java.io.File;
-
import java.io.InputStream;
-
-
import org.apache.commons.io.FileUtils;
-
-
-
-
public class FastDFSTest {
-
-
/**
-
* 上傳
-
*/
-
public static void upload() throws Exception {
-
// id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
-
// fastdfsBasePath = http://192.168.1.170/fastdfs
-
// url: http://192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
-
String path = System.getProperty( "user.dir") + File.separatorChar + "source" + File.separatorChar + "001.jpg";
-
File file = new File(path);
-
String fileId = FastDFSClientUtils.upload(file, path);
-
System.out.println( "本地文件:" + path + ",上傳成功! 文件ID為:" + fileId);
-
}
-
-
/**
-
* 下載
-
*/
-
public static void download() throws Exception {
-
// id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
-
// url: http://192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
-
String fileId = "group1/M00/00/00/wKgBr1crUIuAJ22iAADHTIxNXeI872.jpg";
-
InputStream inputStream = FastDFSClientUtils.download( "group1", fileId);
-
System.out.println(inputStream.available());
-
String path = System.getProperty( "user.dir") + File.separatorChar + "receive" + File.separatorChar + "001.jpg";
-
System.out.println(path);
-
//FileUtils.copyInputStreamToFile(inputStream, new File(path));
-
}
-
-
/**
-
* 刪除
-
*/
-
public static void delete() throws Exception {
-
String fileId = "group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg";
-
int result = FastDFSClientUtils.delete( "group1", fileId);
-
System.out.println(result == 0 ? "刪除成功" : "刪除失敗:" + result);
-
}
-
-
-
-
/**
-
* @param args
-
* @throws Exception
-
*/
-
public static void main(String[] args) throws Exception {
-
-
-
//upload();
-
download();
-
Thread.sleep( 10000);
-
download();
-
Thread.sleep( 10000);
-
download();
-
//delete();
-
-
}
-
-
}