【问题标题】:Stream writes only 512 bytes to a file on FTP server using C#Stream 使用 C# 仅将 512 字节写入 FTP 服务器上的文件
【发布时间】:2016-12-19 10:56:14
【问题描述】:

我正在尝试使用 C# 将文件从本地计算机复制到 FTP 服务器。 当我使用下面的代码时,文件被完全复制到 FTP 服务器,但 ** 原始行被切割成只有 512 字节长的片段,而它们应该是 1152、1126 或 1024 字节长。 ** 我使用的示例文件现在有 16 行而不是 7 行。

    public void uploadLOTFILE(string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://mysite.mine/mypathandfilename");
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.KeepAlive = true;
        System.IO.Stream rs = request.GetRequestStream();

        var lines = File.ReadLines(@"myLocalFile.txt");
        foreach (var line in lines)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(line);                
            Console.WriteLine("buffer.length:" + buffer.Length.ToString());
            rs.Write(buffer,0,buffer.Length);
        }
        rs.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
      }

来自 Console.writeline 示例文件的输出:

buffer.length:1152

buffer.length:1126

buffer.length:1152

buffer.length:1152

buffer.length:1152

buffer.length:1152

buffer.length:1024

我也使用了来自 msdn (https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx) 的精确副本,但结果相同。

编辑: 还尝试了以下代码: 字符串文件路径 = @“我的文件路径”; var fileName = Path.GetFileName(filePath); var request = (FtpWebRequest)WebRequest.Create("ftp://myftp");

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        using (var fileStream = File.OpenRead(filePath))
        {
            using (var requestStream = request.GetRequestStream())
            {
                fileStream.CopyTo(requestStream);
                requestStream.Close();
            }
        }

它给出了相同的结果。该文件被完全复制,但每 512 个字节添加一个换行符。

使用 FileZilla,我可以对相同类型的文件进行正确的 FTP 传输。

【问题讨论】:

  • "文件完全复制到 FTP 服务器,但行只有 512 字节长" - 这没有意义。那么文件是否完全复制?
  • 如何检查行只有 512 行?
  • 你到底想做什么?为什么要逐行上传文件?
  • 当您使用常规 FTP 客户端上传文件时,您得到正确的结果吗?
  • 您意识到您正在从 UTF8 转换为 ASCII,对吗?另外,“myLocalFile.txt”是否绝对编码为 UTF8?不是,比如说,UTF16?还是ANSI?

标签: c# ftp ftpwebrequest


【解决方案1】:

我使用可使用 cmd 访问的内置 windows FTP 解决了我的问题。

            string[] lines = { username, password, "remote directory", "put " + "\"" + filePath + "\"" , "bye"};
        // WriteAllLines creates a file, writes a collection of strings to the file,
        // and then closes the file.  You do NOT need to call Flush() or Close().
        System.IO.File.WriteAllLines("full file path of your script", lines);
        Process cmd = new Process();
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.RedirectStandardInput = true;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.CreateNoWindow = true;
        cmd.StartInfo.UseShellExecute = false;
        cmd.Start();

        cmd.StandardInput.WriteLine(@"ftp -s:" + "\"" + "full file path of your script" + "\"" + " remote device");
        cmd.StandardInput.Flush();
        cmd.StandardInput.Close();
        cmd.WaitForExit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多