zoukankan      html  css  js  c++  java
  • FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码(转)

    本文来自 诗商·柳惊鸿 Poechant CSDN博客,转载请注明源地址:FastDFS的配置、部署与API使用解读(2)上传文件到FastDFS分布式文件系统的客户端代码


    在阅读本文之前,请您先通过《FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS》一文中给出的参考博文中的部署篇和测试篇来完成前期的准备工作。


    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可参考本博客上一篇博文。(by Poechant)

     1     /** 
     2      * Upload File to DFS. 
     3      * @param fileBuff, file to be uploaded. 
     4      * @param uploadFileName, the name of the file. 
     5      * @param fileLength, the length of the file. 
     6      * @return the file ID in DFS. 
     7      * @throws IOException  
     8      */  
     9     public String uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException {  
    10         byte[] fileBuff = getFileBuffer(inStream, fileLength);  
    11         String fileId = "";  
    12         String fileExtName = "";  
    13         if (uploadFileName.contains(".")) {  
    14             fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);  
    15         } else {  
    16             logger.warn("Fail to upload file, because the format of filename is illegal.");  
    17             return fileId;  
    18         }  
    19       
    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       
    28         //设置元信息  
    29         NameValuePair[] metaList = new NameValuePair[3];  
    30         metaList[0] = new NameValuePair("fileName", uploadFileName);  
    31         metaList[1] = new NameValuePair("fileExtName", fileExtName);  
    32         metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));  
    33       
    34       
    35         //上传文件  
    36         try {  
    37             fileId = client.upload_file1(fileBuff, fileExtName, metaList);  
    38         } catch (Exception e) {  
    39             logger.warn("Upload file "" + uploadFileName + ""fails");  
    40         }  
    41         trackerServer.close();  
    42         return fileId;  
    43     }  

    3、调用方式详解
    (1)客户端与Tracker Server通信
    根据《FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS》一文中提供的FastDFS的工作原理,结合上面的代码,首先通过TrackerClient构造函数从全局配置中获取Tracker Servers的IP和端口初始化一个TrackerClient对象tracker,并与其建立连接,我们可以从API的源码中看到:

     1     /** 
     2     * constructor with global tracker group 
     3     */  
     4     public TrackerClient()  
     5     {  
     6         this.tracker_group = ClientGlobal.g_tracker_group;  
     7     }     
     8     /** 
     9     * constructor with specified tracker group 
    10     * @param tracker_group the tracker group object 
    11     */  
    12     public TrackerClient(TrackerGroup tracker_group)  
    13     {  
    14         this.tracker_group = tracker_group;  
    15     }  

    上述代码中ClientGlobal是一个提供很多静态成员供外部读取的类。通过tracker这个TrackerClient建立的与Tracker Server的连接,实例化了一个trackerServer对象。


    (2)客户端与Storage Server通信

    通过trackerServer取得某一个可用的Storage Server的地址并用其实例化一个StorageClient1对象。这样就完成了FastDFS的客户端调用上传、下载、删除等所有操作的前期建立连接的工作。


    (3)调用文件操作API
    这些操作包括upload、download、append、delete等。上例中提供的是上传的实例。



    本文来自 诗商·柳惊鸿 Poechant CSDN博客,转载请注明源地址:FastDFS的配置、部署与API使用解读(2)Java API:根据InputStream、文件名、文件长度上传文件

  • 相关阅读:
    线程池及其原理和使用
    多线程通信Queue
    Condition实现线程通信
    守护线程和锁
    习题 7:更多的打印
    习题 6:字符串和文本
    习题 5:更多的变量和打印
    习题 4:变量和命名
    习题 3:数字和数学计算
    习题 2:注解和#号
  • 原文地址:https://www.cnblogs.com/sandea/p/4439277.html
Copyright © 2011-2022 走看看