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;
    }



  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC可以通过CFtpConnection类来实现FTP上传、下载、删除文件夹的操作。具体步骤如下: 1. 连接FTP服务器,使用CFtpConnection类的Connect函数连接FTP服务器。 2. 登录FTP服务器,使用CFtpConnection类的Login函数登录FTP服务器。 3. 切换工作目录,使用CFtpConnection类的SetCurrentDirectory函数切换到指定的工作目录。 4. 上传文件,使用CFtpConnection类的PutFile函数上传指定的文件。 5. 下载文件,使用CFtpConnection类的GetFile函数下载指定的文件。 6. 删除文件,使用CFtpConnection类的Remove函数删除指定的文件。 7. 删除文件夹,使用CFtpConnection类的RemoveDirectory函数删除指定的文件夹。 以下是一个简单的例子: ``` CFtpConnection ftp; CString serverName = _T("ftp.server_name.com"); CString userName = _T("user_name"); CString password = _T("password"); if (ftp.Connect(serverName, INTERNET_DEFAULT_FTP_PORT, userName, password)) { // 登录FTP服务器 ftp.Login(); // 切换工作目录 ftp.SetCurrentDirectory(_T("/folder_name")); // 上传文件 ftp.PutFile(_T("local_file_path"), _T("remote_file_name")); // 下载文件 ftp.GetFile(_T("remote_file_name"), _T("local_file_path")); // 删除文件 ftp.Remove(_T("remote_file_name")); // 删除文件夹 ftp.RemoveDirectory(_T("folder_name")); // 断开连接 ftp.Close(); } ``` 需要注意的是,CFtpConnection类中的函数都是同步的,即会阻塞当前线程直到操作完成。如果想要异步执行操作,可以使用CFtpFileFind类和CFtpTransaction类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值