实习笔记6 WebService Axis2 传递Gzip压缩后的字符串乱码问题
来源:互联网 发布:知乎接口 编辑:程序博客网 时间:2024/06/06 19:30
最近比较倒霉,也碰到了乱码问题,这里还是记录一下,方便以后的学习。话不多说,进入正题。
问题描述:
一台安卓设备通过调用WebService接口,返回得到一串Json字符串,为了让通信的流量减少,这里采用了Java的GZip压缩,结果安卓得到返回的字符串出现乱码。
分析问题产生原因:
1. Tomcat中传递字符都是以ISO-8859-1的编码进行的。
2. 本机中的Eclipse工作空间默认编码是UTF-8。
3. 在压缩的过程中,字符串应该是以UTF-8变成字符数组的,而原来代码中只是按编码的缺省值进行转换。现在代码发布在ApechaAxis2上,是以ISO-8859-1编码,不进行特定转换,原来的UTF-8字符就以ISO-8859-1编码进行表示,所以才出错。
问题解决代码:(注意:解决前与解决后的代码差别只是加了一行有注释的代码)
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;import org.json.JSONObject;/** * Gzip压缩解压编码 * @author lin-xiandong * */public class GZipUtil { public static String compress(String str){String ret = ""; if (null == str || str.length() <= 0) { ret = str; }else{ ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip; try { gzip = new GZIPOutputStream(out); gzip.write(str.getBytes("UTF-8"));//修复乱码所在 gzip.close(); ret = out.toString("ISO-8859-1"); } catch (IOException e) { e.printStackTrace(); } } return ret; }public static String unCompress(String str) throws IOException { if (null == str || str.length() <= 0) { return str; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1")); GZIPInputStream gzip = new GZIPInputStream(in); byte[] buffer = new byte[256]; int n = 0; while ((n = gzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } return out.toString("UTF-8"); }public static String getCompressJsonStr(JSONObject jObj){JSONObject resultJson = new JSONObject();String result = null;try {result = GZipUtil.compress(jObj.toString());resultJson.put("result", result);} catch (Exception e) {e.printStackTrace();}return resultJson.toString();}}
0 0
- 实习笔记6 WebService Axis2 传递Gzip压缩后的字符串乱码问题
- xfire 开发webservice, 传递中文字符串乱码问题
- WebService Axis2:复合类型数据的传递
- 解决JAVA、.NET间WebService传递中文乱码的问题
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- webservice 笔记(小结) axis2 集成spring 传递对象
- webservice 学习笔记,axis2传递简单对象,非stub方式
- webservice 学习笔记,axis2 传递对象数组,非stub方式
- php file_get_contents读取远程文件的乱码问题(gzip压缩引起的)
- java gzip 压缩字符串
- ios gzip 压缩字符串
- axis2:ServiceClient增加GZIP压缩支持
- Axis2 Webservice new RPCServiceClient()错误的问题
- Golang gzip压缩的一个问题
- flume hdfs sink gzip 压缩的问题
- axis2 webservice学习笔记
- 国内的ntp服务器
- 全局hook键盘消息
- 【转】Control.DataBinding数据绑定细解
- Json概述以及python对json的相关操作
- TextView的超链接问题
- 实习笔记6 WebService Axis2 传递Gzip压缩后的字符串乱码问题
- PreparedStatement和Statement
- Android WebView 隐藏网页中内容
- Android资源访问
- Android NDK课程录制完成上线
- helloworld
- JFreeChart的乱码问题
- linux学习之八---Linux进程基础知识
- ORB Feature