c++连接FTP服务器(上传、下载)


void  OnConnect()
{
    //新建对话
    m_pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
    try 
    { 
        //新建连接对象
        CString m_strServer="服务器地址";
        CString m_strUserName="登陆名";
        CString m_strPassword="登陆密码";
        m_pFtpConnection=m_pInetSession->GetFtpConnection(m_strServer,m_strUserName,m_strPassword); 
    } 
    catch(CInternetException *pEx) 
    {
        //获取错误
        TCHAR szError[1024];
        if(pEx->GetErrorMessage(szError,1024))
            AfxMessageBox(szError);
        else  
            AfxMessageBox("There was an exception");
        pEx->Delete();
        m_pFtpConnection=NULL;

        return;
    }
    m_pRemoteFinder = new CFtpFileFind(m_pFtpConnection);

}
//下载单个文件
CString  DownFile(CString strRemoteFile,CString strLocalFile)
{
    if(m_pFtpConnection==NULL)
    {
        AfxMessageBox("连接服务器失败!");
        return "";
    }
    else
    {
    
        m_pFtpConnection->GetFile(strRemoteFile,strLocalFile, FALSE, FILE_ATTRIBUTE_NORMAL, INTERNET_FLAG_RELOAD, 1)       
    }

    return strLocalFile; 

}
//上传单个文件
void CRemoteDriveDlg::UpFile(CString fromPath,CString toPath)
{
    if(m_pFtpConnection==NULL)
    {
        AfxMessageBox("连接服务器失败!");        
    }
    else 
    {
        m_pFtpConnection->PutFile(fromPath,toPath);
        //在数据库添加记录
    }
}

 

  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值