在jar文件中运行时无法读取文件
问题描述:
我有一个akka http服务。我只是返回一个get请求的api文档。该文档在html文件中。在jar文件中运行时无法读取文件
在IDE中运行时,它一切正常。当我将它打包成jar时,我收到错误'找不到资源'。我不确定它为什么不能读取托管在jar中的html文件,并且在IDE中正常工作。
这里是路线的代码。
private Route topLevelRoute() {
return pathEndOrSingleSlash(() -> getFromResource("asciidoc/html/api.html"));
}
这些文件位于资源路径中。
答
我有现在这个工作。
我正在这样做。
private Route topLevelRoute() {
try {
InputStreamReader inputStreamReader = new InputStreamReader(getClass().getResourceAsStream("/asciidoc/html/api.html"));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//Get the stream input into string builder
reader.lines().forEach(s -> strBuild.append(s));
inputStreamReader.close();
bufferedReader.close();
//pass the string builder as string with contenttype set to html
complete(HttpEntities.create(ContentTypes.TEXT_HTML_UTF8, strBuild.toString()))
} catch (Exception ex) {
//Catch any exception here
}
}
+0
)这对于其他人来说并不是很好的例子,你在阅读文件的时候阻塞了,我建议你找出你的类路径问题,并使用内置的指令,如果上面的解决方案是你要去的请至少确保您在单独的调度程序中运行它。有关更多信息,请参阅http://doc.akka.io/docs/akka/current/scala/dispatchers.html#problem-blocking-on-default-dispatcher。细节。 – johanandren
如何更改为getFromResource(“/ asciidoc/html/api.html”)? – StanislavL
这不起作用。 ( – Newbee