我希望在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) at io.netty.handler.codec.compression.ZlibCodecFactory.newZlibDecoder(ZlibCodecFactory.java:86) at testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:36) at testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:21) at io.netty.channel.ChannelInitializer.channelRegistered(ChannelInitializer.java:70) at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRegistered(DefaultChannelHandlerContext.java:188) at io.netty.channel.DefaultChannelHandlerContext.fireChannelRegistered(DefaultChannelHandlerContext.java:174) at io.netty.channel.DefaultChannelPipeline.fireChannelRegistered(DefaultChannelPipeline.java:730) at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:426) at io.netty.channel.AbstractChannel$AbstractUnsafe.access$100(AbstractChannel.java:367) at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:403) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:353) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:366) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) at 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 java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 15 more
主程序报错如下:
Exception in thread "main" java.nio.channels.ClosedChannelException
当我尝试在字符串编码之前进行压缩/解压缩时,客户端/服务器可以正常工作,但我仍然遇到了同样的错误?请帮帮我好吗?