功能很少,只有下面四个功能:
1、连接。
2、断开。
3、上传文件。
4、续传。
就放两个功能的类,主要是使用了wininet库,简单方便。
.h文件:
#pragma once
#include <afx.h>
#include <WinInet.h>
#include <afxinet.h>
class CFtpManager
{
public:
CFtpManager(void);
~CFtpManager(void);
CFtpManager(const CString& ip, const CString& port);
private:
CString ip_;
CString port_;
CFtpConnection *ftp_connect_;
CInternetSession session;
public:
void setIpPort(const CString& ip, const CString& port);
bool connect2Server();
bool disConnect();
const CString getServerFileInfo();
bool uploadFile(const CString& filepath, const CString& filename);
bool continueUpload(const CString& remove_file, const CString& local_file);
};
.cpp文件:
#include "StdAfx.h"
#include "FtpManager.h"
//#include <afxinet.h>
CFtpManager::CFtpManager(void):ftp_connect_(NULL)
{
}
CFtpManager::~CFtpManager(void)
{
}
CFtpManager::CFtpManager(const CString& ip, const CString& port):
ftp_connect_(NULL)
{
ip_ = ip;
port_ = port;
}
void CFtpManager::setIpPort(const CString& ip, const CString& port){
ip_ = ip; port_ = port;
}
//链接到服务器
bool CFtpManager::connect2Server(){
if(ftp_connect_ == NULL){
try{
ftp_connect_ = session.GetFtpConnection(ip_, _T("test01"), _T("testtest"), _ttoi(port_));
}catch(CInternetException *ex){
AfxMessageBox(_T("connect ftp server fail.."));
session.Close();
if(ftp_connect_ != NULL){
delete ftp_connect_;
ftp_connect_ = NULL;
}
ex->Delete();
return false;
}
}
return true;
}
//断开链接
bool CFtpManager::disConnect(){
if(ftp_connect_ != NULL){
ftp_connect_->Close();
// session.Close();
delete ftp_connect_;
ftp_connect_ = NULL;
}
return true;
}
//简单的获取服务器文件信息
const CString CFtpManager::getServerFileInfo(){
if(ftp_connect_ == NULL)
return _T("");
CFtpFileFind finder(ftp_connect_);
BOOL findit = finder.FindFile(_T("*"));
CString s = _T("");
while(findit){
findit = finder.FindNextFile();
CString t;
t.Format(_T("name:%s path:%s title:%s url:%s\t\n"), finder.GetFileName(), finder.GetFilePath(), finder.GetFileTitle(), finder.GetFileURL());
s += t;
}
return s;
}
//上传文件
bool CFtpManager::uploadFile(const CString& filepath, const CString& filename){
if(ftp_connect_ == NULL) return false;
BOOL b = ftp_connect_->PutFile(filepath, filename);
//if(!b)
DWORD d = GetLastError();
return true;
}
//断点上传文件
bool CFtpManager::continueUpload(const CString& remove_file, const CString& local_file){
if(ftp_connect_ == NULL) return false;
ULONGLONG size = 0;
CFtpFileFind finder(ftp_connect_);
if(finder.FindFile(remove_file)){
finder.FindNextFileW();
size = finder.GetLength();
}
CInternetFile *serverfile;
CFile lfile;
lfile.Open(local_file, 0);
if(size > 0){
CString str;
str.Format(_T("REST %ld\r\n"), size);
ftp_connect_->Command(str);
serverfile = ftp_connect_->OpenFile(remove_file, GENERIC_WRITE);
lfile.Seek(size, 0);
}
const ULONGLONG buflen = 2048;
char buffer[buflen];
while(lfile.Read(buffer, buflen)){
serverfile->Write(buffer, buflen);
}
serverfile->Close();
lfile.Close();
return true;
}
这里转一个用socket写的ftp客户端文章连接(没有运行测过)
http://blog.csdn.net/liuyizhe567/article/details/8250937
socket写的ftp连接地址