FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
调用的API为:String[] upload_file(
String group_name,//组名,不指定则可设为null
long file_size,//文件大小,必须制定
UploadCallback callback,//回调
String file_ext_name,
NameValuePair[] meta_list
)
view plaincopyprint?
[*]/**
[*] * 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;
[*] metaList = new NameValuePair("fileName", uploadFileName);
[*] metaList = new NameValuePair("fileExtName", fileExtName);
[*] metaList = new NameValuePair("fileLength", String.valueOf(fileLength));
[*]
[*] try {
[*] // results: groupName, results: 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;
[*]}
/**
* 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;
metaList = new NameValuePair("fileName", uploadFileName);
metaList = new NameValuePair("fileExtName", fileExtName);
metaList = new NameValuePair("fileLength", String.valueOf(fileLength));
try {
// results: groupName, results: 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接口的类:
view plaincopyprint?
[*]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;
[*] }
[*]}
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;
}
}
页:
[1]