gpt4 book ai didi

c# - FTP 上传失败 'The underlying connection was closed: An unexpected error occurred on a receive.'

转载 作者:行者123 更新时间:2023-12-03 20:20:18 24 4
gpt4 key购买 nike

我正在尝试将 文件上传到 FTP ,并且直到今天一直成功这样做。在这里(在工作中)我从来没有遇到过问题,但是在现场生产中我们遇到了零星的问题,但是现在 今天突然无法 100% 地工作 并且我无法解决这个问题我的生活。

我已经尝试了几乎所有我能找到的东西。

  • 这不是网络服务。
  • 我已将 FtpWebRequest 的 Timeout 和 ReadWriteTimeout 增加到 -1。
  • 我已经设置了用于写入文件内容的流的 WriteTimeout 和 ReadTimeout。
  • 我们已确保设置出站规则以允许我们通过等进行通信的端口,并且可以确认我们可以使用 FileZilla 从同一机器/网络进行写入/读取。
  • 文件不大。它们最大为 1MB,是简单的文本文件。

  • 下面是我使用的方法。如果您需要更多信息,请告诉我。
    private static void FtpUpload(String username, String password, String address, 
    Int32 port, Boolean usePassive, String filePath)
    {
    try
    {
    String fileName = "";

    fileName = Path.GetFileName(filePath);
    FtpWebRequest request = null;
    request = (FtpWebRequest)FtpWebRequest.Create(String.Format("ftp://{0}", address));

    request.Credentials = new NetworkCredential(
    username,
    password);
    request.UsePassive = usePassive;
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Timeout = -1;
    request.ReadWriteTimeout = -1;
    request.KeepAlive = false;
    request.Proxy = null;

    Console.WriteLine(String.Format("Uploading {0}...", fileName));

    Stream ftpStream = null;

    using (ftpStream = request.GetRequestStream())
    {
    ftpStream.WriteTimeout = -1;
    ftpStream.ReadTimeout = -1;

    using (FileStream file = File.OpenRead(filePath))
    {
    file.CopyTo(ftpStream);
    }
    }
    }
    catch
    {
    throw;
    }
    }

    编辑:

    此代码片段有效。请注意,当我更新此代码段以使用 using 块时,它又会失败。使用块会是原因吗?
    public static void FtpUpload(
    String username,
    String password,
    String address,
    Int32 port,
    Boolean usePassive,
    String filePath)
    {

    string ftpServerIP = String.Format("ftp://{0}", address);
    string ftpUserID = username;
    string ftpPassword = password;
    FileInfo fileInf = new FileInfo(filePath);

    try
    {

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);

    request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UseBinary = false;
    request.UsePassive = false;
    request.ContentLength = fileInf.Length;

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

    FileStream fs = fileInf.OpenRead();

    Stream strm = request.GetRequestStream();

    contentLen = fs.Read(buff, 0, buffLength);

    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);
    }

    strm.Close();
    fs.Close();

    }
    catch
    {
    throw;
    }
    }

    最佳答案

    对于偶然发现此问题的任何其他人,解决方案包含在
    http://www.experts-exchange.com/questions/28693905/FtpWebRequest-The-underlying-connection-as-closed-An-unexpected-error-has-occurred-on-a-receive.html
    为我工作,你必须将以下设置为 false

    var request = (FtpWebRequest)WebRequest.Create(finalPath);
    request.UseBinary = false;
    request.UsePassive = false;
    这解决了FTP上传在本地工作并且一旦部署到UAT就失败的问题

    关于c# - FTP 上传失败 'The underlying connection was closed: An unexpected error occurred on a receive.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383206/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com