话不多说,直接上干货
#ifndef _ELEMENT_FTP_INCLUDE_H_
#define _ELEMENT_FTP_INCLUDE_H_
/*FTP OPERATION CODE*/
typedef enum FTP_STATE
{
FTP_UPLOAD_SUCCESS,
FTP_UPLOAD_FAILED,
FTP_DOWNLOAD_SUCCESS,
FTP_DOWNLOAD_FAILED
}FTP_STATE;
/*FTP OPERATIONS OPTIONS*/
typedef struct FTP_OPT
{
char *url; /*url of ftp*/
char *user_key; /*username:password*/
char *file; /*filepath*/
FTP_OPT() : url(""), user_key(""), file("") { }
}FTP_OPT;
#ifdef __cplusplus
extern "C" {
#endif
/*upload file to ftp server*/
FTP_STATE ftp_upload(const FTP_OPT &ftp_option);
/*download file from ftp server*/
FTP_STATE ftp_download(const FTP_OPT &ftp_option);
#ifdef __cplusplus
}
#endif
#endif
#include "ftp_client.h"
#include <stdio.h>
#include <stdlib.h>
#include "curl\curl.h"
#include "io.h"
/*****************util api******************/
int get_file_size(FILE *file)
{
int size = 0;
fseek(file, 0L, SEEK_END);
size = ftell(file);
fseek(file, 0L, SEEK_SET);
return size;
}
/******************curl api****************/
CURL *curl_init()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if (NULL == curl) {
printf("init curl failed.\n");
return NULL;
}
return curl;
}
void curl_set_upload_opt(CURL *curl, const char *url, const char *user_key, FILE *file)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
curl_easy_setopt(curl, CURLOPT_READDATA, file);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, get_file_size(file));
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
void curl_set_download_opt(CURL *curl, const char *url, const char *user_key, FILE *file)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERPWD, user_key);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
void curl_exit(CURL *curl)
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
CURLcode curl_perform(CURL *curl)
{
CURLcode ret = curl_easy_perform(curl);
if (ret != 0) {
printf("perform curl failed, ret = %d\n", ret);
}
return ret;
}
/****************ftp upload & download api******************/
FTP_STATE ftp_upload(const FTP_OPT &ftp_option)
{
FTP_STATE state = FTP_UPLOAD_FAILED;
CURL *curl = NULL;
//先查询文件是否被占用
/*HANDLE Handle;
Handle = CreateFile(LPCWSTR(ftp_option.file), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == Handle)
{
printf("文件被占用, %s\n", ftp_option.file);
return FTP_UPLOAD_FAILED;
}*/
if (!_access(ftp_option.file, 0))//如果文件存在:
{
try {
FILE *fp = fopen(ftp_option.file, "rb");
if (NULL == fp) {
printf("open file failed, %s\n", ftp_option.file);
return FTP_UPLOAD_FAILED;
}
if (!(curl = curl_init()))
return FTP_UPLOAD_FAILED;
curl_set_upload_opt(curl, ftp_option.url, ftp_option.user_key, fp);
CURLcode code = curl_perform(curl);
printf("upload file(%s) return = %d\n", ftp_option.file, code);
if (CURLE_OK == code)
state = FTP_UPLOAD_SUCCESS;
else
state = FTP_UPLOAD_FAILED;
curl_exit(curl);
fclose(fp);
}
catch (...) {
state = FTP_UPLOAD_FAILED;
curl_exit(curl);
}
}
else
{
return FTP_UPLOAD_FAILED;
}
return state;
}
FTP_STATE ftp_download(const FTP_OPT &ftp_option)
{
FTP_STATE state = FTP_DOWNLOAD_FAILED;
try {
CURL *curl;
FILE *fp = fopen(ftp_option.file, "wb");
if (NULL == fp) {
printf("open file failed, file = %s\n", ftp_option.file);
return FTP_UPLOAD_FAILED;
}
curl = curl_init();
curl_set_download_opt(curl, ftp_option.url, ftp_option.user_key, fp);
CURLcode code = curl_perform(curl);
printf("download file(%s) return = %d\n", ftp_option.file, code);
if (CURLE_OK == code)
state = FTP_DOWNLOAD_SUCCESS;
else
state = FTP_DOWNLOAD_FAILED;
curl_exit(curl);
fclose(fp);
}
catch (...) {
state = FTP_DOWNLOAD_FAILED;
}
return state;
}
#pragma once
#include <string>
struct FILE_FTP_
{
std::string file_path;
std::string ftp_path;
};
class Ftp
{
public:
Ftp();
~Ftp();
public:
int UploadFtp(std::string file_path, std::string ftp_path);
int InitFtp(std::string ftpIp, std::string ftpPort, std::string userName, std::string passWord);
int FtpControl(std::string file_path, std::string ftp_path);
private:
std::string ftp_ip_;
std::string ftp_port_;
std::string user_name_;
std::string pass_word_;
};
#include "Ftp.h"
#include "ftp_client.h"
Ftp::Ftp()
{
ftp_ip_ = "";
ftp_port_ = "";
user_name_ = "";
pass_word_ = "";
}
Ftp::~Ftp()
{
}
int Ftp::FtpControl(std::string file_path, std::string ftp_path)
{
std::string url_path = "ftp://" + ftp_ip_ + ":" + ftp_port_ + "/" + ftp_path;
FTP_OPT OPT;
OPT.url = (char*)(url_path.c_str());
OPT.file = (char*)(file_path.c_str());
std::string key = user_name_ + ":" + pass_word_;
OPT.user_key = (char*)(key.c_str());
return ftp_upload(OPT);
}
int Ftp::InitFtp(std::string ftpIp, std::string ftpPort, std::string userName, std::string passWord)
{
ftp_ip_ = ftpIp;
ftp_port_ = ftpPort;
user_name_ = userName;
pass_word_ = passWord;
return 0;
}
int Ftp::UploadFtp(std::string file_path, std::string ftp_path)
{
return FtpControl(file_path, ftp_path);
}