FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
2016-07-08 00:00
686 查看
调用的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;
}
}
相关文章推荐
- Java深度历险(三)——Java线程:基本概念、可见性与同步
- Java REST框架一览
- CAS研究(四)-登出/logout
- Eclipse中建立JSR181的web服务
- Java深度历险(七)——Java反射与动态代理
- FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码
- Java深度历险(四)——Java垃圾回收机制与引用类型
- Spring Bean 初始化过程
- 使用Spring @Async异步执行方法的笔记
- java中abstract详解
- java中的匿名内部类总结
- 在代码中修改TextView的DrawableRight图片
- 使用Eclipse-Maven-git做Java开发(17)--maven项目初步
- spring 简介(译)
- jdbc的操作流程代码
- Java后端工程师学习大纲
- atomikos + druid + spring 多数据源分布式事务
- Java 500M的表,约1千万的数据放在HashMap里面大约占用内存多大?给多大内存最保险?
- Java23种设计模式案例:策略模式(strategy)
- jetty/eclipse:A full JDK (not just JRE) is require