1

我正在制作一个 FTP 客户端并尝试将文件上传到服务器(~300 MB),但是当传输了近 100 MB 的文件时出现以下错误:

The underlying connection was closed: An unexpected error occurred on a receive.

这是我的代码:

private void UploadFile(string filepath, string filename)
    {
        try
        {
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://" + server + "/" + filename);

            //ftp.KeepAlive = false;
            //ftp.Timeout = 1000000;
            //ftp.UsePassive = true;
            //ftp.ReadWriteTimeout = 100000;

            Path.GetFileName(filepath);
            ftp.Credentials = new NetworkCredential(username, password);
            ftp.Method = WebRequestMethods.Ftp.UploadFile;


            FileStream stream = File.OpenRead(filepath);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();


            Stream requestStream = ftp.GetRequestStream();
            //requestStream.ReadTimeout = 1000000;
            //requestStream.WriteTimeout = 1000000;
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();         

            FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
            response.Close();
        }
        catch (Exception ex) { CreateRunLogFile(ex.Message); }

        CreateRunLogFile("Uploading of file " + filepath + " ended.");
    }

我尝试使用,

ftp.KeepAlive = false;
ftp.Timeout = 1000000;
ftp.UsePassive = true;

但这没有帮助。

4

1 回答 1

1

检查目标防火墙设置。如果是运行 vsFTPd 的 LINUX 服务器,则服务器 ftp 服务在配置文件中有 FILESIZE 和 TIMEOUT 设置。

请务必在调整 FILESIZE 和 TIMEOUT 设置后重新启动 vsFTPd 服务。

于 2015-01-15T21:46:11.120 回答