Java 创建具有相对路径的 URL 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4247736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:00:00  来源:igfitidea点击:

creating a URL object with a relative path

javaswingjeditorpane

提问by Anto

I am creating a Swing application with a JEditorPane that should display an HTML file named url1.htmlstored locally in the pagefolder in the root folder of the project.

我正在创建一个带有 JEditorPane 的 Swing 应用程序,该应用程序应该显示一个名为url1.html的 HTML 文件,该文件本地存储在项目根文件夹的页面文件夹中。

I have instantiated the following String object

我已经实例化了以下 String 对象

final String pagePath = "./page/";

and in order to be displayed by the JEditorPane pane I have created the following URL object:

为了在 JEditorPane 窗格中显示,我创建了以下 URL 对象:

URL url1 = new URL("file:///"+pagePath+"url1.html");

However when the setPage method is called with thecreated URL object as a parameter

但是当 setPage 方法以创建的 URL 对象作为参数被调用时

pagePane.setPage(url1);

it throws me a java.io.FileNotFoundException error

它给我一个 java.io.FileNotFoundException 错误

It seems that there is something wrong with the way url1has been constructed. Anyone knows a solution to this problem?

url1 的构造方式似乎有问题。有谁知道这个问题的解决方案?

采纳答案by khachik

The solution is to find an absolute path to url1.htmlmake an object of java.io.Fileon it, and then use toURI().toURL()combination:

解决办法是找到一个绝对路径url1.htmljava.io.File在上面创建一个对象,然后使用toURI().toURL()组合:

URL url1 = (new java.io.File(absolutePathToHTMLFile)).toURI().toURL();

Assuming if the current directory is the root of page, you can pass a relative path to File:

假设当前目录是 的根目录,则page可以将相对路径传递给File

URL url1 = (new java.io.File("page/url1.html")).toURI().toURL();

or

或者

URL url1 = (new java.io.File(new java.io.File("page"), "url1.html")).toURI().toURL();

But this will depend on where you run the application from. I would make it taking the root directory as a command-line argument if it is the only configurable option for the app, or from a configuration file, if it has one.

但这将取决于您从何处运行应用程序。如果根目录是应用程序的唯一可配置选项,或者来自配置文件(如果有),我会将其作为命令行参数。

The another solution is to put the html file as a resource into the jar file of your application.

另一种解决方案是将 html 文件作为资源放入应用程序的 jar 文件中。

回答by Salman Paracha

I would try the following

我会尝试以下

URL url = new URL("file", "", pagePath+"url1.html");

I believe by concatenating the whole string, you are running into problems. Let me know, if that helped

我相信通过连接整个字符串,您会遇到问题。让我知道,如果这有帮助

回答by jcadcell

To load a resource from the classpath (as khachik mentioned) you can do the following:

要从类路径加载资源(如 khachik 所述),您可以执行以下操作:

URL url = getClass().getResource("page/url1.html");

or from a static context:

或从静态上下文:

URL url = Thread.currentThread().getContextClassLoader().getResource("page/url1.html");

So in the case above, using a Maven structure, the HTML page would be at a location such as this:

因此,在上述情况下,使用 Maven 结构,HTML 页面将位于如下位置:

C:/myProject/src/main/resources/page/url1.html