VB.NET - FTP上传/下载完整性问题

时间:2015-10-14 09:15:06

标签: vb.net ftp ftpwebrequest

我有一个Windows服务,定期将文件上传到FTP服务器,然后重新下载文件以检查字节是否完全匹配。对于大多数文件来说似乎没问题,但我们最近使用的其中一个文件是一个10mb的CSV文件,文件总是略有不同(几个字节)。

以下是上传,下载和文件比较方法。请注意,下载将UseBinary设置为true,但上传不是 - 这可能是原因吗?

 public string Upload(FileInfo fi, string targetFilename)
    {
        //copy the file specified to target file: target file can be full path or just filename (uses current dir)

        //1. check target
        string target;
        if (targetFilename.Trim() == "")
        {
            //Blank target: use source filename & current dir
            target = this.CurrentDirectory + fi.Name;
        }
        else if (targetFilename.Contains("/"))
        {
            //If contains / treat as a full path
            target = AdjustDir(targetFilename);
        }
        else
        {
            //otherwise treat as filename only, use current directory
            target = CurrentDirectory + targetFilename;
        }

        string URI = Hostname + target;
        //perform copy
        System.Net.FtpWebRequest ftp = GetRequest(URI);

        //Set request to upload a file in binary
        ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
        ftp.UseBinary = false;

        //Notify FTP of the expected size
        ftp.ContentLength = fi.Length;

        //create byte array to store: ensure at least 1 byte!
        const int BufferSize = 2048;
        byte[] content = new byte[BufferSize - 1 + 1];
        int dataRead;

        string result = null;

        //open file for reading
        using (FileStream fs = fi.OpenRead())
        {
            try
            {
                //open request to send
                using (Stream rs = ftp.GetRequestStream())
                {
                    do
                    {
                        dataRead = fs.Read(content, 0, BufferSize);
                        rs.Write(content, 0, dataRead);
                    } while (!(dataRead < BufferSize));
                    rs.Close();
                }

            }
            catch (Exception x)
            {
                result = URI + " - " + x.ToString();
            }
            finally
            {
                //ensure file closed
                fs.Close();
            }

        }

        ftp = null;
        return result;

    }
    #endregion

  public bool Download(string sourceFilename, FileInfo targetFI, bool PermitOverwrite)
    {
        //1. check target
        if (targetFI.Exists && !(PermitOverwrite))
        {
            throw (new ApplicationException("Target file already exists"));
        }

        //2. check source
        string target;
        if (sourceFilename.Trim() == "")
        {
            throw (new ApplicationException("File not specified"));
        }
        else if (sourceFilename.Contains("/"))
        {
            //treat as a full path
            target = AdjustDir(sourceFilename);
        }
        else
        {
            //treat as filename only, use current directory
            target = CurrentDirectory + sourceFilename;
        }

        string URI = Hostname + target;

        //3. perform copy
        System.Net.FtpWebRequest ftp = GetRequest(URI);

        //Set request to download a file in binary mode
        ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
        ftp.UseBinary = true;
        ftp.UsePassive = false;

        //open request and get response stream
        using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                //loop to read & write to file
                using (FileStream fs = targetFI.OpenWrite())
                {
                    try
                    {
                        byte[] buffer = new byte[2048];
                        int read = 0;
                        do
                        {
                            read = responseStream.Read(buffer, 0, buffer.Length);
                            fs.Write(buffer, 0, read);
                        } while (!(read == 0));
                        responseStream.Close();
                        fs.Flush();
                        fs.Close();
                    }
                    catch (Exception)
                    {
                        //catch error and delete file only partially downloaded
                        fs.Close();
                        //delete target file as it's incomplete
                        targetFI.Delete();
                        throw;
                    }
                }

                responseStream.Close();
            }

            response.Close();
        }


        return true;
    }

Public Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean

        ' Checks to see if two files are the same
        Dim file1byte As Integer
        Dim file2byte As Integer
        Dim fs1 As FileStream
        Dim fs2 As FileStream

        If file1 = file2 Then
            Return True
        End If

        fs1 = New FileStream(file1, FileMode.Open)
        fs2 = New FileStream(file2, FileMode.Open)

        ' Simple length test
        If fs1.Length <> fs2.Length Then
            fs1.Close()
            fs2.Close()
            Return False
        End If

        Do
            file1byte = fs1.ReadByte()
            file2byte = fs2.ReadByte()
        Loop While file1byte = file2byte And file1byte <> -1

        fs1.Close()
        fs2.Close()
        Return ((file1byte - file2byte) = 0)

    End Function

0 个答案:

没有答案