您的位置:首页 > 编程语言 > C语言/C++

使用java 发布webservice,使用C++接收避免编码不兼容问题

2016-12-08 20:08 471 查看
由于java编码格式的与C++的编码格式不同,不清楚怎么将java使用iso-8859-1等格式进行编码的字符串在C++上进行解码,而且由于java中char是两个字节,C++是一个字节也使得程序在解码乱码时候有些不对。

但验证了一种方式可以成功避免这个编码问题;

核心思想为:

将一个字节本身的二进制进行转换,以两位十六进制数进行保存,这样发送十六进制数的String,我们用vc就可以正确接收存储了;

例如:



缺陷:

由于一个字节是用两个字节字符串表示,所以文件直接大了一倍;

现在当文件大小大于8M会出现异常;java调用时也会报错;不能得到该json对象。

Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: java.lang.reflect.InvocationTargetException


在Java端发送,写入字符串代码:

StringBuffer outPutString=new StringBuffer();
StringBuffer tempCh=new StringBuffer(2);
int len = 0;
Date data=new Date();
long time1 = data.getTime();

byte[] bys = new byte[1000];

while ((len = fis.read(bys)) != -1) {
for (int i = 0; i < len; i++) {

String str = Integer.toHexString(bys[i]).toString();
if (str.length() == 1) {
tempCh.replace(0, 1, "0");
tempCh.replace(1, 2, str);
} else {
tempCh = tempCh.replace(0, 2, str);
}
if (bys[i] >= 0) {
outPutString.append(tempCh.toString());
//System.out.print(tempCh);System.out.print(" ");
tempCh.delete(0, 2);
} else {
tempCh.delete(0, 6);
//System.out.print(tempCh);System.out.print(" ");
outPutString.append(tempCh.toString());
tempCh.delete(0, 2);
}
}
}
jsonTem.accumulate("buffer", outPutString.toString());


在VC接收端接收,写入文件代码:

void writeFile(const  char * fileName,const char * buffer,int64_t filelength){
ofstream outfile;
setlocale(LC_ALL,"Chinese-simplified");//设置中文环境
string fileNameString=fileName;
string allFileNameString="G:\\jiang_gra\\Test\\CollectionFile\\";
allFileNameString+=fileNameString;
const char * allFileName= allFileNameString.data();

int exist=0;
exist=access(allFileName, 0);//如果文件存在,返回,不存在,返回-1
if (exist==-1)//如果没有被创建
{
outfile.open(allFileName,std::ios::binary | std::ios::app);
unsigned int temp;
unsigned int temp1,temp2;
char num;
//cout<<buffer<<endl;
int filelength1=strlen(buffer);
for (int i=0;i<filelength1;i=i+2)
{
if (buffer[i]>='0'&& buffer[i]<='9')
{
temp1=buffer[i]-'0';
}else if (buffer[i]>='a'&&buffer[i]<='f')
{
temp1=buffer[i]-'a'+10;
}
if (buffer[i+1]>='0'&&buffer[i+1]<='9')
{
temp2=buffer[i+1]-'0';
}else if (buffer[i+1]>='a'&&buffer[i+1]<='f')
{
temp2=buffer[i+1]-'a'+10;
}

//temp1=buffer[i];
//temp2=buffer[i+1];
//temp=buffer[i]+buffer[i+1];
temp=(temp1*16+temp2);
num=(char)(temp);
outfile.write(&num,1);
}
outfile.close();

}else {
//已经存在文件

while (remove( allFileName)!=0)
{
remove( allFileName);
}
writeFile(fileName, buffer, filelength);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web service vc java c语言