Exemplo n.º 1
0
        private static async Task UploadAppendFile()
        {
            var         testBytes   = Encoding.UTF8.GetBytes("123456789");
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "server");

            var filename = await FastDFSClient.UploadAppenderFileAsync(storageNode, testBytes.Take(6).ToArray(), "", "default");

            FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, filename, "default");

            if (fileInfo == null)
            {
                Console.WriteLine($"GetFileInfoAsync Fail, path: {filename}");
                return;
            }

            Console.WriteLine("FileName:{0}", filename);
            Console.WriteLine("FileSize:{0}", fileInfo.FileSize);
            Console.WriteLine("CreateTime:{0}", fileInfo.CreateTime);
            Console.WriteLine("Crc32:{0}", fileInfo.Crc32);

            var appendBytes = testBytes.Skip((int)fileInfo.FileSize).ToArray();
            await FastDFSClient.AppendFileAsync("group1", filename, appendBytes, "default");

            var test = await HttpClient.GetByteArrayAsync(StorageLink + filename);

            if (Encoding.UTF8.GetString(test) == Encoding.UTF8.GetString(testBytes))
            {
                Console.WriteLine($"UploadAppendFile Success");
            }
            else
            {
                throw new ApplicationException($"UploadAppendFile Fail : Bytes Diff ");
            }
            await FastDFSClient.RemoveFileAsync("group1", filename, "default");
        }
Exemplo n.º 2
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name=""></param>
        /// <returns>文件流</returns>
        public FileStream DownloadFile(string fileName)
        {
            FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(storageNode, fileName);

            FileStream fileStream = new FileStream(Path.GetTempFileName(), FileMode.Create);

            if (fileInfo.FileSize >= 1024)//如果文件大小大于1KB  分次写入
            {
                long offset = 0;
                long len    = 1024;
                while (len > 0)
                {
                    byte[] buffer = new byte[len];
                    buffer = FastDFSClient.DownloadFile(storageNode, fileName, offset, len);
                    fileStream.Write(buffer, 0, int.Parse(len.ToString()));
                    fileStream.Flush();
                    offset = offset + len;
                    len    = (fileInfo.FileSize - offset) >= 1024 ? 1024 : (fileInfo.FileSize - offset);
                }
                return(fileStream);
            }
            else//如果文件大小小小于1KB  直接写入文件
            {
                byte[] buffer = new byte[fileInfo.FileSize];
                buffer = FastDFSClient.DownloadFile(storageNode, fileName);
                //FileStream fileStream = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write);
                fileStream.Write(buffer, 0, buffer.Length);
                fileStream.Flush();
                fileStream.Close();
                return(fileStream);
            }
        }
Exemplo n.º 3
0
        protected static async Task BigFileAppendUploadDownLoad()
        {
            var         temp        = Enumerable.Repeat((byte)99, 1024 * 1024 * 100);
            var         testBytes   = temp.ToArray();
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "default");


            var filename = await FastDFSClient.UploadAppenderFileAsync(storageNode, testBytes.Take(1024 * 1024 * 2).ToArray(), "txt", "default");

            for (int i = 0; i < 49; i++)
            {
                FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, filename, "default");

                var appendBytes = testBytes.Skip((int)fileInfo.FileSize).Take(1024 * 1024 * 2).ToArray();
                await FastDFSClient.AppendFileAsync("group1", filename, appendBytes, "default");
            }

            using (var fileStream = File.OpenWrite("c:\\fastdfs_test.txt"))
            {
                for (int i = 0; i < 50; i++)
                {
                    var buffer = await FastDFSClient.DownloadFileAsync(storageNode, filename, "default",
                                                                       1024 * 1024 * 2 *i,
                                                                       1024 * 1024 * 2);

                    fileStream.Write(buffer, 0, buffer.Length);
                }
            }

            await FastDFSClient.RemoveFileAsync("group1", filename, "default");

            temp      = null;
            testBytes = null;
        }
