欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

服务器端跟客户端交互时的压缩

更新时间: 2014-01-05 02:33:32 责任编辑: Author_N1

 

服务器端和客户端交互时的压缩
服务器端:
private byte[] compress(byte[] data) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 压缩
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data, 0, data.length);
gos.finish();
byte[] output = baos.toByteArray();
baos.flush();
baos.close();
return output;
}
private byte[] compress(byte[] data) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 压缩
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data, 0, data.length);
gos.finish();
byte[] output = baos.toByteArray();
baos.flush();
baos.close();
return output;
}

String json = caseReportService.findAppCaseQueryByPage(contentresult);
//System.out.println("json.length"+json.length());
byte[] output = compress(json.getBytes("UTF-8"));
//System.out.println("output.length"+output.length);
OutputStream out = response().getOutputStream();
response().setCharacterEncoding("UTF-8");
response().setHeader("Content-Encoding", "gzip"); 
response().setContentLength(output.length); 
//System.out.println(new String(output,"UTF-8"));
out.write(output);
out.flush();
out.close();

客户端 :
if (headerField != null && headerField.indexOf("gzip") > -1) {
result = zipToString(in);
} else {
result = inputStreamToString(in);
}
public static String zipToString(InputStream is) throws IOException {
GZIPInputStream gzin = new GZIPInputStream(is);

InputStreamReader isr = new InputStreamReader(gzin, "UTF-8");
java.io.BufferedReader br = new java.io.BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String tempbf;
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
}
isr.close();
gzin.close();
System.out.println("sb to String: zip" + sb.toString());
return sb.toString();
}
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:33:32
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/11370/
本文WWW.DOC100.NET DOC100.NET版权所有。