如何使用Java锚点启动文件协议URL? [英] How to launch a file protocol URL with an anchor from Java?

查看:210
本文介绍了如何使用Java锚点启动文件协议URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java程序中,我需要在本地HTML文件上启动默认浏览器,指向文件中的锚点。在Java SE 6中,java.awt.Desktop.browse方法将打开该文件,但不会遵循锚点,因此类似下面的内容会在顶部打开文件,但不会将浏览器分页到锚点:

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在这里说 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477862 文件URI协议不支持锚点。

Sun says here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477862 that anchors are not supported in the file URI protocol.

有没有人有更好的答案?

Does anyone have a better answer?

我可以使用Java SE 6.我可以使用仅限Windows的解决方案。

I can use Java SE 6. I would be OK with a Windows only solution.

推荐答案

我刚刚用另一种方式解决了这个问题,因为这些例子中没有任何引用或空格对我有效。

I just solved this another way, because no amount of quoting or spaces in any of these examples worked for me.

1检测文件URI是否有锚点或查询字符串

1 Detect if the file URI has a anchor or query string

2如果是,请创建临时文件文件tmpfile = File.createTempFile(apphelp,。html)带有元重定向到我想要的实际文件URI:

2 If so, create a temp file File tmpfile = File.createTempFile("apphelp", ".html") with a meta-redirect to the actual file URI I desire:

<html><head>
<meta http-equiv="refresh" content="0;url=help.html#set_filter" />
</head></html>

3使用新的临时URI执行本地rundll命令:

3 Execute the local rundll command using new temporary URI:

Runtime.getRuntime().exec(
  "rundll32 url.dll,FileProtocolHandler \"" 
  +tmpfile.toURI().toString()+ "\"");

我希望这适合你!

这篇关于如何使用Java锚点启动文件协议URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