/**
* 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.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import java.util.Arrays;
import java.net.Socket;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.common.Base64;
/**
* Storage client for 2 fields file id: group name and filename
* @author Happy Fish / YuQing
* @version Version 1.24
*/
public class StorageClient
{
/**
* Upload file by file buff
* @author Happy Fish / YuQing
* @version Version 1.12
*/
public static class UploadBuff implements UploadCallback
{
private byte[] fileBuff;
private int offset;
private int length;
/**
* constructor
* @param fileBuff the file buff for uploading
*/
public UploadBuff(byte[] fileBuff, int offset, int length)
{
super();
this.fileBuff = fileBuff;
this.offset = offset;
this.length = length;
}
/**
* send file content callback function, be called only once when the file uploaded
* @param out output stream for writing file content
* @return 0 success, return none zero(errno) if fail
*/
public int send(OutputStream out) throws IOException
{
out.write(this.fileBuff, this.offset, this.length);
return 0;
}
}
public final static Base64 base64 = new Base64('-', '_', '.', 0);
protected TrackerServer trackerServer;
protected StorageServer storageServer;
protected byte errno;
/**
* constructor using global settings in class ClientGlobal
*/
public StorageClient()
{
this.trackerServer = null;
this.storageServer = null;
}
/**
* constructor with tracker server and storage server
* @param trackerServer the tracker server, can be null
* @param storageServer the storage server, can be null
*/
public StorageClient(TrackerServer trackerServer, StorageServer storageServer)
{
this.trackerServer = trackerServer;
this.storageServer = storageServer;
}
/**
* get the error code of last call
* @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, MyException
{
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
*/
protected String[] upload_file(String group_name, String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
final byte cmd = ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE;
return this.upload_file(cmd, group_name, local_filename, file_ext_name, meta_list);
}
/**
* upload file to storage server (by file name)
* @param cmd the command
* @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
*/
protected String[] upload_file(byte cmd, String group_name, String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
File f = new File(local_filename);
FileInputStream fis = new FileInputStream(f);
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);
}
}
try
{
return this.do_upload_file(cmd, group_name, null, null, file_ext_name,
f.length(), new UploadStream(fis, f.length()), meta_list);
}
finally
{
fis.close();
}
}
/**
* upload file to storage server (by file buff)
* @param file_buff file content/buff
* @param offset start offset of the buff
* @param length the length of buff to upload
* @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, int offset, int length, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
final String group_name = null;
return this.upload_file(group_name, file_buff, offset, length, 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 offset start offset of the buff
* @param length the length of buff to upload
* @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, int offset, int length,
String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException
{
return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, null, null, file_ext_name,
length, new UploadBuff(file_buff, offset, length), 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, MyException
{
final String group_name = null;
return this.upload_file(group_name, file_buff, 0, file_buff.length, 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 ar
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java操作FastDFS示例代码 (170个子文件)
edu-demo-fdfs.log.2015-03-29 312B
edu-demo-fdfs.log.2015-03-30 545B
StorageClient.class 28KB
StorageClient.class 28KB
StructStorageStat.class 15KB
StructStorageStat.class 15KB
TrackerClient.class 14KB
TrackerClient.class 14KB
StorageClient1.class 13KB
StorageClient1.class 12KB
ProtoCommon.class 11KB
ProtoCommon.class 11KB
Monitor.class 11KB
Monitor.class 11KB
TestClient.class 11KB
TestAppender.class 10KB
TestClient.class 10KB
TestAppender.class 10KB
TestAppender1.class 10KB
TestClient1.class 9KB
TestAppender1.class 9KB
TestClient1.class 9KB
Base64.class 6KB
Base64.class 6KB
FastDFSClient.class 5KB
ClientGlobal.class 4KB
ClientGlobal.class 4KB
FastDFSClient.class 4KB
StructGroupStat.class 4KB
StructGroupStat.class 4KB
IniFileReader.class 3KB
IniFileReader.class 3KB
TrackerGroup.class 3KB
Test.class 3KB
TrackerGroup.class 3KB
Test1.class 3KB
Test.class 3KB
TestLoad$DownloadThread.class 3KB
Test1.class 2KB
TestLoad$DownloadThread.class 2KB
TestLoad.class 2KB
TestLoad.class 2KB
TestLoad$Uploader.class 2KB
TestLoad$Downloader.class 2KB
StructBase.class 2KB
StructBase.class 2KB
TestLoad$Uploader.class 2KB
FastDFSTest.class 2KB
TestLoad$Downloader.class 2KB
FileInfo.class 2KB
FileInfo.class 2KB
FastDFSTest.class 2KB
TestLoad$UploadThread.class 2KB
TestLoad$UploadThread.class 2KB
ProtoStructDecoder.class 2KB
ProtoStructDecoder.class 2KB
TrackerServer.class 2KB
TrackerServer.class 1KB
DownloadFileWriter.class 1KB
DownloadFileWriter.class 1KB
UploadLocalFileSender.class 1KB
ServerInfo.class 1KB
ServerInfo.class 1KB
StorageServer.class 1KB
UploadStream.class 1KB
UploadStream.class 1KB
StorageServer.class 1KB
UploadLocalFileSender.class 1KB
NameValuePair.class 970B
NameValuePair.class 970B
DownloadStream.class 872B
DownloadStream.class 872B
StorageClient$UploadBuff.class 860B
StorageClient$UploadBuff.class 860B
TestLoad$DownloadFileDiscard.class 624B
TestLoad$DownloadFileDiscard.class 624B
StructBase$FieldInfo.class 583B
StructBase$FieldInfo.class 583B
ProtoCommon$RecvPackageInfo.class 527B
ProtoCommon$RecvPackageInfo.class 527B
ProtoCommon$RecvHeaderInfo.class 526B
ProtoCommon$RecvHeaderInfo.class 526B
MyException.class 444B
MyException.class 444B
UploadCallback.class 220B
UploadCallback.class 220B
DownloadCallback.class 158B
DownloadCallback.class 158B
.classpath 669B
org.eclipse.wst.common.component 588B
fdfs_client.conf 190B
org.eclipse.wst.jsdt.ui.superType.container 49B
wc.db 932KB
entries 3B
format 3B
log4j-1.2.17.jar 478KB
commons-lang3-3.1.jar 308KB
junit-4.11.jar 239KB
commons-io-2.0.1.jar 156KB
fastdfs_client_v1.24.jar 92KB
共 170 条
- 1
- 2
资源评论
- 只佣2018-08-27资源可以,适合学习和研究
- 一只安静的程序猿2017-12-27资源很好,很适合学习研究。谢谢楼主分享。需要学习研究的童鞋可以放心下载。
冰河
- 粉丝: 22w+
- 资源: 131
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功