c#实现ftp上传和下载

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        string FTPAddress = "ftp://***.***.***.**:21"; //ftp服务器地址
        string FTPUsername = "your username";   //用户名
        string FTPPwd = "your password";        //密码


        //上传
        private void UpFile()
        {
            string LocalPath = "F:\\ftp\\text.txt"; //待上传文件
            FileInfo f = new FileInfo(LocalPath);
            string FileName = f.Name;
            //Path = Path.Replace("\\", "/");
            string ftpRemotePath = "/home/test/";
            string FTPPath = FTPAddress + ftpRemotePath + FileName; //上传到ftp路径,如ftp://***.***.***.**:21/home/test/test.txt
            //实现文件传输协议 (FTP) 客户端
            FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
            reqFtp.UseBinary = true;
            reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd); //设置通信凭据
            reqFtp.KeepAlive = false; //请求完成后关闭ftp连接
            reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
            reqFtp.ContentLength = f.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            //读本地文件数据并上传
            FileStream fs = f.OpenRead();
            try
            {
                Stream strm = reqFtp.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Console.Write("上传失败");
            }
        }

        //下载
        private void DownFile()
        {
            string FtpFilePath = "/home/test.txt";   //远程路径
            string LocalPath = "F:\\ftp\\test.txt"; //下载到的本地路径
            if (File.Exists(LocalPath))
            {
                File.Delete(LocalPath);
            }
            string FTPPath = FTPAddress + FtpFilePath;
            //建立ftp连接
            FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPPath));
            reqFtp.UseBinary = true;
            reqFtp.Credentials = new NetworkCredential(FTPUsername, FTPPwd);
            FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.ContentLength;
            int buffersize = 2048;
            int readCount;
            byte[] buffer = new byte[buffersize];
            readCount = ftpStream.Read(buffer, 0, buffersize);
            //创建并写入文件
            FileStream OutputStream = new FileStream(LocalPath, FileMode.Create);
            while(readCount > 0)
            {
                OutputStream.Write(buffer,0,buffersize);
                readCount = ftpStream.Read(buffer,0,buffersize);
            }
            ftpStream.Close();
            OutputStream.Close();
            response.Close();
            if(File.Exists(LocalPath))
                Console.Write("下载完毕");
        }

        static void Main(string[] args)
        {
            //非静态方法必须先实例化对象,然后再使用
            ConsoleApplication2.Program p = new ConsoleApplication2.Program(); 
            p.UpFile();
            p.DownFile();
        }

        
    }
}

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值