/**
* 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
没有合适的资源?快使用搜索试试~ 我知道了~
fastdfs_client(淘淘项目第三天缺的带pom.xml文件的就是这个)

共86个文件
class:41个
java:32个
xml:2个

需积分: 9 144 浏览量
2018-06-17
18:13:28
上传
评论 4
收藏 244KB ZIP 举报
温馨提示
fastdfs_client(淘淘项目第三天缺的带pom.xml文件的就是这个)下载解压就能用哦
资源推荐
资源详情
资源评论






收起资源包目录


















































































































共 86 条
- 1
资源评论


yoo_bi
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
