博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
阅读量:5264 次
发布时间:2019-06-14

本文共 4016 字,大约阅读时间需要 13 分钟。

调用的API为:

String[] upload_file(

String group_name,//组名,不指定则可设为null

long file_size,//文件大小,必须制定

UploadCallback callback,//回调

String file_ext_name,

NameValuePair[] meta_list

)

 

 

[java]
  1. /**
  2. * Upload File to DFS, directly transferring java.io.InputStream to java.io.OutStream
  3. * @author Poechant
  4. * @email zhongchao.ustc@gmail.com
  5. * @param fileBuff, file to be uploaded.
  6. * @param uploadFileName, the name of the file.
  7. * @param fileLength, the length of the file.
  8. * @return the file ID in DFS.
  9. * @throws IOException
  10. */ 
  11. public String[] uploadFileByStream(InputStream inStream, String uploadFileName, long fileLength) throws IOException { 
  12.      
  13.     String[] results = null; 
  14.     String fileExtName = ""; 
  15.     if (uploadFileName.contains(".")) { 
  16.         fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1); 
  17.     } else { 
  18.         logger.warn("Fail to upload file, because the format of filename is illegal."); 
  19.         return results; 
  20.     } 
  21.      
  22.     TrackerClient tracker = new TrackerClient(); 
  23.        TrackerServer trackerServer = tracker.getConnection(); 
  24.        StorageServer storageServer = null; 
  25.        StorageClient1 client = new StorageClient1(trackerServer, storageServer); 
  26.         
  27.        NameValuePair[] metaList = new NameValuePair[3]; 
  28.        metaList[0] = new NameValuePair("fileName", uploadFileName); 
  29.        metaList[1] = new NameValuePair("fileExtName", fileExtName); 
  30.        metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength)); 
  31.         
  32.        try { 
  33.         // results[0]: groupName, results[1]: remoteFilename. 
  34.         results = client.upload_file(null, fileLength, new UploadFileSender(inStream), fileExtName, metaList); 
  35.     } catch (Exception e) { 
  36.         logger.warn("Upload file \"" + uploadFileName + "\"fails"); 
  37.     } 
  38.         
  39.         trackerServer.close(); 
  40.      
  41.     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[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接口的类:

 

[java]
  1. private static class UploadFileSender implements UploadCallback { 
  2.      
  3.     private InputStream inStream; 
  4.      
  5.     public UploadFileSender(InputStream inStream) { 
  6.         this.inStream = inStream; 
  7.     } 
  8.      
  9.     public int send(OutputStream out) throws IOException { 
  10.         int readBytes; 
  11.         while((readBytes = inStream.read()) > 0) { 
  12.             out.write(readBytes); 
  13.         } 
  14.         return 0; 
  15.     } 
  16. }  
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;		}	}

转载于:https://www.cnblogs.com/fengchaowang/p/4138060.html

你可能感兴趣的文章
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>