FastDFS的配置、部署与API使用解读——设置FastDFS配置参数的两种方式
一种方式是通过调用ClientGlobal类的初始化方法对配置文件进行加载,另一种是通过调用API逐一设置配置参数。后一种方式对于使用Zookeeper等加载属性的方式很方便。
1. 加载配置文件:
Java代码
String configFileName = "conf/dfs-client.conf";
try {
ClientGlobal.init(configFileName);
2. 主动设置配置参数:
Java代码
//连接超时的时限,单位为毫秒
ClientGlobal.setG_connect_timeout(2000);
//网络超时的时限,单位为毫秒
ClientGlobal.setG_network_timeout(30000);
ClientGlobal.setG_anti_steal_token(false);
//字符集
ClientGlobal.setG_charset("UTF-8");
ClientGlobal.setG_secret_key(null);
//HTTP访问服务的端口号
ClientGlobal.setG_tracker_http_port(7271);
//Tracker服务器列表
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
tracker_servers[0] = "200.200.200.200:8080";
tracker_servers[1] = "200.200.201.200:8080";
tracker_servers[2] = "200.200.202.200:8080";
ClientGlobal.setG_tracker_group(new TrackerGroup(trackerServers));
调用的API为:
String[] upload_file(
String group_name,//组名,不指定则可设为null
long file_size,//文件大小,必须制定
UploadCallback callback,//回调
String file_ext_name,
NameValuePair[] meta_list
)
Java代码
/**
* Upload File to DFS, directly transferring java.io.InputStream to java.io.OutStream
* @author Poechant
* @email zhongchao.ustc@gmail.com
* @param fileBuff, file to be uploaded.
* @param uploadFileName, the name of the file.
* @param fileLength, the length of the file.
* @return the file ID in DFS.
* @throws IOException
*/
public String[] uploadFileByStream(InputStream inStream, String uploadFileName, long fileLength) throws IOException {
String[] results = null;
String fileExtName = "";
if (uploadFileName.contains(".")) {
fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
} else {
logger.warn("Fail to upload file, because the format of filename is illegal.");
return results;
}
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", uploadFileName);
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
try {
// results[0]: groupName, results[1]: remoteFilename.
results = client.upload_file(null, fileLength, new UploadFileSender(inStream), fileExtName, metaList);
} catch (Exception e) {
logger.warn("Upload file \"" + uploadFileName + "\"fails");
}
trackerServer.close();
return results;
}
其中的UploadFileSender是一个实现了UploadCallback接口的类:
Java代码
private static class UploadFileSender implements UploadCallback {
private InputStream inStream;
public UploadFileSender(InputStream inStream) {
this.inStream = inStream;
}
public int send(OutputStream out) throws IOException {
int readBytes;
while((readBytes = inStream.read()) > 0) {
out.write(readBytes);
}
return 0;
}
}
通过Servlet得到InputStream、文件名称和文件长度,然后通过调用FastDFS提供的Java API把文件上传到FastDFS服务器。
下段代码中的getFileBuffer可本博客参考上一篇博文。(by Poechant)
Java代码
/**
* Upload File to DFS.
* @param fileBuff, file to be uploaded.
* @param uploadFileName, the name of the file.
* @param fileLength, the length of the file.
* @return the file ID in DFS.
* @throws IOException
*/
public String uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException {
byte[] fileBuff = getFileBuffer(inStream, fileLength);
String fileId = "";
String fileExtName = "";
if (uploadFileName.contains(".")) {
fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
} else {
logger.warn("Fail to upload file, because the format of filename is illegal.");
return fileId;
}
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", uploadFileName);
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));
try {
fileId = client.upload_file1(fileBuff, fileExtName, metaList);
} catch (Exception e) {
logger.warn("Upload file \"" + uploadFileName + "\"fails");
}
trackerServer.close();
return fileId;
}
Java代码
/**
* Transfer java.io.InpuStream to byte array.
* @param inStream, input stream of the uploaded file.
* @param fileLength, the length of the file.
* @return the byte array transferred from java.io.Inputstream.
* @throws IOException occurred by the method read(byte[]) of java.io.InputStream.
*/
private byte[] getFileBuffer(InputStream inStream, long fileLength) throws IOException {
byte[] buffer = new byte[256 * 1024];
byte[] fileBuffer = new byte[(int) fileLength];
int count = 0;
int length = 0;
while((length = inStream.read(buffer)) != -1){
for (int i = 0; i < length; ++i)
{
fileBuffer[count + i] = buffer[i];
}
count += length;
}
return fileBuffer;
}