底层连接已关闭服务器提交了协议违规c#

时间:2017-06-13 10:52:44

标签: c# file-upload ftp

执行以下代码时,我收到以下错误

  

(底层连接已关闭服务器已提交协议   违规)

。请帮忙。我几乎把所有关于错误的文章扔到了仍没有得到解决方案。

先谢谢。

private static void Upload()
{
     string sourceFile = System.IO.Path.Combine(filepath, filename);
     string ftpServerIP = "Hostname";
     string ftpUserID = "UserName";
     string ftpPassword = "Password";

     FileInfo fileInf = new FileInfo(sourceFile);
     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
     FtpWebRequest reqFTP;
     // Create FtpWebRequest object from the Uri provided
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
     // Provide the WebPermission Credintials
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
     // 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
     FileStream fs = fileInf.OpenRead();
     //try
     //{
          // Stream to which the file to be upload is written
          Stream strm = reqFTP.GetRequestStream();
          // Read from the file stream 2kb at a time
          contentLen = fs.Read(buff, 0, buffLength);
          // Until 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)
      //{
      //    Console.Write(ex.Message, "Upload Error");
      //}
 }

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案。我使用的是SFTP服务器而不是FTP代码完全不同。

所以我改变了代码。

    using Renci.SshNet;
    public static void UploadSFTPFile()
    {
        _SftpDetails = ReadIniFile(IniFilePath, "Sftp_Settings", SftpDetails);
        string sourcefile = System.IO.Path.Combine(filepath, filename);
        string destinationpath = _SftpDetails["SftpDestinationFolder"];
        string host = _SftpDetails["Host"];
        string username = _SftpDetails["UserName"];
        string password = _SftpDetails["Password"];
        int port = Convert.ToInt32(_SftpDetails["Port"]);  //Port 22 is defaulted for SFTP upload

        try
        {
            using (SftpClient client = new SftpClient(host, port, username, password))
            {
                client.Connect();
                client.ChangeDirectory(destinationpath);
                using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                {
                    client.BufferSize = 4 * 1024;
                    client.UploadFile(fs, Path.GetFileName(sourcefile));
                }
            }
        }
        catch(Exception ex)
        {
            Console.Write(ex.Message);
            return;
        }
    }

您必须从NuGet包管理器安装.sshNet