libcurl post图片 base64编码
来源:互联网 发布:数据挖掘技术 客户微盘 编辑:程序博客网 时间:2024/04/30 09:44
#include "httppost.h"#include <curl/curl.h>#include <string.h>#include <sys/time.h>
#define POSTURL "http://121.40.93.5:7300/dbzf/router/rest?method=api/picture/upload" #ifndef MAX_PATH#define MAX_PATH 256#endif
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char * base64_encode( const unsigned char * bindata, char * base64, int binlength ){ int i, j; unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 ) { current = (bindata[i] >> 2) ; current &= (unsigned char)0x3F; base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ; if ( i + 1 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F ); base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ; if ( i + 2 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 ); base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ; base64[j++] = base64char[(int)current]; } base64[j] = '\0'; return base64;}
int base64_decode( const char * base64, unsigned char * bindata ){ int i, j; unsigned char k; unsigned char temp[4]; for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 ) { memset( temp, 0xFF, sizeof(temp) ); for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i] ) temp[0]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+1] ) temp[1]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+2] ) temp[2]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+3] ) temp[3]= k; }
bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) | ((unsigned char)((unsigned char)(temp[1]>>4)&0x03)); if ( base64[i+2] == '=' ) break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) | ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F)); if ( base64[i+3] == '=' ) break;
bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) | ((unsigned char)(temp[3]&0x3F)); } return j;}
void encode(FILE * fp_in, FILE * fp_out){ unsigned char bindata[2050]; char base64[4096]; size_t bytes; while ( !feof( fp_in ) ) { bytes = fread( bindata, 1, 2049, fp_in ); base64_encode( bindata, base64, bytes ); fprintf( fp_out, "%s", base64 ); }}
void decode(FILE * fp_in, FILE * fp_out){ int i; unsigned char bindata[2050]; char base64[4096]; size_t bytes; while ( !feof( fp_in ) ) { for ( i = 0 ; i < 2048 ; i ++ ) { base64[i] = fgetc(fp_in); if ( base64[i] == EOF ) break; else if ( base64[i] == '\n' || base64[i] == '\r' ) i --; } bytes = base64_decode( base64, bindata ); fwrite( bindata, bytes, 1, fp_out ); }}int HttpPostJpegString(char *buf,int buf_len){FILE *pictureSrc;printf("buf=%s buf_len=%d\n",buf,buf_len);pictureSrc=fopen("/tmp/webproject/pictureSrc.jpg","w+");if (!pictureSrc) {return -1;} printf("buf=%s\n",buf);fwrite(buf, 1, buf_len,pictureSrc);fseek(pictureSrc,0L,SEEK_SET);HttpPostJpegFile(pictureSrc);}int HttpPostJpegFile(FILE *fp){CURL *curl; CURLM *multi_handle; int still_running;long int flength,first;struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; struct curl_slist *headerlist=NULL; static const char buf[] = "Expect:"; char *didi;FILE *pEncode;pEncode=fopen("/tmp/webproject/pEncode.jpg","w+");if (!pEncode) {return -1;}encode(fp, pEncode);fclose(fp);printf("httpPostJpeg\n");fseek(pEncode,0L,SEEK_SET);printf("The file pointer is at byte %ld\n", ftell(pEncode));first=ftell(pEncode);printf("ftell success \n");fseek(pEncode,0L,SEEK_END);printf("The file pointer is at byte %ld\n", ftell(pEncode));flength=ftell(pEncode)-first;fseek(pEncode,0L,SEEK_SET);printf("The file flength is at byte %ld\n", flength); /*read the data and display it*/ didi=(char *) malloc(flength);if(didi==NULL)exit(1);fread(didi,1,flength,pEncode);fclose(pEncode);curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "imgBase64", CURLFORM_COPYCONTENTS, didi, CURLFORM_END); curl = curl_easy_init(); multi_handle = curl_multi_init(); /* initalize custom header list (stating that Expect: 100-continue is not wanted */ headerlist = curl_slist_append(headerlist, buf); if(curl && multi_handle) { /* what URL that receives this POST */ curl_easy_setopt(curl, CURLOPT_URL,POSTURL); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_multi_add_handle(multi_handle, curl); curl_multi_perform(multi_handle, &still_running); do { struct timeval timeout; int rc; /* select() return code */fd_set fdread;fd_set fdwrite; fd_set fdexcep; int maxfd = -1; long curl_timeo = -1; FD_ZERO(&fdread); FD_ZERO(&fdwrite);FD_ZERO(&fdexcep);/* set a suitable timeout to play around with */ timeout.tv_sec = 1; timeout.tv_usec = 0; curl_multi_timeout(multi_handle, &curl_timeo);if(curl_timeo >= 0) { timeout.tv_sec = curl_timeo / 1000; if(timeout.tv_sec > 1) timeout.tv_sec = 1; else timeout.tv_usec = (curl_timeo % 1000) * 1000; } /* get file descriptors from the transfers */ curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the function calls.On success, the value of maxfd is guaranteed to be greater or equal than -1. We call select(maxfd + 1, ...), specially in case of (maxfd == -1), we call select(0, ...), which is basically equal to sleep. */ rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) { case -1: /* select error */ break; case 0: default: /* timeout or readable/writable sockets */ printf("perform!\n"); curl_multi_perform(multi_handle, &still_running); printf("running: %d!\n", still_running); break; } } while(still_running); curl_multi_cleanup(multi_handle); /* always cleanup */ curl_easy_cleanup(curl); /* then cleanup the formpost chain */ curl_formfree(formpost); /* free slist */ curl_slist_free_all (headerlist); }free(didi);return 0; }
阅读全文
0 0
- libcurl post图片 base64编码
- java post图片,图片用base64编码
- iOS post请求图片Base64编码上传
- 图片转成NSDATA然后BASE64编码POST到服务器
- libcurl抓取base64编码的页面内容
- base64编码图片
- Base64编码图片转化
- Base64 图片编码
- 图片转成base64编码
- base64 编码图片
- 图片转成base64编码
- base64图片编码
- .net 图片Base64编码
- Base64编码转图片
- 图片转Base64编码
- 图片base64编码
- base64编码替换图片
- 图片Base64编码
- eclipse打不开遇到 Failed to load the JNL shared library...问题
- NOI_OPENJUDGE_2.4_8463:Stupid cat & Doge题解
- spring配置多个redis
- WebRTC带宽估计
- linux/unix命令详解之crontab
- libcurl post图片 base64编码
- java面试总结
- Leetcode Algorithm 416. Partition Equal Subset Sum
- Redis之——Spring基于注解整合Redis
- 【新收录】CSDN日报 —— Kotlin 专场
- 二、build.gradle的一些详解
- linux/unix命令详解之find
- 信息检索-搜索引擎的搭建(Lucene)
- linux socket阻塞recv