Ftp上传类


/***************************************************
  FTP upload class
  version 1.0.8, Aug 10th, 2016

  Copyright (C) 2015-2016 Acheld CHEN

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Acheld CHEN       
  http://blog.csdn.net/acheld
  
 ****************************************************/
 
 //Ftplib.h
 
 #pragma once

#ifndef CURL_STATICLIB
#define CURL_STATICLIB
#endif


#include <stdlib.h>
#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>


enum FTPCODE{
	FTP_OK = 1,
	FTP_FAIL,
	FTP_LOCALFILE_NOTACCESS,
	FTP_LOGIN_DENIED,
	FTP_COULDNT_CONNECT,
};

class CFtpLib
{
public:
	CFtpLib(void);
	~CFtpLib(void);
protected:
	int upload(CURL *curlhandle, const char * remotepath, const char* usrpasswd,const char * localpath,long timeout=200, long tries=3);

	int uploadFile(CURL *curlhandle, const char * remotepath, const char* usrpasswd,FILE * file,long timeout=200, long tries=3);

	CURLcode FtpUpload(const char * remotepath, const char* usrpasswd,FILE * file);
public:
	static CFtpLib* GetFtp(){
		static CFtpLib ftp;
		return &ftp;
	}
	int FtpUploadmain(const char * remotepath, const char* user,const char* password,const char * localpath);
};

extern CFtpLib* theFtp;


///

//Ftplib.cpp

#include "StdAfx.h"
#include "FtpLib.h"

CFtpLib::CFtpLib(void)
{
}

CFtpLib::~CFtpLib(void)
{
}



/* parse headers for Content-Length */

size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {

	int r;

	long len = 0;

	/* _snscanf() is Win32 specific */

	//r = _snscanf(ptr, size * nmemb, "Content-Length: %ld/n", &len);

	r = sscanf((const char*)ptr, "Content-Length: %ld/n", &len);

	if (r) /* Microsoft: we don't read the specs */

	*((long *) stream) = len;

	return size * nmemb;

}

/* discard downloaded data */

size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream) {

    return size * nmemb;

}

//write data to upload

size_t writefunc(void *ptr, size_t size, size_t nmemb, void *stream)

{
	return fwrite(ptr, size, nmemb, (FILE*)stream);
}

/* read data to upload */

size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)

{

	FILE *f = (FILE*)stream;

	size_t n;

	if (ferror(f))

	return CURL_READFUNC_ABORT;

	n = fread(ptr, size, nmemb, f) * size;

	return n;

}
int CFtpLib::upload( CURL *curlhandle, const char * remotepath, const char* usrpasswd,const char * localpath,long timeout, long tries )
{

	FILE *f;

	long uploaded_len = 0;

	CURLcode r = CURLE_GOT_NOTHING;

	int c;

	f = fopen(localpath, "rb");

	if (f == NULL) {

		perror(NULL);

		return 0;

	}

	curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);

	curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
	/*sprintf(usrpasswd, "%s:%s", user, pass); */ 
	curl_easy_setopt(curlhandle, CURLOPT_USERPWD, usrpasswd);  

	if (timeout)

	curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);

	curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);

	curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);

	curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);

	curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);

	curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);

	curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */

	curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);

	curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);

	for (c = 0; (r != CURLE_OK) && (c < tries); c++) {

		/* are we resuming? */

		if (c) { /* yes */

		/* determine the length of the file already written */

		/*

		* With NOBODY and NOHEADER, libcurl will issue a SIZE

		* command, but the only way to retrieve the result is

		* to parse the returned Content-Length header. Thus,

		* getcontentlengthfunc(). We need discardfunc() above

		* because HEADER will dump the headers to stdout

		* without it.

		*/

			curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);

			curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);

			r = curl_easy_perform(curlhandle);

			if (r != CURLE_OK)

			continue;

			curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);

			curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);

			fseek(f, uploaded_len, SEEK_SET);

			curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);

		}

		else { /* no */

		curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);

		}

		r = curl_easy_perform(curlhandle);

		}

	fclose(f);

	if (r == CURLE_OK){
		return 1;
	}

	else {
		// fprintf(stderr, "%s/n", curl_easy_strerror(r));
		char errorMsg[1023];
		sprintf(errorMsg, "%s/n", curl_easy_strerror(r));
		//CURLE_LOGIN_DENIED 67,Login denied/n
		//CURLE_COULDNT_CONNECT 7,Couldn't connect to server/n
		return 0;
	  }

}

