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

调用的API为:

String[] upload_file(

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

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

UploadCallback callback,//回调

String file_ext_name,

NameValuePair[] meta_list

)

 1     /**
 2      * Upload File to DFS, directly transferring java.io.InputStream to java.io.OutStream
 3      * @author Poechant
 4      * @email [email protected]
 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;
42     }  

其中的UploadFileSender是一个实现了UploadCallback接口的类:

 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     }   
时间: 2024-08-10 01:43:49

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

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

1.下载FastDFS的API FastDFS提供Java和PHP等语言的客户端API.可以到FastDFS在Google Code的项目主页 http://code.google.com/p/fastdfs/downloads/list 下载.本文以Java API为例. 2.调用API的上传接口 通过Servlet得到InputStream.文件名称和文件长度,然后通过调用FastDFS提供的Java API把文件上传到FastDFS服务器.下段代码中的getFileBuffer可参考本博客

FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解(转)

1.StorageClient与StorageClient1的区别 相信使用happy_fish的FastDFS的童鞋们,一定都熟悉StorageClient了,或者你熟悉的是StorageClient1,两者有什么区别呢? 我们在前面的几篇博文中已经知道,FastDFS存储文件时,是将 FastDFS存储到某个Group的某个Storage的某个路径下.如果你在Storage Server上设置的是两层目录结构的话,则一个文件上传到FastDFS后的File ID一般形如"group1/M01

FastDFS的配置、部署与API使用解读(5)FastDFS配置详解之Tracker配置

本文是 tracker.conf 配置文件的详细介绍. 1 基本配置 disable #func:配置是否生效 #valu:true.false disable=false bind_addr #func:绑定IP #valu:IP地址 bind_addr=192.168.6.102 port #func:服务端口 #valu:端口整数值 port=22122 connect_timeout #func:连接超时 #valu:秒单位正整数值 connect_timeout=30 network_

PHP安装FastDFS扩展,测试上传文件

PHP安装FastDFS扩展,测试上传文件 PHP所在服务器需先安装FastDFS.FastDFS安装方法如上,无需启动服务. 安装FastDFS扩展 # cd /usr/local/FastDFS/php_client/ # ls /opt/lampp/bin/phpize /opt/lampp/bin/phpize # /opt/lampp/bin/phpize Configuring for: PHP Api Version:         20121113 Zend Module Ap

Fastdfs 无法上传文件问题描述及解决方案

今开发反馈一问题,发现fastdfs中有两个组无法上传文件,简单测试了下: fdfs_upload_file /etc/fdfs/storage.conf /root/1.jpg 会返回给我N00~N02,N04,N06~N10但缺少了N03,N05 通过fdfs_monitor和监控(23000端口)都是没问题的,于是专门对比了N02和N03,N05的区别之处: 发现N02的磁盘: /dev/sdc1             3.6T  884G  2.6T  26% /data/M00 /d

FastDfs 学习进阶之环境安装及上传文件测试

一:环境介绍       10.63.13.138 tracker,nginx proxy 10.63.13.143 storage,nginx 二:资源包准备 FastDFS官方论坛:http://www.csource.org 资源下载路径:http://sourceforge.net/projects/fastdfs/files/ 本实验学习环境使用CENTOS 6.4 X64 需要下载的资源包: FastDFS源代码:FastDFS_v5.05.tar.gz nginx模块源代码:fas

ueditor1.4.3配置过程(包含单独上传文件以及图片的使用)

这里使用的是ueditor1.4.3的jsp版本的UTF-8版本. 首先下载相应的ueditor,将ueditor文件夹直接拷贝到项目中,文件结构如下所示: 然后将项目要用的jar包导入到lib目录下,在导入之前要先修改一下ueditor的jar包中的bug,这个可以参考这里点击打开链接 接着就是配置jsp目录下的config.json文件了,让它能适应我们的项目,配置内容如下: /* 前后端通信相关的配置,注释只允许使用多行方式 */ { /* 上传图片配置项 */ "imageActionN

HDFS基本命令行操作及上传文件的简单API

一.HDFS基本命令行操作: 1.HDFS集群修改SecondaryNameNode位置到hd09-2 (1)修改hdfs-site.xml <configuration> //配置元数据存储位置 <property> <name>dfs.namenode.name.dir</name> <value>/root/hd/dfs/name</value> </property> //配置数据存储位置 <property

.Net C#向远程服务器Api上传文件

Api服务代码一: /// <summary> /// 服务器接收接口 /// </summary> [HttpPost] [Route("ReceiveFile")] public HttpResponseMessage ReceiveFile() { string result = string.Empty; ArrayList list = new ArrayList(); try { Stream postStream = HttpContext.Cur