中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C# FTP上傳下載 代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Net;  
    using System.IO;  
      
    namespace JianKunKing.Common.Ftp  
    {  
        /// <summary>  
        /// ftp方式文件下載上傳  
        /// </summary>  
        public static class FileUpDownload  
        {  
            #region 變量屬性  
            /// <summary>  
            /// Ftp服務(wù)器ip  
            /// </summary>  
            public static string FtpServerIP = string.Empty;  
            /// <summary>  
            /// Ftp 指定用戶名  
            /// </summary>  
            public static string FtpUserID = string.Empty;  
            /// <summary>  
            /// Ftp 指定用戶密碼  
            /// </summary>  
            public static string FtpPassword = string.Empty;  
     
            #endregion  
     
            #region 從FTP服務(wù)器下載文件,指定本地路徑和本地文件名  
            /// <summary>  
            /// 從FTP服務(wù)器下載文件,指定本地路徑和本地文件名  
            /// </summary>  
            /// <param name="remoteFileName">遠(yuǎn)程文件名</param>  
            /// <param name="localFileName">保存本地的文件名(包含路徑)</param>  
            /// <param name="ifCredential">是否啟用身份驗(yàn)證(false:表示允許用戶匿名下載)</param>  
            /// <param name="updateProgress">報(bào)告進(jìn)度的處理(第一個(gè)參數(shù):總大小,第二個(gè)參數(shù):當(dāng)前進(jìn)度)</param>  
            /// <returns>是否下載成功</returns>  
            public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null)  
            {  
                FtpWebRequest reqFTP, ftpsize;  
                Stream ftpStream = null;  
                FtpWebResponse response = null;  
                FileStream outputStream = null;  
                try  
                {  
                    outputStream = new FileStream(localFileName, FileMode.Create);  
                    if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)  
                    {  
                        throw new Exception("ftp下載目標(biāo)服務(wù)器地址未設(shè)置!");  
                    }  
                    Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);  
                    ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);  
                    ftpsize.UseBinary = true;  
      
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);  
                    reqFTP.UseBinary = true;  
                    if (ifCredential)//使用用戶身份認(rèn)證  
                    {  
                        ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);  
                        reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);  
                    }  
                    ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;  
                    FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();  
                    long totalBytes = re.ContentLength;  
                    re.Close();  
      
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
                    response = (FtpWebResponse)reqFTP.GetResponse();  
                    ftpStream = response.GetResponseStream();  
      
                    //更新進(jìn)度    
                    if (updateProgress != null)  
                    {  
                        updateProgress((int)totalBytes, 0);//更新進(jìn)度條     
                    }  
                    long totalDownloadedByte = 0;  
                    int bufferSize = 2048;  
                    int readCount;  
                    byte[] buffer = new byte[bufferSize];  
                    readCount = ftpStream.Read(buffer, 0, bufferSize);  
                    while (readCount > 0)  
                    {  
                        totalDownloadedByte = readCount + totalDownloadedByte;  
                        outputStream.Write(buffer, 0, readCount);  
                        //更新進(jìn)度    
                        if (updateProgress != null)  
                        {  
                            updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新進(jìn)度條     
                        }  
                        readCount = ftpStream.Read(buffer, 0, bufferSize);  
                    }  
                    ftpStream.Close();  
                    outputStream.Close();  
                    response.Close();  
                    return true;  
                }  
                catch (Exception)  
                {  
                    return false;  
                    throw;  
                }  
                finally  
                {  
                    if (ftpStream != null)  
                    {  
                        ftpStream.Close();  
                    }  
                    if (outputStream != null)  
                    {  
                        outputStream.Close();  
                    }  
                    if (response != null)  
                    {  
                        response.Close();  
                    }  
                }  
            }  
     
            #endregion  
     
            #region 上傳文件到FTP服務(wù)器  
            /// <summary>  
            /// 上傳文件到FTP服務(wù)器  
            /// </summary>  
            /// <param name="localFullPath">本地帶有完整路徑的文件名</param>  
            /// <param name="updateProgress">報(bào)告進(jìn)度的處理(第一個(gè)參數(shù):總大小,第二個(gè)參數(shù):當(dāng)前進(jìn)度)</param>  
            /// <returns>是否下載成功</returns>  
            public static bool FtpUploadFile(string localFullPath, Action<int, int> updateProgress = null)  
            {  
                FtpWebRequest reqFTP;  
                Stream stream = null;  
                FtpWebResponse response = null;  
                FileStream fs = null;  
                try  
                {  
                    FileInfo finfo = new FileInfo(localFullPath);  
                    if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)  
                    {  
                        throw new Exception("ftp上傳目標(biāo)服務(wù)器地址未設(shè)置!");  
                    }  
                    Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);  
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);  
                    reqFTP.KeepAlive = false;  
                    reqFTP.UseBinary = true;  
                    reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用戶,密碼  
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服務(wù)器發(fā)出下載請(qǐng)求命令  
                    reqFTP.ContentLength = finfo.Length;//為request指定上傳文件的大小  
                    response = reqFTP.GetResponse() as FtpWebResponse;  
                    reqFTP.ContentLength = finfo.Length;  
                    int buffLength = 1024;  
                    byte[] buff = new byte[buffLength];  
                    int contentLen;  
                    fs = finfo.OpenRead();  
                    stream = reqFTP.GetRequestStream();  
                    contentLen = fs.Read(buff, 0, buffLength);  
                    int allbye = (int)finfo.Length;  
                    //更新進(jìn)度    
                    if (updateProgress != null)  
                    {  
                        updateProgress((int)allbye, 0);//更新進(jìn)度條     
                    }  
                    int startbye = 0;  
                    while (contentLen != 0)  
                    {  
                        startbye = contentLen + startbye;  
                        stream.Write(buff, 0, contentLen);  
                        //更新進(jìn)度    
                        if (updateProgress != null)  
                        {  
                            updateProgress((int)allbye, (int)startbye);//更新進(jìn)度條     
                        }  
                        contentLen = fs.Read(buff, 0, buffLength);  
                    }  
                    stream.Close();  
                    fs.Close();  
                    response.Close();  
                    return true;  
      
                }  
                catch (Exception)  
                {  
                    return false;  
                    throw;                 
                }  
                finally  
                {  
                    if (fs != null)  
                    {  
                        fs.Close();  
                    }  
                    if (stream != null)  
                    {  
                        stream.Close();  
                    }  
                    if (response != null)  
                    {  
                        response.Close();  
                    }  
                }  
            }  
            #endregion   
      
        }  
    }  

