目的:在后台使用GZIP的格式压缩内存中的数据,传输到前台在进行解压缩
机制:java的GZIPInputStream
过程:
1:修改接口,返回一个byte数组
OK,到这里时,传输的数据已经进行压缩了!
扩展:压缩失败时,在catch里面捕获到异常,再传回一个不进行压缩的byte数组,需要在接口中添加一个标志,告诉客户端是否是进行过压缩。
扩展二:对应的压缩与解压方法可以放入工具类。
机制:java的GZIPInputStream
过程:
1:修改接口,返回一个byte数组
- /**
- * 获取压缩功能节点bytes
- * **时使用
- *
- * @return
- */
- public byte [] loadVOsBytes
2:修改对应方法,将传输的数据进行压缩
- /**
- * 获取功能节点(前台)
- * @return
- */
- public FunVO[] getVOs() {
- if (vos == null) {
- byte[] zipBytes = login.loadVOsBytes();
- ByteArrayInputStream byteIn;
- GZIPInputStream gzIn;
- ObjectInputStream objectIn;
- try {
- // 建立字节数组输入流
- byteIn = new ByteArrayInputStream(zipBytes);
- // 建立gzip解压输入流
- gzIn = new GZIPInputStream(byteIn);
- // 建立对象序列化输入流
- objectIn = new ObjectInputStream(gzIn);
- // 还原成未压缩的功能节点对象数组
- vos = (FunVO[]) objectIn.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- } finally {
- if (byteIn != null) {
- byteIn.close();
- }
- if (gzIn != null) {
- gzIn.close();
- }
- if (objectIn != null) {
- objectIn.close();
- }
- }
- }
- return vos;
- }
扩展:压缩失败时,在catch里面捕获到异常,再传回一个不进行压缩的byte数组,需要在接口中添加一个标志,告诉客户端是否是进行过压缩。
扩展二:对应的压缩与解压方法可以放入工具类。