/** Copyright (C) 2012 Happy Fish / YuQing
* My FastDFS Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
*/
package org.csource.myfastdfs;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.csource.common.MyException;
import org.csource.fastdfs.TrackerServer;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.UploadCallback;
import org.csource.fastdfs.DownloadCallback;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdht.ServerGroup;
import org.csource.fastdht.FastDHTClient;
import org.csource.fastdht.KeyInfo;
/** My FastDFS client
* @author Happy Fish / YuQing
* @version Version 1.00
*/
public class MyFastDFSClient
{
public static final String MY_CLIENT_FILE_ID_KEY_NAME = "fdfs_fid";
protected StorageClient1 storageClient1;
protected FastDHTClient fdhtClient;
protected String fdhtNamespace;
protected int status; //last error code
/**
* constructor
* @param fdhtNamespace the FastDHT namespace
*/
public MyFastDFSClient(String fdhtNamespace) throws MyException
{
this.storageClient1 = new StorageClient1();
this.fdhtClient = new FastDHTClient(true);
if (fdhtNamespace == null || fdhtNamespace.length() == 0)
{
throw new MyException("FastDHT namespace must be set!");
}
this.fdhtNamespace = fdhtNamespace;
}
/**
* constructor
* @param trackerServer the FastDFS tracker server, can be null
* @param storageServer the FastDFS storage server, can be null
* @param serverGroup the FastDHT group info
* @param fdhtNamespace the FastDHT namespace
*/
public MyFastDFSClient(TrackerServer trackerServer, StorageServer storageServer, ServerGroup serverGroup, String fdhtNamespace) throws MyException
{
this.storageClient1 = new StorageClient1(trackerServer, storageServer);
this.fdhtClient = new FastDHTClient(serverGroup);
if (fdhtNamespace == null || fdhtNamespace.length() == 0)
{
throw new MyException("FastDHT namespace must be set!");
}
this.fdhtNamespace = fdhtNamespace;
}
/**
* load parameters from FastDFS and FastDHT config file
* @param fdfsConfigFilename the FastDFS config filename
* @param fdhtConfigFilename the FastDHT config filename
*/
public static void init(String fdfsConfigFilename, String fdhtConfigFilename) throws FileNotFoundException, IOException, MyException
{
org.csource.fastdfs.ClientGlobal.init(fdfsConfigFilename);
org.csource.fastdht.ClientGlobal.init(fdhtConfigFilename);
}
/**
* @return the error code of last call
*/
public int getErrorCode()
{
return this.status;
}
/**
* close connections
*/
public void close()
{
this.fdhtClient.close();
}
/**
* check FastDFS file id not exist
* @param keyInfo the FastDHT key object
* @return true for not exist, otherwise false
*/
protected boolean check_fdfs_file_id_not_exist(KeyInfo keyInfo) throws IOException, MyException
{
String fdfs_file_id = this.fdhtClient.get(keyInfo);
this.status = this.fdhtClient.getErrorCode();
if (fdfs_file_id != null || this.status == 0)
{
this.status = 17; //EEXIST
throw new MyException("file id: " + new String(keyInfo.getObjectId(), org.csource.fastdht.ClientGlobal.g_charset) + " already exist");
}
return (this.status == 2);
}
/**
* save FastDFS file id to FastDHT
* @param keyInfo the FastDHT key object
* @param fdfs_file_id the FastDFS file id
* @return 0 for success, != 0 for error (error no)
*/
protected int set_fdfs_file_id(KeyInfo keyInfo, String fdfs_file_id) throws IOException, MyException
{
try
{
if ((this.status=this.fdhtClient.set(keyInfo, fdfs_file_id)) != 0)
{
this.storageClient1.delete_file1(fdfs_file_id); //rollback
}
}
catch(Exception ex)
{
this.status = 5;
ex.printStackTrace();
this.storageClient1.delete_file1(fdfs_file_id); //rollback
}
return this.status;
}
/**
* get FastDFS file id
* @param my_file_id the file id specified by application
* @return FastDFS file id for success, return null for fail
*/
public String get_fdfs_file_id(String my_file_id) throws IOException, MyException
{
KeyInfo keyInfo = new KeyInfo(this.fdhtNamespace, my_file_id, MY_CLIENT_FILE_ID_KEY_NAME);
String fdfs_file_id = this.fdhtClient.get(keyInfo);
this.status = this.fdhtClient.getErrorCode();
return fdfs_file_id;
}
/**
* upload file to storage server (by file name)
* @param my_file_id the file id specified by application
* @param local_filename local filename to upload
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @return 0 for success, != 0 for error (error no)
*/
public int upload_file(String my_file_id, String local_filename, String file_ext_name) throws IOException, MyException
{
final String group_name = "";
return this.upload_file(my_file_id, group_name, local_filename, file_ext_name);
}
/**
* upload file to storage server (by file name)
* @param my_file_id the file id specified by application
* @param group_name the group name to upload file to, can be empty
* @param local_filename local filename to upload
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @return 0 for success, != 0 for error (error no)
*/
public int upload_file(String my_file_id, String group_name, String local_filename, String file_ext_name) throws IOException, MyException
{
KeyInfo keyInfo = new KeyInfo(this.fdhtNamespace, my_file_id, MY_CLIENT_FILE_ID_KEY_NAME);
if (!this.check_fdfs_file_id_not_exist(keyInfo))
{
return this.status;
}
String fdfs_file_id = this.storageClient1.upload_file1(group_name, local_filename, file_ext_name, null);
this.status = this.storageClient1.getErrorCode();
if (fdfs_file_id == null)
{
return this.status;
}
return this.set_fdfs_file_id(keyInfo, fdfs_file_id);
}
/**
* upload file to storage server (by file buffer)
* @param my_file_id the file id specified by application
* @param file_buff the file content / buffer to upload
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @return 0 for success, != 0 for error (error no)
*/
public int upload_file(String my_file_id, byte[] file_buff, String file_ext_name) throws IOException, MyException
{
final String group_name = "";
return this.upload_file(my_file_id, group_name, file_buff, file_ext_name);
}
/**
* upload file to storage server (by file buffer)
* @param my_file_id the file id specified by application
* @param group_name the group name to upload file to, can be empty
* @param file_buff the file content / buffer to upload
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @return 0 for success, != 0 for error (error no)
*/
public int upload_file(String my_file_id, String group_name, byte[] file_buff, String file_ext_name) throws IOException, MyException
{
KeyInfo keyInfo = new KeyInfo(this.fdhtNamespace, my_file_id, MY_CLIENT_FILE_ID_KEY_NAME);
if (!this.check_fdfs_file_id_not_exist(keyInfo))
{
return this.status;
}
String fdfs_file_id = this.storageClient1.upload_file1(group_name, file_buff, file_ext_name, null);
this.status = this.storageClient1.getErrorCode();
if (fdfs_file_id == null)
{
return this.status;
}
return this.set_fdfs_file_id(keyInfo, fdfs_file_id);
}
/**
* upload file to storage server (by callback object)
* @param my_file_id the file id specified by application
* @param file_size the file size
* @param callback the write data callback object
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @return 0 for success, != 0 for error (error no)
*/
public int upload_file(Stri
没有合适的资源?快使用搜索试试~ 我知道了~
my-fastdfs-client
需积分: 9 11 下载量 39 浏览量
2018-05-16
16:28:37
上传
评论
收藏 143KB GZ 举报
温馨提示
共39个文件
html:13个
c:3个
jar:3个
我现在已经有很多很多文件了,将近500W的文件数,该怎么使用FastDFS来管事呢,难道需要再重新使用fdfs_upload_file重传一遍吗?重传过后的文件名怎么办?因为这些文件名都是在数据库里有的,重传的话,文件名就变掉了。 可以使用my-fastfds-client,这个client library支持应用端指定文件ID。
资源推荐
资源详情
资源评论
收起资源包目录
my-fastdfs-client_v1.01.tar.gz (39个子文件)
my-fastdfs-client
c
HISTORY 233B
INSTALL 716B
my_fdfs_client_test.c 2KB
my_fdfs_client.c 17KB
my_fdfs_link_library.sh 551B
Makefile 2KB
my_fdfs_client.h 13KB
php-extension
my_fastdfs_client.ini 912B
php_my_fastdfs_client.h 735B
config.m4 946B
README 10KB
my_fdfs_client_test.php 723B
php_my_fastdfs_client.c 50KB
java
src
HISTORY 115B
build.xml 2KB
README 673B
org
csource
myfastdfs
MyFDFSClientTest.java 2KB
MyFastDFSClient.java 25KB
lib
my_fastdfs_client.jar 6KB
fastdfs_client.jar 88KB
fastdht_client.jar 27KB
etc
fdht_client.conf 289B
fdfs_client.conf 229B
doc
constant-values.html 6KB
index.html 1KB
overview-tree.html 5KB
allclasses-noframe.html 792B
deprecated-list.html 4KB
help-doc.html 8KB
package-list 23B
allclasses-frame.html 832B
resources
inherit.gif 57B
index-all.html 21KB
org
csource
myfastdfs
package-tree.html 5KB
MyFastDFSClient.html 58KB
package-frame.html 1015B
MyFDFSClientTest.html 9KB
package-summary.html 5KB
stylesheet.css 1KB
共 39 条
- 1
资源评论
匠心零度
- 粉丝: 9458
- 资源: 305
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功