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

Kaw*_*ili 5 html java windows-7

我迫切需要您在解决Windows-7问题方面的专业知识.

场景:我有一个基于框架的帮助包,设置用于上下文相关的帮助调用.Java应用程序能够通过将表示所需HTML命名锚点的标记传递给名为pophelp的HTML文件来控制帮助包打开的页面.此文件具有javascripting,它从URL末尾读取传递的标记,并将其映射到帮助包中的相应HTML文件并打开它.

问题:上述方案适用于Windows XP,但不再适用于Windows 7.

来自Java应用程序的调用机制: rundll32 url.dll,FileProtocolHandler文件://filepath/pophelp.html?标记

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

示例代码:

适用于Windows XP的原始呼叫 -

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

结果:?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();
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:?TAG =未传递callsubflow.

任何援助将不胜感激!

Arc*_*ord 4

我真的不明白为什么 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();
}
Run Code Online (Sandbox Code Playgroud)

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

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")));
    }
}
Run Code Online (Sandbox Code Playgroud)