CURLcode CFtpLib::FtpUpload(const char * remotepath, const char* usrpasswd,FILE * file)
{
	CURL* curlhandle;  
	CURLcode resCode;

	if((resCode = curl_global_init(CURL_GLOBAL_ALL)) != 0)  
	{  
		return resCode;  
	}  
	if((curlhandle = curl_easy_init()) == NULL)  
	{  
		curl_global_cleanup();  
		return CURLE_GOT_NOTHING;  
	}  
	 long timeout = 10;
	 long tries = 2;
	// upload(curlhandle, remotepath, usrpasswd,localpath,timeout, tries);

	 int nret = uploadFile(curlhandle, remotepath, usrpasswd,file,timeout, tries);
	 curl_easy_cleanup(curlhandle);
	 curl_global_cleanup();
	 resCode = (CURLcode)nret;
	 return (resCode);

}

int CFtpLib::uploadFile( CURL *curlhandle, const char * remotepath, const char* usrpasswd,FILE * file,long timeout/*=200*/, long tries/*=3*/ )
{
	FILE *f = file;

	long uploaded_len = 0;

	CURLcode r = CURLE_GOT_NOTHING;

	int c;

	curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
	curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
	curl_easy_setopt(curlhandle, CURLOPT_USERPWD, usrpasswd);  

	if (timeout){
		curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
	}

		curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);

		curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);

		curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);

		curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);

		curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);

		curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */

		curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);

		curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);

		for (c = 0; (r != CURLE_OK) && (c < tries); c++) {

			/* are we resuming? */

			if (c) { /* yes */

			/* determine the length of the file already written */

			/*

			* With NOBODY and NOHEADER, libcurl will issue a SIZE

			* command, but the only way to retrieve the result is

			* to parse the returned Content-Length header. Thus,

			* getcontentlengthfunc(). We need discardfunc() above

			* because HEADER will dump the headers to stdout

			* without it.

			*/

				curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);

				curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);

				r = curl_easy_perform(curlhandle);

				if (r != CURLE_OK)

				continue;

				curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);

				curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);

				fseek(f, uploaded_len, SEEK_SET);

				curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);

			}

			else { /* no */

				curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);

			}

			r = curl_easy_perform(curlhandle);

		} // for

	fclose(f);

	return r;
}


int CFtpLib::FtpUploadmain(const char * remotepath, const char* user,const char* password,const char * localpath)
{
	//const char * remotepath = "ftp://200.200.200.201/Fu9CS5.rar";
	//const char * localpath =  "D:\\log\\Abe_P5.rar";

	char usrpasswd[256];
	sprintf(usrpasswd, "%s:%s", user, password);
	FILE *file = fopen(localpath, "rb");
	if (file == NULL) {
		perror(NULL);
		return FTP_LOCALFILE_NOTACCESS;
	}

	CURLcode resCode = FtpUpload(remotepath, usrpasswd,file);

	if (resCode == CURLE_OK){
		return FTP_OK;
	}
	else {
		char errorMsg[1023];
		sprintf(errorMsg, "%s/n", curl_easy_strerror(resCode));
		//CURLE_LOGIN_DENIED 67,Login denied/n
		//CURLE_COULDNT_CONNECT 7,Couldn't connect to server/n
		if (resCode == CURLE_LOGIN_DENIED){
			return FTP_LOGIN_DENIED;
		}
		else if (resCode == CURLE_COULDNT_CONNECT){
			return FTP_COULDNT_CONNECT;
		}
		return FTP_FAIL;
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值