• 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 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;       
    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     }   
  • 相关阅读:
    autocad.net 利用linq获取矩形框内的块参照
    autocad.net 只在图纸空间遍历块的方法
    autocad.net中判断当前被激活的空间
    计划搞一个程序来应对客户的修改标记问题
    条件编译解决AutoCAD多版本问题
    初学者往往不知道怎么获得断点,请看下面的链接应该可以解决你的问题!
    2014年3月9日正式入住博客园
    学习:SpringCloud(一)
    简单使用:SpringBoot整合Redis
    Redis 使用过程中遇到的具体问题
  • 原文地址:https://www.cnblogs.com/sandea/p/4439281.html
Copyright © 2020-2023  润新知