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");
	}

posted @ 2013-12-07 14:14  张巍的博客  阅读(5085)  评论(2编辑  收藏  举报