gpt4 book ai didi

java - 使用 jEditorPane 打开本地网页

转载 作者:行者123 更新时间:2023-12-01 10:06:44 24 4
gpt4 key购买 nike

先生们,

我正在使用 Netbeans 用 Ja​​va 开发 GUI。

出于原型(prototype)设计的目的,我需要在 jEditorPane 中打开一个 HTML 文档(位于我的硬盘上)。我目前拥有的代码是这样的:

HtmlPane.setPage(new URL("C:\\Users\\blah\\Desktop\\example1\\example1\\PracticeHTML.html"));

这会引发 java.net.MalformedURLException

当我尝试时:

HtmlPane.setPage(new URL("https://www.example.com"));

example.com 页面将按预期在我的 HtmlPane 中打开。

显然,任何以 C:\\Users 开头的内容。 。 . 不是一个真正的 URL,因为计算机永远不需要访问万维网才能访问 html 文档,但我不明白为什么这会引发异常。

谁能告诉我哪里出错了?

提前致谢。

最佳答案

问题在于文件路径本身并不是 URL。要将本地文件指定为 url,您需要执行以下操作:

file:///[path]

完整的语法是

file://[host]/[path]

...但如果缺少的话,host 被假定为 localhost

关于java - 使用 jEditorPane 打开本地网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36392958/

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