如何从 Java 启动带有锚点的文件协议 URL?
在 Java 程序中,我需要在本地 HTML 文件上启动默认浏览器,指向文件内的锚点。 在 Java SE 6 中, java.awt.Desktop.browse 方法将打开文件,但不会遵循锚点,因此类似下面的内容会在顶部打开文件,但不会将浏览器分页到锚点:
Desktop.getDesktop("file:///C:/foo/bar.html#anchor");
Sun 说这里 https://bugs.java.com/bugdatabase/view_bug?bug_id=6477862 文件 URI 协议不支持锚点。
有人有更好的答案吗?
我可以使用 Java SE 6。我可以使用仅 Windows 的解决方案。
From a Java program, I need to launch the default browser on a local HTML file, pointed to an anchor inside the file. In Java SE 6, the java.awt.Desktop.browse method will open the file, but will not honor the anchor, so something like the following opens the file at the top, but does not page the browser to the anchor:
Desktop.getDesktop("file:///C:/foo/bar.html#anchor");
Sun says here https://bugs.java.com/bugdatabase/view_bug?bug_id=6477862 that anchors are not supported in the file URI protocol.
Does anyone have a better answer?
I can use Java SE 6. I would be OK with a Windows only solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我只是用另一种方式解决了这个问题,因为这些示例中的任何引用或空格都对我不起作用。
1 检测文件 URI 是否具有锚点或查询字符串
2 如果有,则创建一个临时文件
File tmpfile = File.createTempFile("apphelp", ".html")
,并元重定向到我想要的实际文件 URI:3 使用新的临时 URI 执行本地 rundll 命令:
我希望这对您有用!
I just solved this another way, because no amount of quoting or spaces in any of these examples worked for me.
1 Detect if the file URI has a anchor or query string
2 If so, create a temp file
File tmpfile = File.createTempFile("apphelp", ".html")
with a meta-redirect to the actual file URI I desire:3 Execute the local rundll command using new temporary URI:
I hope this works for you!
Windows 上的解决方案是:
rundll32 URL.dll, FileProtocolHandler "file:///x:/temp/fragtest.htm#frag"
注意引号!
rundll32 URL.dll、FileProtocolHandler file:///x:/temp/fragtest.htm#frag 确实按预期工作。
Solution on Windows is:
rundll32 URL.dll, FileProtocolHandler "file:///x:/temp/fragtest.htm#frag"
Mind the quotes!!!
rundll32 URL.dll, FileProtocolHandler file:///x:/temp/fragtest.htm#frag does work as expected.
仅适用于 Windows,您可以尝试
For Windows only, you could try
您可以尝试使用 BrowserLauncher2。 它是一个小型且独立的跨平台库,用于打开默认浏览器。 它可以完美地处理锚。
You could try using BrowserLauncher2. It's a small and self-contained cross-platform library to open the default browser. It handles anchors perfectly.
我已经在此处对此项目进行了一些调查 - 请注意,打开
cmd
并输入start file:///c:/temp/test.html#anchor
也不起作用。我认为唯一真正有效的方法是手动调用浏览器(或使用执行此操作的第三方工具)。
在 Windows 上,您始终拥有 Internet Explorer,因此如果您确实不想查找 iexplore.exe,可以调用 Runtime.getRuntime().exec("cmd.exe start iexplore " + myURL)你自己 - 但这并不总是有效。
I've done some investigation on this item here - note that opening
cmd
and typingstart file:///c:/temp/test.html#anchor
also doesn't work.I think the only thing that actually works is to call a browser manually (or use a third-party tool that does this).
On Windows, you always have Internet Explorer, so you could call
Runtime.getRuntime().exec("cmd.exe start iexplore " + myURL)
if you really don't want to find iexplore.exe yourself - but this doesn't always work either.