上传:
size_t DownloadFtp::read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
curl_off_t nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, (FILE*)(stream));
nread = (curl_off_t)retcode;
// fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
// " bytes from file\n", nread);
return retcode;
}
bool DownloadFtp::UpLoad(FILETYPE filetype,const QString& filepath,const QString& filename)
{
FILE* pSendFile = ::fopen(filepath.toStdString().c_str(), "rb");
if(NULL == pSendFile)
{
//fprintf(stderr, "Open file failed at %s:%d\n", __FILE__, __LINE__);
LOGGER_ERROR(m_loggerPtr, "Open file "+filepath+" failed ");
return -1;
}
::fseek(pSendFile, 0L, SEEK_END);
size_t iFileSize = ::ftell(pSendFile);
::fseek(pSendFile, 0L, SEEK_SET);
QString fullpath ="";
m_pCurlhandle = curl_easy_init();
if (NULL == m_pCurlhandle)
{
LOGGER_ERROR(m_loggerPtr, "Curl handle could not create!");
return false;
}
curl_easy_setopt(m_pCurlhandle, CURLOPT_USERPWD, m_userpwd.c_str());
curl_easy_setopt(m_pCurlhandle, CURLOPT_URL, fullpath.toStdString().c_str());
curl_easy_setopt(m_pCurlhandle, CURLOPT_READDATA, pSendFile);
curl_easy_setopt(m_pCurlhandle, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(m_pCurlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
curl_easy_setopt(m_pCurlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(m_pCurlhandle, CURLOPT_INFILESIZE, iFileSize);
//qDebug()<<fullpath;
CURLcode r1es = curl_easy_perform(m_pCurlhandle);
if (CURLE_OK!= r1es)
{
LOGGER_ERROR(m_loggerPtr, curl_easy_strerror(r1es));
return false;
}
fclose(pSendFile);
return true;
}
下载:
size_t AchieveModel::my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename.c_str(), "wb");
if(!out->stream)
return -1; /* failure, can't open file to write */
}
out->fileSize += size * nmemb;
return fwrite(buffer, size, nmemb, out->stream);
}
size_t AchieveModel::write_response(void