• 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可参考本博客上一篇博文。(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; 
    /**
     * Upload File to DFS.
     * @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 uploadFile(InputStream inStream, String uploadFileName, long fileLength) throws IOException {
    	byte[] fileBuff = getFileBuffer(inStream, fileLength);
    	String fileId = "";
    	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 fileId;
    	}
    
    
    	//建立连接
    	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 {
    		fileId = client.upload_file1(fileBuff, fileExtName, metaList);
    	} catch (Exception e) {
    		logger.warn("Upload file "" + uploadFileName + ""fails");
    	}
    	trackerServer.close();
    	return fileId;
    }

    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.     this.tracker_group = ClientGlobal.g_tracker_group; 
    6. }    
    7. /**
    8. * constructor with specified tracker group
    9. * @param tracker_group the tracker group object
    10. */ 
    11. public TrackerClient(TrackerGroup tracker_group) 
    12.     this.tracker_group = tracker_group; 
    /**
    * constructor with global tracker group
    */
    public TrackerClient()
    {
    	this.tracker_group = ClientGlobal.g_tracker_group;
    }	
    /**
    * constructor with specified tracker group
    * @param tracker_group the tracker group object
    */
    public TrackerClient(TrackerGroup tracker_group)
    {
    	this.tracker_group = tracker_group;
    }

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

    (2)客户端与Storage Server通信

    通过trackerServer取得某一个可用的Storage Server的地址并用其实例化一个StorageClient1对象。这样就完成了FastDFS的客户端调用上传、下载、删除等所有操作的前期建立连接的工作。
    (3)调用文件操作API 这些操作包括upload、download、append、delete等。上例中提供的是上传的实例。

  • 相关阅读:
    杭电 1548 A strange lift(广搜)
    JAVA数组的定义及用法
    WPF之Binding深入探讨
    FBReaderJ源代码编译配置
    【剑指offer】合并两有序单链表
    对HGE游戏引擎的一次封装
    WAV文件格式分析
    Ubuntu9.04更新源
    内核及内核模块
    java实现第七届蓝桥杯愤怒小鸟
  • 原文地址:https://www.cnblogs.com/fengchaowang/p/4138058.html
Copyright © 2020-2023  润新知