C# ftp上传和下载

为了测试我们写的代码是否具有ftp上传下载功能,我们需要去下载一个虚拟FTP服务器,如:Serv-U。配置好Serv-U后,我们就可以编码测试了。
引入命名空间:using System.Net;

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath">文件下载后存放路径</param>
        /// <param name="SaveFileName">文件下载后的名称</param>
        /// <param name="parFileName">要下载的文件名</param>
        /// <returns></returns>
        private int Download(string filePath, string SaveFileName, string parFileName)
        {
            FtpWebRequest reqFTP;
            FileStream outputStream = new FileStream(filePath + "\\" + SaveFileName, FileMode.Create);

            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + strFtpDownPath + "/" + parFileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(strFTPUser, strFTPPSW);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

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

                下载成功后,删除服务器上的文件。
                //FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + strFtpPath + "/" + fileName));
                //request.Method = WebRequestMethods.Ftp.DeleteFile;

                //FtpWebResponse responseD = (FtpWebResponse)request.GetResponse();


                ftpStream.Close();
                outputStream.Close();
                response.Close();
                return readCount;
            }
            catch (Exception ex)
            {
                outputStream.Close();
                //MessageBox.Show(ex.Message);
                LogAPI.WriteLog(ex.Message);
                return -1;
            }
        }

        /// <summary>
        /// Method to upload the specified file to the specified FTP Server
        /// </summary>
        /// <param name="filename">file full name to be uploaded</param>
        private  bool Upload(string filePath, string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + strFTPIP + "/" + filePath + "/" + fileInf.Name;
            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strFTPIP + "/" + filePath + "/" + fileInf.Name));

            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential(strFTPUser, strFTPPSW);

            // By default KeepAlive is true, where the control connection is not closed
            // after a command is executed.
            reqFTP.KeepAlive = false;

            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            // Specify the data transfer type.
            reqFTP.UseBinary = true;

            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;

            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            using (FileStream fs = fileInf.OpenRead())
            {

                try
                {
                    // Stream to which the file to be upload is written
                    using (Stream strm = reqFTP.GetRequestStream())
                    {

                        // Read from the file stream 2kb at a time
                        contentLen = fs.Read(buff, 0, buffLength);

                        // Till Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);
                        }

                        // Close the file stream and the Request Stream
                        //strm.Close();
                        //fs.Close();
                    }
                }
                catch (Exception ex)
                {
                    //fs.Close();
                    LogAPI.WriteLog("Error in getting the Upload!" + ex.Message);
                    return false;
                }
            }

            return true;

        }


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值