zoukankan      html  css  js  c++  java
  • FastDFS client for .net

    fastDfs Code: https://code.google.com/p/fastdfs/

    FastDfs_Client_DotNet:https://code.google.com/p/fastdfs/downloads/detail?name=FastDFS_Client_Dotnet.rar

    FastDfs示例代码

    初始化

    List<IPEndPoint> trackerIPs = new List<IPEndPoint>();
    IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.0.123"),22122);
    trackerIPs.Add(endPoint);
    ConnectionManager.Initialize(trackerIPs);
    StorageNode node = FastDFSClient.GetStorageNode("group1");
    

    获取文件信息

    FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(node, fileName);
    Console.WriteLine(string.Format("FileName:{0}", fileName));
    Console.WriteLine(string.Format("FileSize:{0}", fileInfo.FileSize));
    Console.WriteLine(string.Format("CreateTime:{0}", fileInfo.CreateTime));
    Console.WriteLine(string.Format("Crc32:{0}", fileInfo.Crc32));
    

    上传文件

    byte[] buffer = FastDFSClient.DownloadFile(node, fileName, 0L, 0L);
    string localName = @"D:a.ppt";
    if (fileInfo.FileSize >= 1024)//如果文件大小大于1KB  分次写入
    {
        FileStream fs = new FileStream(localName, FileMode.OpenOrCreate, FileAccess.Write);
        //string name_ = LocalName.Substring(LocalName.LastIndexOf("\") + 1, LocalName.Length - LocalName.LastIndexOf("\") - 1);
        long offset = 0;
        long len = 1024;
        while (len > 0)
        {
            byte[] buffer = new byte[len];
            buffer = FastDFSClient.DownloadFile(node, fileName, offset, len);
            fs.Write(buffer, 0, int.Parse(len.ToString()));
            fs.Flush();
            // setrichtext(name_ + "已经下载:" + (offset / fileInfo.FileSize) + "%");
            offset = offset + len;
            len = (fileInfo.FileSize - offset) >= 1024 ? 1024 : (fileInfo.FileSize - offset);
        }
        fs.Close();
    
    }
    else//如果文件大小小小于1KB  直接写入文件
    {
        byte[] buffer = new byte[fileInfo.FileSize];
        buffer = FastDFSClient.DownloadFile(node, fileName);
        FileStream fs = new FileStream(localName, FileMode.OpenOrCreate, FileAccess.Write);
        fs.Write(buffer, 0, buffer.Length);
        fs.Flush();
        fs.Close();
    }
    

    删除文件

    FastDFSClient.RemoveFile("group1", fileName);
    

    HTTP获取文件
    1. Http流读取

    string url = "http://aa.com/da/g5/M02/0D/16/rBEDik_nOJ0IAAAAAAA_cbJCY-UAACrRgMhVLEAAD-J352.jpg";
    System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
    System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
     Image myImage = Image.FromStream(res.GetResponseStream());
     myImage.Save("c:\fast.jpg");//保存
    
    >

    WebClient直接下载

    
    using (WebClient web = new WebClient())
    	{
    	   web.DownloadFile("http://img13.360buyimg.com/da/g5/M02/0D/16/rBEDik_nOJ0IAAAAAAA_cbJCY-UAACrRgMhVLEAAD-J352.jpg", "C:\abc.jpg");
    	   web.DownloadFile("http://192.168.81.233/M00/00/00/wKhR6VADbNr5s7ODAAIOGO1_YmA574.jpg", "C:\abc.jpg");
    	}
    
    
  • 相关阅读:
    UVA12125 March of the Penguins (最大流+拆点)
    UVA 1317 Concert Hall Scheduling(最小费用最大流)
    UVA10249 The Grand Dinner(最大流)
    UVA1349 Optimal Bus Route Design(KM最佳完美匹配)
    UVA1212 Duopoly(最大流最小割)
    UVA1395 Slim Span(kruskal)
    UVA1045 The Great Wall Game(二分图最佳匹配)
    UVA12168 Cat vs. Dog( 二分图最大独立集)
    hdu3488Tour(KM最佳完美匹配)
    UVA1345 Jamie's Contact Groups(最大流+二分)
  • 原文地址:https://www.cnblogs.com/ac1985482/p/FastDFS_client_for_dotnet.html
Copyright © 2011-2022 走看看