java打包的时候把静态资源放进去,[ Java 求助]Netty 传输本 Jar 包内静态资源问题...

本文探讨了如何在Netty程序中通过FileChannel读取打包后的Jar内资源文件,涉及了从类路径资源到RandomAccessFile的转换问题,以及使用InputStream与FileChannel性能的对比。作者分享了绕过障碍的方法和对零拷贝技术的考量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在写一个 Netty 的小工具遇到一个问题。

想读取程序本 Jar 包内的资源文件(在类目录下的 /static/下,打包后在 jar 包里)

在 IDE 里面跑的时候因为是在 /target/下可以用 new File()的形式直接访问,但是打包为 Jar 包后,该文件就不能用 new File(path) 的形式访问了。

但是纠结的是 Netty 提供的文件传输需要一个 FileChannel, 一般都是从 RandomAccessFile 获得的,

这个 RandomAccessFile 只能从 File 获得。

大概如下:

public void handle(ChannelHandlerContext ctx, FullHttpRequest msg, String filePath) throws IOException, URISyntaxException {

filePath = filePath.substring(1);

URL url = this.getClass().getClassLoader().getResource(filePath);

File file = new File(url.getFile().replace("%20"," "));

RandomAccessFile accessFile = new RandomAccessFile(file, "r");

HttpResponse response = new DefaultHttpResponse(msg.protocolVersion(), HttpResponseStatus.OK);

String contentType = parseContentType(filePath);

response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);

long length = file.length();

response.headers().set(HttpHeaderNames.CONTENT_LENGTH, length);

ctx.write(response);

ctx.write(new DefaultFileRegion(accessFile.getChannel(), 0, file.length()));

ChannelFuture channelFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

}

PS : 我知道可以用 getResourceAsStream 拿到 InputStream ,可以转为 channel

ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());

但是这样好像不能拿到 size,也不能利用 FileChannel 的 tranfer 方法进行零拷贝了;

如果直接用 InputStream 拿数据,是阻塞 IO,和 Netty 不搭。

java.lang.NoClassDefFoundError: Could not initialize class io.netty.util.internal.PlatformDependent0 at io.netty.util.internal.PlatformDependent.getSystemClassLoader(PlatformDependent.java:694) at io.netty.channel.nio.NioEventLoop$4.run(NioEventLoop.java:171) at java.base/java.security.AccessController.doPrivileged(AccessController.java:319) at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:167) at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:149) at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:102) at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:64) at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:49) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:70) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:65) at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:56) at org.apache.rocketmq.remoting.netty.NettyRemotingClient.<init>(NettyRemotingClient.java:122) at org.apache.rocketmq.client.impl.MQClientAPIImpl.<init>(MQClientAPIImpl.java:188) at org.apache.rocketmq.client.impl.factory.MQClientInstance.<init>(MQClientInstance.java:133) at org.apache.rocketmq.client.impl.MQClientManager.getOrCreateMQClientInstance(MQClientManager.java:53) at org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl.start(DefaultMQAdminExtImpl.java:120) at org.apache.rocketmq.tools.admin.DefaultMQAdminExt.start(DefaultMQAdminExt.java:145) at org.apache.rocketmq.dashboard.admin.MQAdminFactory.getInstance(MQAdminFactory.java:53) at org.apache.rocketmq.dashboard.admin.MQAdminPooledObjectFactory.makeObject(MQAdminPooledObjectFactory.java:35) at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObject
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值