经过测试,可将9.7M的TXT数据,压缩到 460K左右,配合上Ehcache缓存,接口性能有了很大的提升。
数据压缩采用ZIP方式:
部分代码如下:
-----------------------------------------------------------------------------
public class ZipHelper {
private finalstaticint CacheSize = 1024;
/**
* 压缩Zip
* @param data
* @return
*/
public static byte[] zipByte(byte[] data) {
Deflater compresser = new Deflater();
compresser.reset();
compresser.setInput(data);
compresser.finish();
byte result[] = newbyte[0];
ByteArrayOutputStream o = new ByteArrayOutputStream(1);
try {
byte[] buf = newbyte[CacheSize];
int got = 0;
while (!compresser.finished()) {
got = compresser.deflate(buf);
o.write(buf, 0, got);
}
result = o.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
compresser.end();
}
return result;
}
/***
* 压缩String
* @param data
* @return
*/
public static byte[] zipString(String data) {
byte[] input = newbyte[0];
try {
input = data.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
byte[] result = ZipHelper.zipByte(input);
return result;
}
/**
* 解压Zip
* @param data
* @return
*/
public static byte[] unZipByte(byte[] data) {
Inflater decompresser = new Inflater();
decompresser.setInput(data);
byte result[] = newbyte[0];
ByteArrayOutputStream o = new ByteArrayOutputStream(1);
try {
byte[] buf = newbyte[CacheSize];
int got = 0;
while (!decompresser.finished()) {
got = decompresser.inflate(buf);
o.write(buf, 0, got);
}
result = o.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
o.close();
} catch (IOException e) {
e.printStackTrace();
}
decompresser.end();
}
return result;
}
/**
* 解压Zip数据为String
* @param data
* @return
*/
public static String unZipByteToString(byte[] data) {
byte[] result = unZipByte(data);
String outputString = null;
try {
outputString = new String(result, 0, result.length,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return outputString;
}
@SuppressWarnings("unused")
public static void main(String [] args) throws UnsupportedEncodingException {
String test = "aaaa测试";
byte[] byte1 = ZipHelper.zipString(test);
byte[] byte2 = new String(ZipHelper.zipString(test)).getBytes("ISO-8859-1");
byte[] byte3 = new String(ZipHelper.zipString(test),"ISO-8859-1").getBytes("ISO-8859-1");
// out.println(new String(ZipHelper.zipString(test), "ISO-8859-1"));
String str = new String(byte1, "ISO-8859-1");
// byte[] byteTest = {120, -100, 75, 76, 76, 76, 124, -74, -75, -5, -59, -6, -87, 0, 28, -80, 5, -41};
byte[] byteTest = {120, 63, 75, 76, 76, 76, 124, 63, 63, 63, 63, 63, 0, 28, 63, 5, 63};
String strUnZip = unZipByteToString(byte3);
System.out.println(strUnZip);
// zipHelperTest();
}
}
-------------------------------------------------------------------------------Web端将Json结果组织好之后,使用response的PrintWriter对象,将数据返回给Pad端。
// 输出结果(压缩版)
response.setCharacterEncoding(defaultContentEncoding);
PrintWriter out = response.getWriter();
String strZipped = new String(ZipHelper.zipString(strResult), "ISO-8859-1");
out.print(strZipped);
注意写法“new String(ZipHelper.zipString(strResult), "ISO-8859-1");”,将结果字符串压缩后,返回的是byte [] 类型,需要将其按"ISO-8859-1" 的编码转为字符串,才可以使用out.print方法,将其发送给pad端;
Pad端在接到上面的字符串后,使用相同的编码格式"ISO-8859-1",得到 byte [] 数据,并将其解压即可:
byte[] byte3 =strReturn.getBytes("ISO-8859-1"); (strReturn 为接收到的字符串)
String strUnZip = unZipByteToString(byte3);