使用java 发布webservice,使用C++接收避免编码不兼容问题
2016-12-08 20:08
471 查看
由于java编码格式的与C++的编码格式不同,不清楚怎么将java使用iso-8859-1等格式进行编码的字符串在C++上进行解码,而且由于java中char是两个字节,C++是一个字节也使得程序在解码乱码时候有些不对。
但验证了一种方式可以成功避免这个编码问题;
核心思想为:
将一个字节本身的二进制进行转换,以两位十六进制数进行保存,这样发送十六进制数的String,我们用vc就可以正确接收存储了;
例如:
缺陷:
由于一个字节是用两个字节字符串表示,所以文件直接大了一倍;
现在当文件大小大于8M会出现异常;java调用时也会报错;不能得到该json对象。
在Java端发送,写入字符串代码:
在VC接收端接收,写入文件代码:
但验证了一种方式可以成功避免这个编码问题;
核心思想为:
将一个字节本身的二进制进行转换,以两位十六进制数进行保存,这样发送十六进制数的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); } }
相关文章推荐
- Linux使用Java将C++程序发布为webservice(wsdl)
- 一项浩大的JAVA编码工程(与unix兼容乱码问题)
- Eclipse Java EE IDE 中使用Tomcat 5X / 6X 的一些问题(配置,发布相关)
- 针对WebService使用Service类获取Port类的一个参数问题(java.net.MalformedURLException)
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- java使用gbk编码读取utf-8的js文件吃掉回车问题
- 使用cxf发布webservice(JavaApplication)
- 使用CXF(JaxWsServerFactoryBean、ServerFactoryBean)编码的方式发布WebService服务
- 使用spring.net发布webservice时函数重名的问题的解决办法
- JAVA ,SSH中文及其乱码问题的解决 6大配置点 使用UTF-8编码
- 通过base64编码解决java的webservice调用中文参数乱码问题
- c++避免使用hash_map有性能 问题
- 关于C++调用jar包,在创建虚拟机时使用JNI_CreateJavaVM调用失败返回-1的问题
- 基于JAVA中使用Axis发布/调用Webservice的方法详解
- 使用Axis2将Spring的装配JavaBean发布成WebService(转)
- C#客户端与Java程序使用socket连接并通信的兼容问题
- Java JNI 调用C++ API及中文编码问题
- .NET动态调用WebService并生成程序集 兼容集成java发布的webserive
- WebService调用C++dll在XP下IIS5.1发布问题
- 使用javac编译java文件和使用javah生成C/C++头文件 遇到找不到类的问题