如何在 Windows 7 中将 URL 参数从 Java 传递到本地 HTML 文件?

2023-12-15

我迫切需要您的专业知识来解决 Windows-7 问题。

设想:我有一个基于框架的帮助包,它是为上下文相关的帮助调用而设置的。 Java 应用程序能够通过将表示所需 HTML 的名为“anchor”的标记传递到名为 pophelp 的 HTML 文件来控制帮助包打开的页面。该文件具有 JavaScript 功能,可从 URL 末尾读取传递的标记,并将其映射到帮助包中相应的 HTML 文件并打开它。

Issue:上述场景适用于 Windows XP,但不再适用于 Windows 7。

Java应用程序的调用机制:rundll32 url.dll,FileProtocolHandler file://filepath/pophelp.html?tag

迄今为止的调查结果摘要:url.dll 似乎不再允许在 Windows 7 中通过 URL 传递参数。参数将被删除。我还尝试使用 Java 中的 Desktop.getDesktop().browse() 进行相同类型的调用,但它似乎也删除了 .html 之后的所有参数。

示例代码:

在 Windows XP 中运行的原始调用 --

运行命令:rundll32 url.dll,FileProtocolHandler file://C:\Program Files\App System\App-Editor-8.0.1\help\pophelp.html?TAG=callsubflow

Result:?TAG=callsubflow 未通过。

使用 Desktop.getDesktop().browse() 的新代码 --

public static void main(String[] args) {

    String url = "file:///C:/Program Files/App System/App-Editor-8.0.1/help/pophelp.html?TAG=callsubflow";

    try {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
                  if (desktop.isSupported(Desktop.Action.BROWSE)) {
                      desktop.browse(new URI(url.replace(" ", "%20")));
            }
        }

    } catch (IOException e) {
        System.out.println("Unable to open "+url+": "+e.getMessage());

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

}

Result:?TAG=callsubflow 未通过。

任何援助将不胜感激!


我真的不明白为什么 Windows 会删除本地文件上的参数。正如评论中提到的,这似乎是某种奇怪的安全限制。但我曾经遇到过类似的问题,并且我找到了一个也适合这种情况的解决方法。
只需创建一个本地临时 HTML 文件(不带参数)即可将您重定向到所需的文件(带参数)。
看看这两个方法:

// creates w3c conform redirect HTML page
public String createRedirectPage(String url){
    return  "<!DOCTYPE HTML>" +
            "<meta charset=\"UTF-8\">" +
            "<meta http-equiv=\"refresh\" content=\"1; url=" + url + "\">" +
            "<script>" +
            "window.location.href = \"" + url + "\"" +
            "</script>" +
            "<title>Page Redirection</title>" +
            "<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->" +
            "If you are not redirected automatically, follow the <a href='" + url + "'>link</a>";
}

// creates temporary file with content of redirect HTML page
public URI createRedirectTempFile(String url) {        
    BufferedWriter writer = null;
    File tmpFile = null;
    try {
        // creates temporary file
        tmpFile = File.createTempFile("pophelp", ".html", null);
        // deletes file when the virtual machine terminate
        tmpFile.deleteOnExit(); 
        // writes redirect page content to file 
        writer = new BufferedWriter(new FileWriter(tmpFile));
        writer.write(createRedirectPage(url));
        writer.close();
    }
    catch (IOException e) {
        return null;
    }
    return tmpFile.toURI();
}

现在你可以像这样使用它们:

String url = "file:///C:/Program Files/App System/App-Editor-8.0.1/help/pophelp.html?TAG=callsubflow";

if (Desktop.isDesktopSupported()) {
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        desktop.browse(createRedirectTempFile(url.replace(" ", "%20")));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Windows 7 中将 URL 参数从 Java 传递到本地 HTML 文件? 的相关文章

随机推荐