Netty(4.0.4)版本压缩/解压缩字符串消息错误

时间:2013-08-02 19:19:01

标签: java gzip netty

我想在Netty客户端/服务器上应用压缩/解压缩我在客户端和服务器上使用以下代码来管道:

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
    8192, Delimiters.lineDelimiter()));

pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

// and then business logic.
pipeline.addLast("handler", new NettyClientHandler());        
}

和服务器:

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
    8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
//GlibDecoder
//pipeline.addLast("decoder", new ZlibDecoder());
//pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new NettyServerHandler());        
}

我在客户端启动连接时出现以下错误

  
    

警告:无法初始化频道。结束:[id:0x3553bb5c] java.lang.NoClassDefFoundError:com / jcraft / jzlib / Inflater at     io.netty.handler.codec.compression.JZlibDecoder。(JZlibDecoder.java:28)       在     io.netty.handler.codec.compression.ZlibCodecFactory.newZlibDecoder(ZlibCodecFactory.java:86)       在     testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:36)       在     testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:21)       在     io.netty.channel.ChannelInitializer.channelRegistered(ChannelInitializer.java:70)       在     io.netty.channel.DefaultChannelHandlerContext.invokeChannelRegistered(DefaultChannelHandlerContext.java:188)       在     io.netty.channel.DefaultChannelHandlerContext.fireChannelRegistered(DefaultChannelHandlerContext.java:174)       在     io.netty.channel.DefaultChannelPipeline.fireChannelRegistered(DefaultChannelPipeline.java:730)       在     io.netty.channel.AbstractChannel $ AbstractUnsafe.register0(AbstractChannel.java:426)       在     io.netty.channel.AbstractChannel $ AbstractUnsafe.access 100 $(AbstractChannel.java:367)       在     io.netty.channel.AbstractChannel $ AbstractUnsafe $ 1.run(AbstractChannel.java:403)       在     io.netty.util.concurrent.SingleThreadEventExecutor.runAllTask​​s(SingleThreadEventExecutor.java:353)       在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:366)at     io.netty.util.concurrent.SingleThreadEventExecutor $ 2.run(SingleThreadEventExecutor.java:101)       在java.lang.Thread.run(Thread.java:722)引起:     java.lang.ClassNotFoundException:com.jcraft.jzlib.Inflater at     java.net.URLClassLoader $ 1.run(URLClassLoader.java:366)at     java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)at     java.security.AccessController.doPrivileged(Native Method)at     java.net.URLClassLoader.findClass(URLClassLoader.java:354)at     java.lang.ClassLoader.loadClass(ClassLoader.java:423)at     sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)at at     java.lang.ClassLoader.loadClass(ClassLoader.java:356)... 15更多

  

线程“main”中的异常java.nio.channels.ClosedChannelException

客户端/服务器工作正常没有压缩的东西我尝试在字符串编码之前进行压缩/解压缩但是我得到了同样的错误?有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

您需要在pom.xml中添加以下依赖项:

  <dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jzlib</artifactId>
      <version>1.1.2</version>
  </dependency>

这是因为netty将所有依赖项声明为可选。

答案 1 :(得分:0)

在多次试验后感谢您的评论,我找到了正确的问题解决方案: - 在netbeans中,我将jzlib-1.1.2.jar添加到我的项目中。 - pipline的正确顺序如下:

pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
        8192, Delimiters.lineDelimiter()));


pipeline.addLast("decoder", new MyStringDecoder());
pipeline.addLast("encoder", new MyStringEncoder()); 
客户端和服务器

中的