gpt4 book ai didi

java - 无法将资源作为流读取

转载 作者:行者123 更新时间:2023-12-01 17:41:31 26 4
gpt4 key购买 nike

我正在尝试从磁盘读取 HTML 文件。但它总是返回 null。我尝试将所有位置都放在目录中,但它仍然返回 null。

class MainVerticle : AbstractVerticle() {

override fun start(startPromise: Promise<Void>) {
vertx
.createHttpServer()
.requestHandler { req ->
req.response()
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!")
}
.listen(8888) { http ->
if (http.succeeded()) {
startPromise.complete()
println("HTTP server started on port 8888")
} else {
startPromise.fail(http.cause());
}
}

var mailTemplateStream = (getStreamFromRes("emailVerification.html") as
InputStream).reader().readText()
println(mailTemplateStream)

}

fun getStreamFromRes(fileName: String): InputStream? {
val stream = Object::class.java.getResourceAsStream("/$fileName")
return stream
}
}

它总是抛出以下异常:-

SEVERE: null cannot be cast to non-null type java.io.InputStream
kotlin.TypeCastException: null cannot be cast to non-null type
java.io.InputStream
at example.starter.MainVerticle.start(MainVerticle.kt:26)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$9(DeploymentManager.java:556)
at
io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:369)
at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:510)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:518)
at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1044)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)

这里通过函数 getStreamFromRes,我试图获取输入流。我已将文件 emailVerification.html 放置在 MainVerticle 类旁边,但它仍然返回 null。有谁知道我做错了什么?

更新:-我已将此项目添加到我的 github 项目中:- https://github.com/captabhi/abhishek09021998-gmail.com.git

最佳答案

您的 emailVerification.html 文件应与已编译的类文件一起复制到构建文件夹中。每个源文件都驻留在资源文件夹中并不意味着它会被类加载器自动拾取。您的构建脚本应将资源复制到构建文件夹中。

关于java - 无法将资源作为流读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com