现在的位置: 首页 > 综合 > 正文

使用ftp上传文件和下载文件—C#

2012年12月27日 ⁄ 综合 ⁄ 共 3738字 ⁄ 字号 评论关闭

 // ftp 文件上传,fileName:要上传的文件,fileNewname:要在服务器上保存的名称
    private void Upload(string fileName, string fileNewname)
    {
        string strFileName = "";

        // 获取文件的相关信息
        FileInfo fileInf = new FileInfo(fileName);
        if (fileNewname == "")
        {
            strFileName = fileInf.Name;
        }
        else
        {
            strFileName = fileNewname;
        }

        // 根据uri创建FtpWebRequest对象
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://127.0.0.1/" + strFileName));

        // ftp用户名和密码
        reqFTP.Credentials = new NetworkCredential("yans1", "123456");

        // 销毁到服务器的连接
        reqFTP.KeepAlive = true;
        // 获取或设置要发送到 FTP 服务器的命令。
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        // 传输文件的数据格式Binary
        reqFTP.UseBinary = true;
        // 文件是多大
        reqFTP.ContentLength = fileInf.Length;

        // 缓冲大小设置为2kb
        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;

        try
        {
            Stream strm = reqFTP.GetRequestStream();

            // 把文件读入到流中
            FileStream fs = fileInf.OpenRead();
            // 用于存储要由当前请求发送到服务器的数据。
            // 把文件流分装成小的字节数组,防止占用太多的服务器内存
            contentLen = fs.Read(buff, 0, buffLength);
            // 循环把文件流写入待发给ftp服务器的请求流中
            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            fs.Close();
            strm.Close();
        }
        catch (Exception e)
        {
            Response.Write(e.Message.ToString());
        }
    }

    // ftp 文件下载,filePath:保存在本地的路径,fileName:要下载的文件名称
    private void Download(string filePath, string fileName)
    {
        // 声明请求
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://127.0.0.1/"  + fileName));

        // 销毁到服务器的连接
        reqFTP.KeepAlive = true;
        // 设置要发送的命令形式
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        // 设置要2进制传递文件
        reqFTP.UseBinary = true;
        // 设置登陆的用户名和密码
        reqFTP.Credentials = new NetworkCredential("yans1",  "123456");
        //try
        //{
            // 获取ftp给的响应
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();

            // 建立本地保存文件流
            FileStream outputStream = new FileStream(filePath + "/" + fileName, FileMode.Create);

            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];

            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

            outputStream.Close();
            ftpStream.Close();
            response.Close();
        //}
        //catch (Exception e)
        //{
        //    Response.Write(e.Message.ToString());
        //}

    }

    // 获取ftp的目录下所有的文件列表
    private string[] GetFileList()
    {
        StringBuilder result = new StringBuilder();

        // 创建请求
        FtpWebRequest reqFtp;
        reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://127.0.0.1"+"/"));

        // 设置请求的属性
        reqFtp.KeepAlive = true;
        reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
        reqFtp.UseBinary = true;

        // 设置用户和密码
        reqFtp.Credentials = new NetworkCredential("yans1", "123456");
        FtpWebResponse resFTP = (FtpWebResponse)reqFtp.GetResponse();

        // 获取响应流
        StreamReader read = new StreamReader(resFTP.GetResponseStream());

        string line = read.ReadLine();
        while (line != null && line != "")
        {   // 判断是文件件,还是文件
            result.Append(line);
            result.Append((char)1);
            line = read.ReadLine();
        }
        // 去掉最后一个 字符
        result.Remove(result.ToString().LastIndexOf((char)1), 1);
        read.Close();
        resFTP.Close();
        return result.ToString().Split((char)1);
    }

抱歉!评论已关闭.