c 处理基于java服务端byte[]数组接收图片( xsd__base64Binary)

背景:java端基于webservice实现服务端,c作为客户端,通过gsoap传输图片给服务端。

实现步骤:

1、c端通过服务端wsdl文件生成客户端代码,图片接收部分的结构体如下:

struct xsd__base64Binary{
	unsigned char *__ptr;
	int __size;
	char *id;	/* optional element of type xsd:string */
	char *type;	/* optional element of type xsd:string */
	char *options;	/* optional element of type xsd:string */
};

2、读取图片数据,并赋值给次结构体

merchantLogoByte.__ptr = soap_malloc( &add_soap, size);
memcpy(merchantLogoByte.__ptr, data, size); //data 图片数据
merchantLogoByte.__size = size;            //size图片大小

3、代码示例

#include 
#include 
#include 
#include "AppWebServiceSoapBinding.nsmap"
#include "soapH.h"
#include "soapStub.h"



#define BUFSIZE 10240

/****************************************************************
函数说明    :pos签到
入口参数    :mbs数据结构体
返回类型    :交易结果
						0-成功
						非0-失败
初始时间    :2014-9-18 yuweichao
修改记录    :
用途功能    :用于去后台校验版本信息、生成POS机一级菜单。
****************************************************************/
int main ()
{
	struct soap add_soap;
	struct ns3__AddMerchantRequest _request;
	struct ns4__spWSProcessRegistResponse _param_7;
	struct xsd__base64Binary merchantLogoByte;	
	char aRcvmsg_buf[BUFSIZE];
	char aTmp[256] = {0};
	
	memset(&_request, 0x00, sizeof(struct ns3__AddMerchantRequest));
	memset(&_param_7, 0x00, sizeof(struct ns4__validateAdminTelCellNumResponse));
	memset(&merchantLogoByte, 0x00, sizeof(struct xsd__base64Binary));
//	sleep(10);
	printf("交易处理开始 \n");
	soap_init (&add_soap);
	soap_set_mode(&add_soap,SOAP_C_UTFSTRING); /*设置字符集,要不客户端及服务端都会乱码*/
	soap_set_namespaces(&add_soap, namespaces);/*分配命名空间*/
	add_soap.connect_timeout = 60;/*设置连接超时时间 大于0 seconds 小于 0 usec*/
	add_soap.send_timeout = 60;/*设置发送超时时间 大于0 seconds 小于 0 usec*/
	add_soap.accept_timeout = 60;/*设置接收超时时间 大于0 seconds 小于 0 usec*/
	add_soap.recv_timeout = 60;/*设置接收超时时间 大于0 seconds 小于 0 usec*/
	
	_request.adminName = "Yu";
	_request.adminPwd  ="bcb15f821479b4d5772bd0ca866c00ad5f926e3580720659cc80d39c9d09802a";
	_request.adminTelCellNum = "11111111111";
	_request.merchantAddress ="西安";
	_request.merchantClassfiy="2";
	_request.merchantEmail   ="[email protected]";
	_request.merchantName ="610166010002";
	_request.merchantTelephone = "11111111111";
	/*图片数据处理*/
	
FILE *fp = fopen("qr.jpg", "rb");
  
  if (fp == NULL) 
  {
      fprintf(stderr, "cannot open image file\n");    
      exit(1);
  }
      
  fseek(fp, 0, SEEK_END);
  
  if (ferror(fp)) {
      
      fprintf(stderr, "fseek() failed\n");
      int r = fclose(fp);

      if (r == EOF) {
          fprintf(stderr, "cannot close file handler\n");          
      }    
      
      exit(1);
  }  
  
  int flen = ftell(fp);
  
  if (flen == -1) {
      
      perror("error occurred");
      int r = fclose(fp);

      if (r == EOF) {
          fprintf(stderr, "cannot close file handler\n");
      }
      
      exit(1);      
  }
  
  fseek(fp, 0, SEEK_SET);
  
  if (ferror(fp)) {
      
      fprintf(stderr, "fseek() failed\n");
      int r = fclose(fp);

      if (r == EOF) {
          fprintf(stderr, "cannot close file handler\n");
      }    
      
      exit(1);
  }

  char data[flen+1];
	//merchantLogoByte.__ptr = (unsigned char*)soap_malloc( &add_soap, flen);

  int size = fread(data, 1, flen, fp);
  //int size = fread(merchantLogoByte.__ptr, 1, flen, fp);
  if (ferror(fp)) {
      
      fprintf(stderr, "fread() failed\n");
      int r = fclose(fp);

      if (r == EOF) {
          fprintf(stderr, "cannot close file handler\n");
      }
      
      exit(1);      
  }
  
  int r = fclose(fp);

  if (r == EOF) {
      fprintf(stderr, "cannot close file handler\n");
  } 
  printf("%d\n [%#x]\n", size, data);
 	merchantLogoByte.__ptr = soap_malloc( &add_soap, size);
 	memcpy(merchantLogoByte.__ptr, data, size);
 	//merchantLogoByte.__ptr = data;
 	//strcpy(merchantLogoByte.__ptr, data);
  merchantLogoByte.__size = size;
	
	_request.merchantLogoByte = &merchantLogoByte;

  if (soap_call_ns4__spWSProcessRegist(&add_soap, NULL, NULL, &_request, &_param_7) == SOAP_OK)
  	 {
		printf( "%s(%s):%d|Call webservce OK  [%s] [%s]\n",__FILE__,__FUNCTION__,__LINE__, _param_7._spWSProcessRegistReturn->resultCode, _param_7._spWSProcessRegistReturn->resultMsg);
	}  	
	else
	{
		printf("%s(%s):%d|soap err:%d,%s,%s",__FILE__,__FUNCTION__,__LINE__,add_soap.error,*soap_faultcode(&add_soap),*soap_faultstring(&add_soap));	
		return -1;
	}
	
	soap_end(&add_soap);    /*清除运行环境变量*/
	soap_done(&add_soap);   /*卸载运行环境变量*/

	return 0;
}


你可能感兴趣的:(linux,c,c语言,webservice,base64,图片)