代码之家  ›  专栏  ›  技术社区  ›  Berek Bryan

桌面和URI生成字符工件

  •  0
  • Berek Bryan  · 技术社区  · 7 年前

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

    enter image description here

    这是前端代码。

    <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编码的空格“+”。

    enter image description here

            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();
        }   
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Community Mohan Dere    6 年前

    哈希字符由您正在使用的构造函数追加。 看一看这个 JavaDoc :

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

    您应该使用 URI ;请参阅文档。在我看来,你的第三个论点更像是 query fragment .