我想在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());
}
并且在启动连接时客户端中出现以下错误
WARNING: Failed to initialize a channel. Closing: [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) Caused by:
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线程“主”中的异常java.nio.channels.ClosedChannelException
客户端/服务器工作正常,没有压缩内容我尝试将压缩/解压缩放在字符串编码之前,但是出现相同的错误?有什么帮助吗?
解决方法:
您需要在pom.xml中添加以下依赖项:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<version>1.1.2</version>
</dependency>
这是因为netty将所有依赖项声明为可选.