如何在 Java 中使用#anchor 启动 URL? [英] How to launch a URL with #anchor in Java?

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

问题描述

我正在使用以下 Java 代码启动 URL:

Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + myURL)

这里的 myURL 是file:///C:/Desktop/test.html#bottom"

当我像这样传递 URL 时,它只会执行file:///C:/Desktop/test.html"

不包括#bottom.

如何在 Java 中正确启动此 URL?

解决方案

另一种方法(来自 ) 但据我所知,这种行为是在 IE7 发布时引入的.我的猜测是,这修复了 Windows 中的一些错误或安全漏洞,但代价是完全无法做到这一点.

通过更多研究,Windows 中的 file: URI 似乎具有 非常 肮脏 过去

奇怪的是,一个 Microsoft2010 年的文章file://host/path/file.txt?Query#fragment 显示为 .Net 中的有效 URI...

我认为在有人找到一个神奇的注册表设置之前,您可能不走运 - 或者您可以手动调用浏览器(例如 Runtime.getRuntime().exec(chromepath + "chrome.exe file:///c:/temp/test.html#jump")Runtime.getRuntime().exec(iepath + "iexplore.exe file:///c:/temp/test.html#跳转"))

或者,为了好玩:Runtime.getRuntime().exec("cmd.exe start iexplore " + myURL)

I am launching a URL using the following code in Java:

Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + myURL)

Here myURL is "file:///C:/Desktop/test.html#bottom"

When I am passing the URL like this, it's only executing "file:///C:/Desktop/test.html"

It's not including #bottom.

How do I correctly launch this URL in Java?

解决方案

Another way to do it (from How to launch a file protocol URL with an anchor from Java? ):

Runtime.getRuntime().exec("cmd.exe start file:///C:/foo/bar.html#anchor")


Update:

Yet another link with this issue (using url.dll) shows the author reverting to trying an external program, in this case Quero: Runtime.getRuntime().exec("cmd.exe qlaunch.exe open=\"file:///c:/temp/test.html?p=1\"");

From what I can tell, this is a bug in Windows and/or Internet Explorer in particular. Firefox has other issues with the file:// protocol that you have to manually disable.


Update 2:

This is definitely either expected behavior or a bug in Windows itself. Using Desktop.browse (which sends the URI clean to ShellExecute) or cmd.exe start or Runtime.getRuntime().exec("rundll32 url.dll " + uri) or even just opening a command prompt and typing the file:// URI itself just ends up doing this (which I found via Process Monitor):

Order of operations shown:

  1. (1st box) Thread Create = when I hit enter in the cmd window above
  2. Look for the file by traversing the directory tree (e.g. go into c:, go into temp)
  3. (2nd box) When that file is found, STOP looking (which means Windows parsed off the # and ? characters)
  4. (3rd box) DONE looking (CloseFile on parent folder, followed by no more disk I/O)
  5. (4th box) Now that the file is found, look to see what .htm files are opened with (e.g. your browser)
  6. Oddly enough, later on it finds Chrome (default browser) but runs IE (?!) and then runs Chrome - this might be a big part of the problem.

Note that calling start iexplore file:///c:/temp/test.html#mark does work because it's just passing the parameter without trying to do anything special.

Note that steps 2 and 3 violate Microsoft's own advice from 2006 (see here) but from what I've seen this behavior was introduced at the time that IE7 was released. My guess is that this fixed some bug or security flaw in Windows at the cost of being able to do this at all.

With some more research, it appears that file: URIs in Windows have had a very sordid past

Oddly enough, a Microsoft article from 2010 shows file://host/path/file.txt?Query#fragment as a valid URI in .Net...

I think you might be out of luck until someone finds a magical Registry setting - or you can manually call a browser (e.g. Runtime.getRuntime().exec(chromepath + "chrome.exe file:///c:/temp/test.html#jump") or Runtime.getRuntime().exec(iepath + "iexplore.exe file:///c:/temp/test.html#jump"))

Or, for fun: Runtime.getRuntime().exec("cmd.exe start iexplore " + myURL)

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

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