如何将文件中的FTP服务器上复制?(How to copy a file on an FTP ser

2019-08-26 22:59发布

你如何在FTP服务器上复制一个文件? 我的目标是复制ftp://www.mysite.com/test.jpgftp://www.mysite.com/testcopy.jpg 。 要重命名文件,我会用:

var request = (FtpWebRequest)WebRequest.Create("ftp://www.mysite.com/test.jpg");
request.Credentials = new NetworkCredential(user, pass);
request.Method = WebRequestMethods.Ftp.Rename;
request.RenameTo = "testrename.jpg"
request.GetResponse().Close();

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

但是,用于复制文件没有方法。 你会怎么做复制一个文件?

Answer 1:

试试这个:

static void Main(string[] args)
{
    CopyFile("countrylist.csv", "MySample.csv", "username", "password#");
}

public static bool CopyFile(string fileName, string FileToCopy, string userName, string password)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.mysite.net/" + fileName);
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        request.Credentials = new NetworkCredential(userName, password);
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        Upload("ftp://ftp.mysite.net/" + FileToCopy, ToByteArray(responseStream), userName, password);
        responseStream.Close();
        return true;
    }
    catch
    {
        return false;
    }
}

public static Byte[] ToByteArray(Stream stream)
{
    MemoryStream ms = new MemoryStream();
    byte[] chunk = new byte[4096];
    int bytesRead;
    while ((bytesRead = stream.Read(chunk, 0, chunk.Length)) > 0)
    {
        ms.Write(chunk, 0, bytesRead);
    }

    return ms.ToArray();
}

public static bool Upload(string FileName, byte[] Image, string FtpUsername, string FtpPassword)
{
    try
    {
        System.Net.FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(FileName);
        clsRequest.Credentials = new System.Net.NetworkCredential(FtpUsername, FtpPassword);
        clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
        System.IO.Stream clsStream = clsRequest.GetRequestStream();
        clsStream.Write(Image, 0, Image.Length);

        clsStream.Close();
        clsStream.Dispose();
        return true;
    }
    catch
    {
        return false;
    }
}

这将文件下载到一个流,然后上传它。



Answer 2:

的FtpWebRequest是一个轻量级的类。 微软认为应该由简单的客户端使用,一旦客户端完成下载和删除文件。



Answer 3:

我想你不能真正做到这一点与FTP。 你可以做的就是下载要复制,然后用新名称上传文件。 例如:

        try
        {
            WebClient request = new WebClient();
            request.Credentials = new NetworkCredential(user, pass);
            byte[] data = request.DownloadData(host);
            MemoryStream file = new MemoryStream(data);
            Upload(data);
        }
        catch (Exception ex)
        {

        }

    ...

    private void Upload(byte[] buffer)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(newname);

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(user, pass);

            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();

            var resp = (FtpWebResponse)request.GetResponse();
        }
        catch (Exception ex)
        {

        }
    }


Answer 4:

在我们的项目中,我们做了成才这样

// pass parameters according to your need, 
// the below code is done in a hard coded manner for clarity
public void Copy()  
{
    // from where you want to copy
    var downloadRequest = (FtpWebRequest)WebRequest.Create("ftp://www.mysite.com/test.jpg");
    downloadRequest.Credentials = new NetworkCredential("userName", "passWord");
    downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;

    var ftpWebResponse = (FtpWebResponse)downloadRequest.GetResponse();
    var downloadResponse = ftpWebResponse.GetResponseStream();

    int buffLength = 2048;
    byte[] byteBuffer = new byte[buffLength];

    // bytes read from download stream. 
    // from documentation: When overridden in a derived class, reads a sequence of bytes from the  
    // current stream and advances the position within the stream by the number of bytes read.
    int bytesRead = downloadResponse.Read(byteBuffer, 0, buffLength);

    // the place where you want the file to go
    var uploadRequest = (FtpWebRequest)WebRequest.Create("ftp://www.mysite.com/testCopy.jpg");
    uploadRequest.Credentials = new NetworkCredential("userName", "passWord");
    uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;

    var uploadStream = uploadRequest.GetRequestStream();

    if (bytesRead > 0)
    {
        while (bytesRead > 0)
        {
            uploadStream.Write(byteBuffer, 0, bytesRead);
            bytesRead = downloadResponse.Read(byteBuffer, 0, buffLength);
        }
    }

    uploadStream.Close();
    uploadStream.Dispose();

    downloadResponse.Close();
    ftpWebResponse.Close();
    ((IDisposable)ftpWebResponse).Dispose();
}


文章来源: How to copy a file on an FTP server?