【C#随手记】FTP上传与下载

完整代码:

using System.IO;
using System.Net;

/// <summary>
//——————————参数示例
//private string FTPIpPath = "ftp://119.45.5.197:21/";//默认端口号21
//private string UserName = "Njiyue";//用户名
//private string Password = "000000";//密码
//private string FTPFolderPath = "DigitalMediaInteractiveSystem/";//注意:IP后接的不是“/www/wwwroot/”根目录,而是FTP目录;
/// </summary>

public static class FTPUploadAndDownload
{
    /// <summary>
    /// FTP上传 注意设置读写FTP权限
    /// </summary>
    /// <param name="LoadFilePath">本地文件的路径</param>
    /// <param name="FTPPath">要上传到的FTP文件夹路径</param>
    public static void _____UpLoadFile(FileInfo fileInfo, string FTPPath, string userName, string password)
    {
        //FileInfo fileInfo = new FileInfo(LoadFilePath);
        FtpWebRequest request = CreateFtpWebRequest(FTPPath + fileInfo.Name, userName, password, WebRequestMethods.Ftp.UploadFile);
        int buffLength = 2048;//一次要读取和写入多少个字节
        byte[] buff = new byte[buffLength];//写入的字节
        int contentLen;
        FileStream fs = fileInfo.OpenRead();//读取文件,将文件转换成字节文件
        Stream strm = request.GetRequestStream();
        contentLen = fs.Read(buff, 0, buffLength);//读取文件2048个字节,偏移为0,并赋值给buff字节组;
        while (contentLen != 0)
        {
            strm.Write(buff, 0, contentLen); //重点方法,将流写入
            contentLen = fs.Read(buff, 0, buffLength);
        }
        fs.Close();
        strm.Close();
    }
    /// <summary>
    /// FTP下载 注意设置FTP读写权限
    /// </summary>
    /// <param name="FTPFilePath">要下载的FTP文件的路径</param>
    /// <param name="LoadPath">本地文件夹的路径</param>
    public static void _____DownloadFile(string FTPFilePath, string LocalPath, string userName, string password)
    {
        FtpWebRequest request = CreateFtpWebRequest(FTPFilePath, userName, password, WebRequestMethods.Ftp.DownloadFile);
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();//获得相应体
        FileStream filestream = File.Create(LocalPath + Path.GetFileName(FTPFilePath));
        Stream responseStream = response.GetResponseStream();
        int buflength = 2048;
        byte[] buffer = new byte[buflength];
        int bytesRead;
        bytesRead = responseStream.Read(buffer, 0, buflength);
        while (bytesRead != 0)
        {
            filestream.Write(buffer, 0, bytesRead);
            bytesRead = responseStream.Read(buffer, 0, buflength);
        }
        responseStream.Close();
        filestream.Close();
    }
    //创建FTP请求
    private static FtpWebRequest CreateFtpWebRequest(string uri, string UserName, string Password, string requestMethod)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
        request.Credentials = new NetworkCredential(UserName, Password);
        request.KeepAlive = true;//请求完成后是否保持活动状态,不断开连接
        request.UseBinary = true;//指示服务器要传输的二进制数据
        request.UsePassive = true;//被动
        request.Method = requestMethod;
        return request;
    }
}

上一篇:【python】-- GIL锁、线程锁(互斥锁)、递归锁(RLock)


下一篇:Python 线程间的同步机制2:Rlock 和 Condition