FastDFS分布文件系统Java客户端集成

参考博客:http://blog.csdn.net/xyang81/article/details/52847311

官网Java客户端源代码:

https://github.com/happyfish100/fastdfs-client-java

从上面地址下载压缩包,解压

FastDFS分布文件系统Java客户端集成

使用idea工具打开执行clean install将包打到本地的maven仓库

FastDFS分布文件系统Java客户端集成

FastDFS分布文件系统Java客户端集成

如果协同开发,可将该包上传到公司的私服,供大家一起下载,就不需要下载该工程进行本地打包,只需要引pom地址

工程目录结构

FastDFS分布文件系统Java客户端集成

根据官方提供的sdk封装了一个工具类

 package com.cky.fastdfsdemo;

 import com.cky.util.FileUtil;
import org.apache.commons.io.IOUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; /**
* Created by chenkaiyang on 2017/11/30.
*/
public class FastDFSClient {
private static final String CONFIG_FILENAME = "src/main/resources/fdfs/fdfs_client.conf";
private static StorageClient1 storageClient1 = null; // 初始化FastDFS Client
static {
try {
ClientGlobal.init(CONFIG_FILENAME);
TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
TrackerServer trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
throw new IllegalStateException("getConnection return null");
} StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
if (storageServer == null) {
throw new IllegalStateException("getStoreStorage return null");
} storageClient1 = new StorageClient1(trackerServer,storageServer); } catch (Exception e) {
e.printStackTrace();
}
} /**
* 上传文件
* @param file 文件对象
* @param fileName 文件名
* @return
*/
public static String uploadFile(File file, String fileName) {
return uploadFile(file,fileName,null);
} /**
* 上传文件
* @param file 文件对象
* @param fileName 文件名
* @param metaList 文件元数据
* @return
*/
public static String uploadFile(File file, String fileName, Map<String,String> metaList) {
try {
byte[] buff = IOUtils.toByteArray(new FileInputStream(file));
NameValuePair[] nameValuePairs = null;
if (metaList != null) {
nameValuePairs = new NameValuePair[metaList.size()];
int index = 0;
for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String,String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
nameValuePairs[index++] = new NameValuePair(name,value);
}
}
return storageClient1.upload_file1(buff, FileUtil.getExtensionName(fileName),nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 获取文件元数据
* @param fileId 文件ID
* @return
*/
public static Map<String,String> getFileMetadata(String fileId) {
try {
NameValuePair[] metaList = storageClient1.get_metadata1(fileId);
if (metaList != null) {
HashMap<String,String> map = new HashMap<String, String>();
for (NameValuePair metaItem : metaList) {
map.put(metaItem.getName(),metaItem.getValue());
}
return map;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 删除文件
* @param fileId 文件ID
* @return 删除失败返回-1,否则返回0
*/
public static int deleteFile(String fileId) {
try {
return storageClient1.delete_file1(fileId);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
} /**
* 下载文件
* @param fileId 文件ID(上传文件成功后返回的ID)
* @param outFile 文件下载保存位置
* @return
*/
public static int downloadFile(String fileId, File outFile) {
FileOutputStream fos = null;
try {
byte[] content = storageClient1.download_file1(fileId);
fos = new FileOutputStream(outFile);
// IOUtils.copy(content,fos);
IOUtils.copy(new ByteArrayInputStream(content), fos);
return 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return -1;
} }

Java客户端配置文件(fdfs_client.conf):

connect_timeout = 10
network_timeout = 20
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890 tracker_server = 192.168.0.204:22122

Java客户端文件上传、下载、删除和元数据获取测试FastDFSClientTest文件

 package com.cky.fastdfsdemo;

 import org.junit.Test;

 import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; /**
* Created by chenkaiyang on 2017/11/30.
*/
public class FastDFSClientTest { /**
* 文件上传测试
*/
@Test
public void testUpload() {
File file = new File("F:\\img\\edc.jpg");
Map<String,String> metaList = new HashMap<String, String>();
metaList.put("width","1024");
metaList.put("height","768");
metaList.put("author","陈冠希");
metaList.put("date","20171130");
String fid = FastDFSClient.uploadFile(file,file.getName(),metaList);
System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid);
//上传成功返回的文件ID: group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg
} /**
* 文件下载测试
*/
@Test
public void testDownload() {
int r = FastDFSClient.downloadFile("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg", new File("F:\\cccDownloadFile_fid.jpg"));
System.out.println(r == 0 ? "下载成功" : "下载失败");
} /**
* 获取文件元数据测试
*/
@Test
public void testGetFileMetadata() {
Map<String,String> metaList = FastDFSClient.getFileMetadata("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg");
for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String,String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
System.out.println(name + " = " + value );
}
} /**
* 文件删除测试
*/
@Test
public void testDelete() {
int r = FastDFSClient.deleteFile("group1/M00/00/00/wKgAzFogMFKAVgnMAABIYRjp760474.jpg");
System.out.println(r == 0 ? "删除成功" : "删除失败");
}
}

获取文件扩展名的工具类

FileUtils

 package com.cky.util;

 /**
* Created by chenkaiyang on 2017/11/30.
*/
public class FileUtil {
/*
* Java文件操作 获取文件扩展名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/*
* Java文件操作 获取不带扩展名的文件名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
} }

pom文件

 <dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

附送项目地址

上一篇:[转]行者,一念一生,成功的背后!(给所有IT人)


下一篇:[Xamarin] 透過WebClient跟網路取得資料 (转帖)