12.4 C井实现FTP下载文件

C# FTP下载文件

// FTP 下载
        private int FtpFileDownload(string savePath, string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
        {

            string ftpServerPath = "ftp://" + ftpServerIP + ":23" + fileName; //":23"指定的端口,不填默认端口为21
            string ftpUser = ftpUserID;
            string ftpPwd = ftpPassword;
            string saveFilePath = savePath;

            FileStream outputStream = null;
            FtpWebResponse response = null;
            FtpWebRequest reqFTP;

            try
            {
                outputStream = new FileStream(saveFilePath, FileMode.Create);

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerPath));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Timeout = 3000;
                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
                response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                int bufferSize = 2048;
                int readCount;
                ftpFileReadSize = 0;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    ftpFileReadSize += readCount;
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("FtpClient异常:" + ex.Message);
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }

                return -1;
            }

            return 0;
        }

        // 获取FTP文件大小
        private long GetFtpFileSize(string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            long fileSize = 0;
            string ftpServerPath = "ftp://" + ftpServerIP + ":23" + fileName;
            string ftpUser = ftpUserID;
            string ftpPwd = ftpPassword;

            FtpWebResponse response = null;
            FtpWebRequest reqFTP;

            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerPath));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = false;
                reqFTP.Timeout = 3000;
                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;//要求返回文件大小属性
                response = (FtpWebResponse)reqFTP.GetResponse();
                fileSize = response.ContentLength;
                response.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("FtpClient异常:" + ex.Message);
                if (response != null)
                {
                    response.Close();
                }

                return -1;
            }

            return fileSize;
        }

results matching ""

    No results matching ""