桌面和 URI 生成字符神器

Desktop and URI Producing Character Artifact

我有一个 Primefaces Commandlink,它在我的 mailto 地址末尾给了我一个工件。我不确定“#”是从哪里来的。

这是前端代码。

<p:commandLink value="Mail Video Link" action="#{requestBean.requestUtility.informationRequestLink()}" />     

这是后端操作代码。

    public void informationRequestLink() {
    String subject = "Video Link";
    String cc = "friend2@domain.com,friend3@domain.com";
    String requestLink = "https://www.youtube.com/watch?v=SjeS6gtPq8E";
    String body
            = "Here is the link.\n"
            + requestLink + "\n\n"
            + "Watch at your leisure.";

    try {
        Desktop desktop = Desktop.getDesktop();

        String mailURIString = String.format("?subject=%s&cc=%s&body=%s",
            subject, cc, body);
        URI mailURI = new URI("mailto", "user@domain.com", mailURIString);

        desktop.mail(mailURI);
    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }   
}

[编辑]

我可以去掉“#”,但我得到了 UTF-8 编码的空格“+”。

        String subject = "Video Link";
    String cc = "friend2@domain.com,friend3@domain.com";
    String requestLink = "https://www.youtube.com/watch?v=SjeS6gtPq8E";
    String body
            = "Here is the link.\n"
            + requestLink + "\n\n"
            + "Watch at your leisure.";

    try {
        Desktop desktop = Desktop.getDesktop();

        String mailURIString = String.format("mailto:%s?subject=%s&cc=%s&body=%s",
                "friend1@domain.com", subject.replaceAll(" ", "%20"), cc, URLEncoder.encode(body, "UTF-8"));
        URI mailURI = URI.create(mailURIString);

        desktop.mail(mailURI);
    } catch (Exception e) {
        e.printStackTrace();
    }   

散列字符由您正在使用的构造函数附加。 看看 JavaDoc:

public URI(String scheme, String ssp, String fragment) throws URISyntaxException

[...]

Finally, if a fragment is given then a hash character ('#') is appended to the string, followed by the fragment. Any character that is not a legal URI character is quoted.

您应该使用 URI 的适当构造函数;请参阅文档。对我来说,你的第三个论点似乎更像是 query 而不是 fragment.