1

我想知道 ftp 进程是否可以给我任何上传成功确认。我正在查看其中一个代码项目,除了潜在的异常之外,看不到任何获得真正成功确认的方法。

public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);

            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    } 
4

1 回答 1

3

在文件上传之后,但在你清理之前(关闭流,清空FtpWebRequest)你可以GetResponse()这样调用FtpWebRequest

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

FtpWebResponse对象有这样的字段StatusCode应该给你一个成功的迹象。

完整代码示例:

以下是您的方法重构,上传状态(我实际上没有尝试过您的代码,因为它不会自行编译):

public void upload(string remoteFile, string localFile)
{
    FileStream localFileStream;
    FtpWebResponse ftpResponse;

    try
    {
        ftpRequest = (FtpWebRequest) FtpWebRequest.Create(host + "/" + remoteFile);
        ftpRequest.Credentials = new NetworkCredential(user, pass);
        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = true;
        ftpRequest.KeepAlive = true;
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpStream = ftpRequest.GetRequestStream();
        localFileStream = new FileStream(localFile, FileMode.Create);
        byte[] byteBuffer = new byte[bufferSize];
        int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);

        while (bytesSent != 0)
        {
            ftpStream.Write(byteBuffer, 0, bytesSent);
            bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
        }

        ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        Console.WriteLine("Upload status: {0}, {1}", ftpResponse.StatusCode, ftpResponse.StatusDescription);
    }
    catch (Exception ex)
    {
        // log exception
        Console.WriteLine(ex.ToString());
        // throw;
    }
    finally
    {
        if (localFileStream != null)
        {
            localFileStream.Close();
        }
        if (ftpStream != null)
        {
            ftpStream.Close();
        }   
        if (ftpResponse != null)
        {
            ftpResponse.Close();
        }            
        ftpRequest = null;
    }
} 
于 2013-08-14T07:34:37.473 回答