spring httpInvoker中前后台byte[]数据传输

目的:在后台使用GZIP的格式压缩内存中的数据,传输到前台在进行解压缩
机制:java的GZIPInputStream
过程:

1:修改接口,返回一个byte数组 
 
  1. /**  
  2.  * 获取压缩功能节点bytes  
  3.  * **时使用  
  4.  *   
  5.  * @return  
  6.  */  
  7. public byte [] loadVOsBytes

2:修改对应方法,将传输的数据进行压缩

    /**
     * 获取压缩功能节点bytes
     * **时使用
     * 
     * @return
     */
    public byte[] loadVOsBytes() {

        // 未压缩功能节点对象数组内容
        FunVO[] vos = loadVOs();
        // 压缩
        byte[] zipBytes = null;
        ByteArrayOutputStream byteOut;
        GZIPOutputStream gzout;
        ObjectOutputStream objectOut;
        try {
            // 建立字节数组输出流
            byteOut = new ByteArrayOutputStream();
            // 建立gzip压缩输出流
            gzout = new GZIPOutputStream(byteOut);
            // 建立对象序列化输出流
            objectOut = new ObjectOutputStream(gzout);
            objectOut.writeObject(vos);
            objectOut.flush();
            // 返回压缩后字节流
            zipBytes = byteOut.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (byteOut != null) {
                byteOut.close();
            }
            if (gzout != null) {
                gzout.close();
            }
            if (objectOut != null) {
                objectOut.close();
            }
        }
        return zipBytes;
    }

3:修改前台对应的获取方法,进行解压还原

 
  1. /**  
  2.  * 获取功能节点(前台)  
  3.  * @return  
  4.  */  
  5. public FunVO[] getVOs() {   
  6.   
  7.     if (vos == null) {   
  8.         byte[] zipBytes = login.loadVOsBytes();   
  9.         ByteArrayInputStream byteIn;   
  10.         GZIPInputStream gzIn;   
  11.         ObjectInputStream objectIn;   
  12.         try {   
  13.             // 建立字节数组输入流  
  14.             byteIn = new ByteArrayInputStream(zipBytes);   
  15.             // 建立gzip解压输入流  
  16.             gzIn = new GZIPInputStream(byteIn);   
  17.             // 建立对象序列化输入流  
  18.             objectIn = new ObjectInputStream(gzIn);   
  19.             // 还原成未压缩的功能节点对象数组  
  20.             vos = (FunVO[]) objectIn.readObject();   
  21.         } catch (ClassNotFoundException e) {   
  22.             e.printStackTrace();   
  23.             throw new RuntimeException(e);   
  24.         } catch (IOException e) {   
  25.             e.printStackTrace();   
  26.             throw new RuntimeException(e);   
  27.         } finally {   
  28.             if (byteIn != null) {   
  29.                 byteIn.close();   
  30.             }   
  31.             if (gzIn != null) {   
  32.                 gzIn.close();   
  33.             }   
  34.             if (objectIn != null) {   
  35.                 objectIn.close();   
  36.             }   
  37.         }   
  38.     }   
  39.     return vos;   
  40. }  
OK,到这里时,传输的数据已经进行压缩了!
扩展:压缩失败时,在catch里面捕获到异常,再传回一个不进行压缩的byte数组,需要在接口中添加一个标志,告诉客户端是否是进行过压缩。
扩展二:对应的压缩与解压方法可以放入工具类。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值