調(diào)用實(shí)例:

下載(不需要帶iis部分的路徑):

    FileUpDownload.FtpServerIP = "192.168.1.1";  
               FileUpDownload.FtpUserID = "ftpTest001";  
               FileUpDownload.FtpPassword = "aaaaaa";  
               FileUpDownload.FtpDownload("Beyond Compare(綠色免安裝).zip",  
                   Application.StartupPath + "/downloads/crm2.ra6", false);  

上傳(不需要帶iis部分的路徑):
    OpenFileDialog op = new OpenFileDialog();  
                op.InitialDirectory = Application.StartupPath;  
                op.RestoreDirectory = true;  
                op.Filter = "壓縮文件(*.zip)|*.zip|壓縮文件(*.rar)|*.rar|所有文件(*.*)|*.*";  
                if (op.ShowDialog() == DialogResult.OK)  
                {                 
                    string aa = op.FileName;                 
                    FileUpDownload.FtpServerIP = "192.168.1.1";  
                    FileUpDownload.FtpUserID = "ftpTest001";  
                    FileUpDownload.FtpPassword = "aaaaaa";  
                    //全路徑  
                    FileUpDownload.FtpUploadFile(aa);                
                }  

標(biāo)簽: ftp服務(wù)器 ftp服務(wù)器下載 服務(wù)器 服務(wù)器地址

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:超簡(jiǎn)潔代碼實(shí)現(xiàn)CircleImageView

下一篇:php生成動(dòng)態(tài)驗(yàn)證碼