官网Java客户端源代码:https://github.com/happyfish100/fastdfs-client-java
我已经将源码打包成一个jar,下载后导入到你的工程中即可。如果你是用maven管理工程,可将jar包上传到公司自己担的私服中。jar下载地址:http://download.csdn.net/detail/xyang81/9656690
我将官方提供的sdk封装了一个工具类,将工具类导入工程即可使用,如下所示:
package AA;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.TrackerServer;
/**
* <p>Description: FastDFS文件上传下载工具类 </p>
*/
public class FastDFSClient {
private static final String CONFIG_FILENAME = "src/AA/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 = toByteArray(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);
}
}
String extensionName=fileName.substring(fileName.lastIndexOf(".")+1);
return storageClient1.upload_file1(buff,extensionName,nameValuePairs);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] toByteArray(File f) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}
/**
* 下载文件
* @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);
fos.write(content);
return 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return -1;
}
/**
* 获取文件元数据
* @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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
Java客户端配置文件(fdfs_client.conf):
# 连接tracker服务器超时时长
connect_timeout = 10
# socket连接超时时长
network_timeout = 30
# 文件内容编码
charset = UTF-8
# tracker服务器端口
http.tracker_http_port = 8888
http.anti_steal_token = no
#http.secret_key = FastDFS1234567890
# tracker服务器IP和端口(可以写多个)
tracker_server =192.168.248.128:22122
tracker_server =192.168.248.129:22122
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Java客户端文件上传、下载、删除和元数据获取测试:
package AA;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class FastDFSClientTest {
public static void main(String[] args) {
testUpload();
//testDownload();
//testGetFileMetadata();
//testDelete();
}
/**
* 文件上传测试
*/
public static void testUpload() {
File file = new File("C:\\160452.jpg");
Map<String,String> metaList = new HashMap<String, String>();
//metaList.put("width","1024");
//metaList.put("height","768");
metaList.put("author","aaa");
metaList.put("date","20170409");
String fid = FastDFSClient.uploadFile(file,file.getName(),metaList);
System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid);
//上传成功返回的文件ID: group1/M00/00/00/wKj4fljpDACAF7JXAA0udap1CQE427.jpg
}
/**
* 文件下载测试
*/
public static void testDownload() {
int r = FastDFSClient.downloadFile("group1/M00/00/00/wKj4fljpDACAF7JXAA0udap1CQE427.jpg", new File("f:\\DownloadFile_fid.jpg"));
System.out.println(r == 0 ? "下载成功" : "下载失败");
}
/**
* 获取文件元数据测试
*/
public static void testGetFileMetadata() {
Map<String,String> metaList = FastDFSClient.getFileMetadata("group1/M00/00/00/wKj4fljpDACAF7JXAA0udap1CQE427.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 );
}
}
/**
* 文件删除测试
*/
public static void testDelete() {
int r = FastDFSClient.deleteFile("group1/M00/00/00/wKj4fljpDACAF7JXAA0udap1CQE427.jpg");
System.out.println(r == 0 ? "删除成功" : "删除失败");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68