FTP Changing PGP File During Transfer in C#

Deadly 提交于 2019-12-24 17:43:36

问题


I have PGP files that I've verified as being valid, but at some point during the FTP upload, they become corrupt. When retrieved, I get an error message stating "Found no PGP information in these file(s)."

For what it's worth, the PGP is version 6.5.8, but I think that this is unimportant, as the files seem alright before they're uploaded.

My code is as follows for the file transfer, is there a setting or field that I've missed?

static void FTPUpload(string file)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.itginc.com" + "/" + Path.GetFileName(file));

        request.UseBinary = true;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(ApplicationSettings["Username"], ApplicationSettings["Password"]);

        StreamReader sr = new StreamReader(file);

        byte[] fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
        sr.Close();

        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();

        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

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

        Console.WriteLine("Upload file complete, status {0}", resp.StatusDescription);

        resp.Close();
        string[] filePaths= Directory.GetFiles(tempPath);
        foreach (string filePath in filePaths) 
            File.Delete(filePath);
    }

Any help is appreciated


回答1:


Hmmmm try not reading it into a byte array and instead doing something like this

        using (var reader = File.Open(source, FileMode.Open))
        {
            var ftpStream = request.GetRequestStream();
            reader.CopyTo(ftpStream);
            ftpStream.Close();
        }



回答2:


PGP encodes data to binary stream, so your reading it via StreamReader and UTF8 probably breaks the data. FTP is unlikely to alter the data as you explicitly binary mode (though UseBinary is true by default so your setting should not do anything at all).



来源:https://stackoverflow.com/questions/7353993/ftp-changing-pgp-file-during-transfer-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!