FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
2015-07-06 14:45
1036 查看
调用的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;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
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;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- 小心服务器内存居高不下的元凶--WebAPI服务
- 运维入门
- Linux5.9无人值守安装
- 数据中心和云未来的十二大趋势
- 用vsftp快速搭建ftp服务器
- Linux快速构建apache web服务器
- 负载均衡技术沙龙1期(关于咱的图片)
- 负载均衡沙龙活动第二期现场问答汇集
- 服务器监控策略浅谈
- Tomcat端口被占用解决方法(不用重启)
- 如何降低服务器采购成本 原理分析
- 建议的服务器分区办法
- 服务器托管六大优势分析
- Erlang实现的一个Web服务器代码实例
- 服务器技术全面解析
- “传奇”图象数据存储方式
- 保护DNS服务器的几点方法小结
- 我国成为全球第二大服务器消费国
- 服务器 安全检查要点[星外提供]