libcurl 上传文件



 


#include "stdafx.h"


 


#include "stdafx.h"   


#include  


#include    


#include  


#include  


#include  


#include


 


using namespace cv;


using namespace std;


 


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


{


    string* str = (string*)stream;


    (*str).append((char*)ptr, size*nmemb);


    return size*nmemb;


}


wstring str_cvt(const string& from_str, int cvt_type) {


    int wstr_len =MultiByteToWideChar(cvt_type, 0, from_str.c_str(), -1, nullptr, 0);


    wchar_t *wstr = new wchar_t[wstr_len + 1];


    memset(wstr,0, (wstr_len + 1) * sizeof(wchar_t));


    MultiByteToWideChar(cvt_type, 0, from_str.c_str(), -1, wstr,wstr_len);


    wstring ret_str(wstr); delete[] wstr;


    return ret_str;


}


string str_cvt(const wstring& from_str, int cvt_type) {


    int str_len =WideCharToMultiByte(cvt_type, 0, from_str.c_str(), -1, nullptr, 0, nullptr, nullptr);


    char *str = new char[str_len + 1];


    memset(str,0, str_len + 1);


    WideCharToMultiByte(cvt_type, 0, from_str.c_str(), -1, str,str_len, nullptr, nullptr);


    string ret_str(str); delete[] str;


    return ret_str;


}


//stringstream serialize(Mat input)


//{


//  //We will need to also serialize the width, height, type and size of the matrix


//  intwidth = input.cols;


//  intheight = input.rows;


//  inttype = input.type();


//  size_tsize = input.total() * input.elemSize();


//


//  //Initialize a stringstream and write the data


//  stringstreamss;


//  ss.write((char*)(&width),sizeof(int));


//  ss.write((char*)(&height),sizeof(int));


//  ss.write((char*)(&type),sizeof(int));


//  ss.write((char*)(&size),sizeof(size_t));


//


//  //Write the whole image data


//  ss.write((char*)input.data,size);


//


//  returnss;


//}


void upload_file() {


    CURL *curl;


    CURLcode res;


    string data; // 返回的内容 


    struct curl_httppost *formpost = NULL;


    struct curl_httppost *lastptr = NULL;


    struct curl_slist *headerlist = NULL;


    static const char buf[] = "Expect:";


 


    Mat image = imread("d:\\11.jpeg", CV_LOAD_IMAGE_COLOR);


 


    IplImage iplimage = image;


 


    vector<uchar> buff;//buffer for coding


 


    vector<int> param = vector<int>(2);


 


    param[0] = CV_IMWRITE_JPEG_QUALITY;


 


    param[1] = 95;//default(95) 0-100


 


 


 


    imencode(".jpg", image, buff,param);


    std::stringstr_encode(buff.begin(), buff.end());


 


 


    //stringstream serializedStream = serialize(image);


    int size =image.total() * image.elemSize();


    byte * bytes = new byte[size];  // you will have todelete[] that later


    std::memcpy(bytes,image.data, size * sizeof(byte));


    curl_global_init(CURL_GLOBAL_ALL);


    /* Fill in the file upload field */


    curl_formadd(&formpost,


         &lastptr,


         CURLFORM_COPYNAME, "message",


         CURLFORM_COPYCONTENTS, "hello",


         CURLFORM_END);


 


    /* Add a buffer to upload */


    curl_formadd(&formpost,&lastptr,


         CURLFORM_COPYNAME, "myfile",


         CURLFORM_BUFFER, "data",


         CURLFORM_BUFFERPTR, str_encode.c_str(),


         CURLFORM_BUFFERLENGTH, str_encode.length(),


         CURLFORM_CONTENTTYPE, "image/jpeg",


         CURLFORM_END);


 


    curl_formadd(&formpost,


         &lastptr,


         CURLFORM_COPYNAME, "myfile",


         CURLFORM_FILE, "11.jpeg",


         //CURLFORM_CONTENTTYPE,"application/vnd.ms-excel",


         CURLFORM_END);


 


    curl= curl_easy_init();


    /* initalize custom header list (stating that Expect:100-continue is not


    wanted*/


    headerlist= curl_slist_append(headerlist, buf);


    if (curl) {


         /* what URL that receives this POST */


         curl_easy_setopt(curl,CURLOPT_URL, "http://127.0.0.1:8000/pic_class/");


         //if ((argc == 2) && (!strcmp(argv[1],"noexpectheader")))


             /* only disable 100-continue header if explicitlyrequested */


             curl_easy_setopt(curl,CURLOPT_HTTPHEADER, headerlist);


         curl_easy_setopt(curl,CURLOPT_HTTPPOST, formpost);


         curl_easy_setopt(curl,CURLOPT_WRITEDATA, &data);


         curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, Reply);


         /* Perform the request, res will get the return code */


         res= curl_easy_perform(curl);


         /* Check for errors */


         if (res != CURLE_OK)


             fprintf(stderr, "curl_easy_perform() failed: %s\n",


                  curl_easy_strerror(res));


 


         /* always cleanup */


         curl_easy_cleanup(curl);


 


         /* then cleanup the formpost chain */


         curl_formfree(formpost);


         /* free slist */


         curl_slist_free_all(headerlist);


    }


}


 


int main()


{


    upload_file();


    string data; // 返回的内容 


    CURL *curl;


    CURLcode res;


 


    curl_global_init(CURL_GLOBAL_ALL);


 


    curl= curl_easy_init();


    if (curl) {


         curl_easy_setopt(curl,CURLOPT_URL, "https://kyfw.12306.cn/otn/login/init");


         curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER, 0L);


         curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST, 0L);


         curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, Reply);


         curl_easy_setopt(curl,CURLOPT_WRITEDATA, &data);


         res= curl_easy_perform(curl);


         if (res != CURLE_OK)


             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));


         curl_easy_cleanup(curl);


    }


    curl_global_cleanup();


    cout<<str_cvt(str_cvt(data, CP_UTF8), CP_ACP) << endl;


    getchar();


    return 0;


}


你可能感兴趣的:(c++)