FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
2015-11-18 16:47
741 查看
调用的API为:
String[] upload_file(
String group_name,//组名,不指定则可设为null
long file_size,//文件大小,必须制定
UploadCallback callback,//回调
String file_ext_name,
NameValuePair[] meta_list
)
/**
* 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接口的类:
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;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。合作请联系微信 sinosuperman。
String[] upload_file(
String group_name,//组名,不指定则可设为null
long file_size,//文件大小,必须制定
UploadCallback callback,//回调
String file_ext_name,
NameValuePair[] meta_list
)
/**
* 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接口的类:
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;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。合作请联系微信 sinosuperman。
相关文章推荐
- 负载均衡技术沙龙1期(关于咱的图片)
- 负载均衡沙龙活动第二期现场问答汇集
- 流量引导:网络世界的负载均衡解密
- 流量引导:网络世界的负载均衡解密
- 浅谈sqlserver的负载均衡问题
- PHP开发负载均衡指南
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- LVS+Keepalived构建高可用负载均衡(测试篇)
- 简单测试Apache是如何完成负载均衡策略配置
- Apache负载均衡设置方法 mod_proxy使用介绍
- LVS(Linux Virtual Server)Linux 虚拟服务器介绍及配置(负载均衡系统)
- Shell脚本实现把进程负载均衡到多核CPU中
- tomcat6_apache2.2_ajp 负载均衡加集群实战分享
- [Oracle] RAC 之 - 负载均衡深入解析
- windows NLB+ARR实现Web负载均衡高可用/可伸缩的方法
- haproxy+keepalived实现高可用负载均衡(理论篇)
- 理解web服务器和数据库的负载均衡以及反向代理
- 用apache和tomcat搭建集群(负载均衡)
- LVS+Keepalived构建高可用负载均衡配置方法(配置篇)
- Apache实现Web Server负载均衡详解(不考虑Session版)