Exemplo n.º 4
0
        private void DownloadFile2(string fileName, string targetPath)
        {
            //  byte[] buffer = FastDFSClient.DownloadFile(node, fileName, 0L, 0L);
            targetPath = "d:\\" + Path.GetFileName(fileName);
            FDFSFileInfo fileInfo = FastDFS.Client.FastDFSClient.GetFileInfo(storageNode, fileName);

            if (fileInfo.FileSize >= 1024)//如果文件大小大于1KB  分次写入
            {
                FileStream fs     = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write);
                long       offset = 0;
                long       len    = 1024;
                while (len > 0)
                {
                    byte[] buffer = new byte[len];
                    buffer = FastDFS.Client.FastDFSClient.DownloadFile(storageNode, 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 = FastDFS.Client.FastDFSClient.DownloadFile(storageNode, fileName);
                FileStream fs = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                fs.Close();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="storageNode">GetStorageNode方法返回的存储节点</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static FDFSFileInfo GetFileInfo(StorageNode storageNode, string fileName)
        {
            var storageReqeust = QUERY_FILE_INFO.Instance.GetRequest(storageNode.EndPoint, storageNode.GroupName, fileName);

            var result = new FDFSFileInfo(storageReqeust.GetResponse());

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取文件信息
        /// </summary>
        /// <param name="storageNode">GetStorageNode方法返回的存储节点</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        public static FDFSFileInfo GetFileInfo(StorageNode storageNode, string fileName)
        {
            var storageReqeust = QUERY_FILE_INFO.Instance.GetRequest(storageNode.EndPoint, storageNode.GroupName, fileName);

            var result = new FDFSFileInfo(storageReqeust.GetResponse());

            return result;
        }
Exemplo n.º 7
0
        public async Task <IActionResult> GetFileInfoAsync([FromRoute] string filename)
        {
            filename = HttpUtility.UrlDecode(filename);
            //StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync(NodeGroup);
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync();

            FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, filename);

            return(Ok(fileInfo));
        }
Exemplo n.º 8
0
        /// <summary>
        /// TestGetFileInfo
        /// </summary>
        private static void TestGetFileInfo()
        {
            StorageNode storageNode = FastDFSClient.GetStorageNode("group1");

            var          str      = "M00/00/81/CgE-EFm42mCARBiqAADl7mZ3mRU647.png";
            FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(storageNode, str);

            Console.WriteLine("FileName:{0}", str);
            Console.WriteLine("FileSize:{0}", fileInfo.FileSize);
            Console.WriteLine("CreateTime:{0}", fileInfo.CreateTime);
            Console.WriteLine("Crc32:{0}", fileInfo.Crc32);

            Console.WriteLine("Complete");
        }
Exemplo n.º 9
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name=""></param>
        /// <returns>文件流</returns>
        public byte[] DownloadFileByte(string fileName)
        {
            FDFSFileInfo fileInfo = FastDFSClient.GetFileInfo(storageNode, fileName);

            byte[] buffer = new byte[fileInfo.FileSize];
            buffer = FastDFSClient.DownloadFile(storageNode, fileName);

            FileStream fs = new FileStream("D://down123.png", FileMode.Create, FileAccess.Write);

            fs.Write(buffer, 0, buffer.Length);
            fs.Flush();
            fs.Close();

            return(buffer);
        }
Exemplo n.º 10
0
        /// <summary>
        /// TestGetFileInfo
        /// </summary>
        private static async Task TestGetFileInfo()
        {
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1");

            var          str      = "M00/0E/82/CgE-EFsTqKmAEHO7AADMG7XS9Fc7491.jpg";
            FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, str);

            if (fileInfo == null)
            {
                Console.WriteLine("Not Exist");
                return;
            }
            Console.WriteLine("FileName:{0}", str);
            Console.WriteLine("FileSize:{0}", fileInfo.FileSize);
            Console.WriteLine("CreateTime:{0}", fileInfo.CreateTime);
            Console.WriteLine("Crc32:{0}", fileInfo.Crc32);

            Console.WriteLine("Complete");
        }
Exemplo n.º 11
0
        /// <summary>
        /// 获取对象流
        /// </summary>
        /// <param name="groupName">组名称</param>
        /// <param name="objectName">对象名称</param>
        /// <returns>对象流</returns>
        public async Task <Stream> GetObjectStreamAsync(string objectName, string groupName = "")
        {
            RestoreFileName(objectName, out string fileName, out groupName);
            var storageNode = await GetStorageNodeAsync(groupName);

            FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, fileName);

            Stream stream = new MemoryStream();
            int    limit  = 1024 * 100;//每次获取100KB的数据

            //如果文件大小大于100KB  分次写入
            if (fileInfo.FileSize >= limit)
            {
                long offset = 0;
                long len    = limit;
                while (len > 0)
                {
                    byte[] buffer = await FastDFSClient.DownloadFileAsync(storageNode, fileName, offset, len);

                    stream.Write(buffer, 0, int.Parse(len.ToString()));
                    stream.Flush();
                    offset += len;
                    len     = (fileInfo.FileSize - offset) >= limit ? limit : (fileInfo.FileSize - offset);
                }
            }
            else
            {
                //如果文件大小小小于100KB  直接写入文件
                byte[] buffer = await FastDFSClient.DownloadFileAsync(storageNode, fileName);

                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();
            }

            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            //===========================Initial========================================
            List <IPEndPoint> trackerIPs = new List <IPEndPoint>();
            IPEndPoint        endPoint   = new IPEndPoint(IPAddress.Parse("10.0.21.101"), 22122);

            trackerIPs.Add(endPoint);
            ConnectionManager.Initialize(trackerIPs);
            StorageNode node = FastDFSClient.GetStorageNode();

            //===========================UploadFile=====================================
            byte[] content  = null;
            string filename = @"D:\软件开发\OS\FastDFS\file\4.jpg";

            if (File.Exists(filename))
            {
                FileStream streamUpload = new FileStream(filename, FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
            }
            //string fileName = FastDFSClient.UploadAppenderFile(node, content, "mdb");
            string fileName = FastDFSClient.UploadFile(node, content, "jpg");

            FastDFSClient.DownloadFileEx(node, fileName, @"D:\软件开发\OS\FastDFS\file", "5.jpg");

            Console.ReadKey();

            //===========================BatchUploadFile=====================================
            string[] _FileEntries = Directory.GetFiles(@"E:\fastimage\三维", "*.jpg");
            DateTime start        = DateTime.Now;

            foreach (string file in _FileEntries)
            {
                string name = Path.GetFileName(file);
                content = null;
                FileStream streamUpload = new FileStream(file, FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
                //string fileName = FastDFSClient.UploadAppenderFile(node, content, "mdb");
                fileName = FastDFSClient.UploadFile(node, content, "jpg");
            }
            DateTime end            = DateTime.Now;
            TimeSpan consume        = ((TimeSpan)(end - start));
            double   consumeSeconds = Math.Ceiling(consume.TotalSeconds);

            //===========================QueryFile=======================================
            fileName = "M00/00/00/wKhR6U__-BnxYu0eAxRgAJZBq9Q180.mdb";
            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));
            //==========================AppendFile=======================================
            FastDFSClient.AppendFile("group1", fileName, content);
            FastDFSClient.AppendFile("group1", fileName, content);

            //===========================DownloadFile====================================
            fileName = "M00/00/00/wKhR6U__-BnxYu0eAxRgAJZBq9Q180.mdb";
            byte[] buffer = FastDFSClient.DownloadFile(node, fileName, 0L, 0L);
            if (File.Exists(@"D:\SZdownload.mdb"))
            {
                File.Delete(@"D:\SZdownload.mdb");
            }
            FileStream stream = new FileStream(@"D:\SZdownload.mdb", FileMode.CreateNew);

            using (BinaryWriter write = new BinaryWriter(stream, Encoding.BigEndianUnicode))
            {
                write.Write(buffer);
                write.Close();
            }
            stream.Close();
            //===========================RemoveFile=======================================
            FastDFSClient.RemoveFile("group1", fileName);

            //===========================Http测试,流读取=======================================
            string url = "http://img13.360buyimg.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");//保存
            //===========================Http测试,直接下载=======================================
            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");

            Console.WriteLine("Complete");
            Console.Read();
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            //===========================Initial========================================
            List <IPEndPoint> trackerIPs = new List <IPEndPoint>();
            IPEndPoint        endPoint   = new IPEndPoint(IPAddress.Parse("192.168.81.233"), 22122);

            trackerIPs.Add(endPoint);
            ConnectionManager.Initialize(trackerIPs);
            StorageNode node = FastDFSClient.GetStorageNode("group1");

            //===========================UploadFile=====================================
            byte[] content = null;
            if (File.Exists(@"D:\材料科学与工程基础.doc"))
            {
                FileStream streamUpload = new FileStream(@"D:\材料科学与工程基础.doc", FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
            }
            //string fileName = FastDFSClient.UploadAppenderFile(node, content, "mdb");
            //主文件
            string fileName = FastDFSClient.UploadFile(node, content, "doc");

            //UploadFileByName
            //string fileName = FastDFSClient.UploadFileByName(node, @"D:\材料科学与工程基础.doc");

            //从文件
            string slavefileName = FastDFSClient.UploadSlaveFile("group1", content, fileName, "-part1", "doc");

            //===========================BatchUploadFile=====================================
            string[] _FileEntries = Directory.GetFiles(@"E:\fastimage\三维", "*.jpg");
            DateTime start        = DateTime.Now;

            foreach (string file in _FileEntries)
            {
                string name = Path.GetFileName(file);
                content = null;
                FileStream streamUpload = new FileStream(file, FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
                //string fileName = FastDFSClient.UploadAppenderFile(node, content, "mdb");
                fileName = FastDFSClient.UploadFile(node, content, "jpg");
            }
            DateTime end            = DateTime.Now;
            TimeSpan consume        = ((TimeSpan)(end - start));
            double   consumeSeconds = Math.Ceiling(consume.TotalSeconds);

            //===========================QueryFile=======================================
            fileName = "M00/03/80/wKhR6VAhwA72jCDyAABYAMjfFsM288.doc";
            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));
            //==========================AppendFile=======================================
            FastDFSClient.AppendFile("group1", fileName, content);
            FastDFSClient.AppendFile("group1", fileName, content);

            //===========================DownloadFile====================================
            fileName = "M00/00/00/wKhR6VAAAN7J2FLQAABYAMjfFsM849.doc";
            string localName = @"D:\SZdownload.doc";

            if (File.Exists(@"D:\SZdownload.doc"))
            {
                File.Delete(@"D:\SZdownload.doc");
            }
            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();
            }
            //byte[] buffer = FastDFSClient.DownloadFile(node, fileName, 0L, 0L);
            //if (File.Exists(@"D:\SZdownload.mdb"))
            //    File.Delete(@"D:\SZdownload.mdb");
            //FileStream stream = new FileStream(@"D:\SZdownload.mdb", FileMode.CreateNew);
            //using (BinaryWriter write = new BinaryWriter(stream, Encoding.BigEndianUnicode))
            //{
            //    write.Write(buffer);
            //    write.Close();
            //}
            //stream.Close();
            //===========================RemoveFile=======================================
            //FastDFSClient.RemoveFile("group1", fileName);

            //===========================Http测试,流读取=======================================
            string url = "http://img13.360buyimg.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");//保存
            //===========================Http测试,直接下载=======================================
            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");
            }
            //===========================防盗链请求=======================================
            start   = new DateTime(1970, 1, 1);
            end     = DateTime.Now;
            consume = (TimeSpan)(end - start);
            int    ts             = (int)(consume.TotalSeconds);
            string pwd            = FastDFS.Client.Util.GetToken("M00/03/81/wKhR6VAh0sfyH0AxAABYAMjfFsM301-part1.doc", ts, "FastDFS1qaz2wsxsipsd");
            string anti_steel_url = "http://192.168.81.233/M00/03/81/wKhR6VAh0sfyH0AxAABYAMjfFsM301-part1.doc?token=" + pwd + "&ts=" + ts;
            string url1           = "http://192.168.81.233/M00/01/E0/wKhR6VANJBiInHb5AAClVeZnxGg341.pdf";

            using (WebClient web = new WebClient())
            {
                web.DownloadFile(anti_steel_url, "C:\\salve.doc");
            }
            Console.WriteLine("Complete");
            Console.Read();
        }