/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* 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.fastdfs;
import java.io.*;
import java.util.*;
import java.net.*;
import org.csource.common.*;
/**
* Storage client for 2 fields file id: group name and filename
* @author Happy Fish / YuQing
* @version Version 1.9
*/
public class StorageClient
{
protected Socket trackerSocket;
protected StorageServer storageServer;
protected byte errno;
/**
* @param trackerSocket Socket of tracker server
* @param storageServer Socket of storage server, can be null
*/
public StorageClient(Socket trackerSocket, StorageServer storageServer)
{
this.trackerSocket = trackerSocket;
this.storageServer = storageServer;
}
/**
* @return the error code of last call
*/
public byte getErrorCode()
{
return this.errno;
}
/**
* upload file to storage server (by file name)
* @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
* @param meta_list meta info array
* @return 2 elements string array if success:<br>
* <ul><li>results[0]: the group name to store the file </li></ul>
* <ul><li>results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] upload_file(String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, Exception
{
final String group_name = null;
return this.upload_file(group_name, local_filename, file_ext_name, meta_list);
}
/**
* upload file to storage server (by file name)
* @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
* @param meta_list meta info array
* @return 2 elements string array if success:<br>
* <ul><li>results[0]: the group name to store the file </li></ul>
* <ul><li>results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] upload_file(String group_name, String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, Exception
{
if (file_ext_name == null)
{
int nPos = local_filename.lastIndexOf('.');
if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1)
{
file_ext_name = local_filename.substring(nPos+1);
}
}
return this.do_upload_file(group_name, local_filename, null, file_ext_name, meta_list);
}
/**
* upload file to storage server (by file buff)
* @param file_buff file content/buff
* @param file_ext_name file ext name, do not include dot(.)
* @param meta_list meta info array
* @return 2 elements string array if success:<br>
* <ul><li>results[0]: the group name to store the file</li></ul>
* <ul><li>results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] upload_file(byte[] file_buff, String file_ext_name,
NameValuePair[] meta_list) throws IOException, Exception
{
final String group_name = null;
return this.do_upload_file(group_name, null, file_buff, file_ext_name, meta_list);
}
/**
* upload file to storage server (by file buff)
* @param group_name the group name to upload file to, can be empty
* @param file_buff file content/buff
* @param file_ext_name file ext name, do not include dot(.)
* @param meta_list meta info array
* @return 2 elements string array if success:<br>
* <ul><li>results[0]: the group name to store the file</li></ul>
* <ul><li>results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] upload_file(String group_name, byte[] file_buff, String file_ext_name,
NameValuePair[] meta_list) throws IOException, Exception
{
return this.do_upload_file(group_name, null, file_buff, file_ext_name, meta_list);
}
/**
* upload file to storage server (by callback)
* @param group_name the group name to upload file to, can be empty
* @param file_size the file size
* @param callback the write data callback object
* @param file_ext_name file ext name, do not include dot(.)
* @param meta_list meta info array
* @return 2 elements string array if success:<br>
* <ul><li>results[0]: the group name to store the file</li></ul>
* <ul><li>results[1]: the new created filename</li></ul>
* return null if fail
*/
public String[] upload_file(String group_name, long file_size,
UploadCallback callback, String file_ext_name,
NameValuePair[] meta_list) throws IOException, Exception
{
byte[] header;
byte[] ext_name_bs;
String new_group_name;
String remote_filename;
boolean bNewConnection;
bNewConnection = this.newWritableStorageConnection(group_name);
try
{
byte[] meta_buff;
byte[] sizeBytes;
byte[] hexLenBytes;
long fileBytes;
FileInputStream fis;
if (meta_list == null)
{
meta_buff = new byte[0];
}
else
{
meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset);
}
sizeBytes = new byte[1 + 2 * ProtoCommon.TRACKER_PROTO_PKG_LEN_SIZE];
ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN];
Arrays.fill(ext_name_bs, (byte)0);
if (file_ext_name != null && file_ext_name.length() > 0)
{
byte[] bs = file_ext_name.getBytes();
int ext_name_len = bs.length;
if (ext_name_len > ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN)
{
ext_name_len = ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN;
}
System.arraycopy(bs, 0, ext_name_bs, 0, ext_name_len);
}
Arrays.fill(sizeBytes, (byte)0);
sizeBytes[0] = (byte)this.storageServer.getStorePathIndex();
hexLenBytes = ProtoCommon.long2buff(meta_buff.length);
System.arraycopy(hexLenBytes, 0, sizeBytes, 1, hexLenBytes.length);
hexLenBytes = ProtoCommon.long2buff(file_size);
System.arraycopy(hexLenBytes, 0, sizeBytes, 1 + ProtoCommon.TRACKER_PROTO_PKG_LEN_SIZE, hexLenBytes.length);
header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE,
1 + 2 * ProtoCommon.TRACKER_PROTO_PKG_LEN_SIZE
+ ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + meta_buff.length
+ file_size, (byte)0);
OutputStream out = this.storageServer.getOutputStream();
byte[] wholePkg = new byte[header.length + sizeBytes.length + ext_name_bs.length + meta_buff.length];
System.arraycopy(header, 0, wholePkg, 0, header.length);
System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length);
System.arraycopy(ext_name_bs, 0, wholePkg, header.length+sizeBytes.length, ext_name_bs.length);
if (meta_buff.length > 0)
{
System.arraycopy(meta_buff, 0, wholePkg, header.length+sizeBytes.length+ext_name_bs.length, meta_buff.length);
}
out.write(wholePkg);
if ((this.errno=(byte)callback.send(out)) != 0)
{
return null;
}
ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(this.storageServer.getInputStream(),
ProtoCommon.STORAGE_PROTO_CMD_RESP, -1);
this.errno = pkgInfo.errno;
if (pkgInfo.errno != 0)
{
return null;
}
if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN)
{
throw new Exception("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
}
new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim();
remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN);
String[] results = new String[2];
results[0] = new_group_name;
results[1] =
没有合适的资源?快使用搜索试试~ 我知道了~
fastdfs客户端api
5星 · 超过95%的资源 需积分: 20 20 下载量 66 浏览量
2015-12-14
12:39:17
上传
评论
收藏 17KB GZ 举报
温馨提示
共22个文件
java:18个
readme:1个
history:1个
fastdfs客户端api fastdfs_client_java_v1.10.tar.gz 解压缩即可使用
资源推荐
资源详情
资源评论
收起资源包目录
fastdfs_client_java_v1.10.tar.gz (22个子文件)
src
build.xml 2KB
README 769B
org
csource
fastdfs
StorageClient1.java 10KB
ProtoCommon.java 10KB
UploadCallback.java 737B
ClientGlobal.java 3KB
TrackerGroup.java 2KB
StorageServer.java 2KB
TestClient.java 8KB
DowloadFileWriter.java 1KB
TrackerServer.java 2KB
ServerInfo.java 1KB
UploadLocalFileSender.java 1KB
DowloadCallback.java 785B
StorageClient.java 29KB
TrackerClient.java 10KB
TestClient1.java 7KB
TestLoad.java 7KB
common
NameValuePair.java 972B
IniFileReader.java 3KB
fdfs_client.conf 208B
HISTORY 2KB
共 22 条
- 1
资源评论
- angle3angle12016-10-30非常实用值得下载
domm5566
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功