FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码

调用的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;
		}
	} 


  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
文本格式不能很好显示,请见谅(附件里有比较齐整的excel表格统计) 大小类型 传输类型 api方法 文件大小 花费时间 速率byte/ms 速率mb/s 缓存数组 次数 备注 大文件 下载 download_file(group_name, remote_filename, localFile) 1073741824(约1G) 28343ms 37883 36.12804413 无 1 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 29195ms 36778 35.07423401 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 24352ms 44092 42.04940796 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 24831ms 43241 41.23783112 2K 1 实现DownloadCallback,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 25922ms 41422 39.50309753 8K 1 实现DownloadCallback,BufferedOutputStream 普通文件 下载 download_file(group_name, remote_filename, localFile) 59113472(约56M) 382ms 154747 147.5782394 无 1 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约57M) 369ms 160199 152.7776718 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约58M) 499ms 118702 113.2030487 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 59113472(约59M) 592ms 99853 95.22724152 2K 1 实现DownloadCallback,BufferedOutputStream 下载建议:100M内数据使用fastDFS提供的DownloadStream;大于1G的数据,使用BufferedOutputStream和DowloadStream
策略模式是一种行为型设计模式,它允许在运行时选择算法的行为。在上传文件到文件服务时,不同的文件服务可能要求不同的上传算法,因此可以使用策略模式来灵活选择上传算法。 以下是使用策略模式设计上传文件到文件服务的一般步骤: 1. 定义一个文件上传接口 UploadStrategy,其中包含一个上传方法 upload(); 2. 定义不同的文件上传策略类,这些类实现 UploadStrategy 接口并提供自己的上传实现; 3. 在客户端代码中创建一个 UploadContext 对象,该对象包含一个 UploadStrategy 成员变量; 4. 根据需要选择上传策略,并将其设置为 UploadContext 的成员变量; 5. 调用 UploadContext 的上传方法,该方法会调用 UploadStrategy 的 upload() 方法,实现文件上传。 下面是一个示例代码: ```java // 定义文件上传接口 interface UploadStrategy { void upload(String filePath); } // 定义 FastDFS 文件上传策略 class FastDFSUploadStrategy implements UploadStrategy { @Override public void upload(String filePath) { // 实现 FastDFS 文件上传 } } // 定义 Minio 文件上传策略 class MinioUploadStrategy implements UploadStrategy { @Override public void upload(String filePath) { // 实现 Minio 文件上传 } } // 定义上传上下文 class UploadContext { private UploadStrategy strategy; public void setStrategy(UploadStrategy strategy) { this.strategy = strategy; } public void uploadFile(String filePath) { strategy.upload(filePath); } } // 客户端代码 public static void main(String[] args) { UploadContext context = new UploadContext(); // 选择 FastDFS 上传策略 context.setStrategy(new FastDFSUploadStrategy()); context.uploadFile("file_path"); // 选择 Minio 上传策略 context.setStrategy(new MinioUploadStrategy()); context.uploadFile("file_path"); } ``` 注意,上述示例代码中的上传实现只是伪代码,实际实现可能需要根据具体文件服务的 API 进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值