MFC 实现FTP服务器上传下载

事先搭建FTP服务器,上传下载端口

新建MFC基于对话框程序,添加上传下载按钮。

上传按钮

CInternetSession * pInternetSession = NULL;
    CFtpConnection* pFtpConnection = NULL;
    //建立连接
    pInternetSession = new CInternetSession(AfxGetAppName());
    //服务器的ip地址
    CString strADddress = "192.168.119.1";
    //用户名与密码
    CString strUserName = "cgs";
    CString strPwd = "a12345";
    //服务器的目录
    CString strDir = "\\"; //若要设置为服务器的根目录,则使用"\\"就可  这个目录是你ftp里面的文件夹目录
    //创建了一个CFtpConnection对象,之后就可以通过这个对象进行上传文件,下载文件了
    pFtpConnection = pInternetSession->GetFtpConnection(strADddress, strUserName, strPwd);
    //设置服务器的目录
    bool bRetVal = pFtpConnection->SetCurrentDirectory(strDir);
    if(bRetVal == false)
    {
        AfxMessageBox("目录设置失败");
        return ;
    }
    else
    {
        //把本地文件上传到服务器上
        CString strLocalFile = "D:\\ftp.txt";
        CString strRemoteFile = "abc.txt";
        pFtpConnection->PutFile(strLocalFile, strRemoteFile);
    }
    //释放资源
    if(NULL != pFtpConnection)
    {
        pFtpConnection->Close();
        delete pFtpConnection;
        pFtpConnection = NULL;
    }
    if(NULL != pInternetSession)
    {
        delete pInternetSession;
        pInternetSession = NULL;
    }
下载按钮

CInternetSession* pInternetSession = NULL;
    CFtpConnection* pFtpConnection = NULL;
    //建立连接
    pInternetSession = new CInternetSession(AfxGetAppName());
    //服务器的ip地址
     //若要设置为服务器的根目录,则使用"\\"就可以了
    //创建了一个CFtpConnection对象,之后就可以通过这个对象进行上传文件,下载文件了
    pFtpConnection = pInternetSession->GetFtpConnection("192.168.119.1", "cgs", "a12345");
    //设置服务器的目录
    bool bRetVal = pFtpConnection->SetCurrentDirectory("\\");
    if(bRetVal == false)
    {
        AfxMessageBox("目录设置失败");
        return ;
    }
    else
    {
        pFtpConnection->GetFile("abc.txt", "D:\\a.txt");
    }
    //释放源
    if(NULL != pFtpConnection)
    {
        pFtpConnection->Close();
        delete pFtpConnection;
        pFtpConnection = NULL;
    }
    if(NULL != pInternetSession)
    {    
        delete pInternetSession;
        pInternetSession = NULL;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值