curl post base64 jpeg图片精简版

/**
 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
 * author: Kaige Li <lkg@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


#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"  
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;
pictureSrc=fopen("/tmp/webproject/pictureSrc.jpg","w+");
if (!pictureSrc) {
return -1;
}
fwrite(buf, 1, buf_len,pictureSrc);
fseek(pictureSrc,0L,SEEK_SET);
HttpPostJpegFile(pictureSrc);
fclose(pictureSrc);
}
int HttpPostJpegFile(FILE *fp)
{
CURL *curl;   
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;
}
fseek(fp,0L,SEEK_SET);
encode(fp, pEncode);
fseek(pEncode,0L,SEEK_SET);
first=ftell(pEncode);
fseek(pEncode,0L,SEEK_END);
flength=ftell(pEncode)-first;
fseek(pEncode,0L,SEEK_SET);
    /*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_global_init(CURL_GLOBAL_ALL);
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,POSTURL);  
 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, flength);  
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 1000L);  
   curl_easy_perform(curl);  
   curl_easy_cleanup(curl);
 curl_formfree(formpost);
 curl_global_cleanup();
 /* free slist */
 curl_slist_free_all (headerlist);  
}
free(didi);
return 0;  
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dingdongkk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值