【问题标题】:Building an absolute URL from a relative URL in Java在 Java 中从相对 URL 构建绝对 URL
【发布时间】:2009-09-07 12:30:21
【问题描述】:

我在不使用 Stringhackery 的情况下从相对 URL 构建绝对 URL 时遇到问题...

给定

http://localhost:8080/myWebApp/someServlet

方法内部:

   public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

什么是最“正确”的构建方式:

http://localhost:8080/myWebApp/someImage.jpg

(注意,必须是绝对的,不是相对的)

目前,我正在通过构建字符串来做到这一点,但必须有更好的方法。

我查看了新 URI / URL 的各种组合,最终得到了

http://localhost:8080/someImage.jpg

帮助不胜感激

【问题讨论】:

  • 如果你不知道它是如何工作的,你怎么知道它是“非常简单的 101 东西”?
  • 这更像是对我自己能力的诽谤,而不是其他任何事情。但是,我已经编辑删除了评论,以防它被认为是冒犯的——这不是本意。)

标签: java url relative-url


【解决方案1】:

使用 java.net.URL

 URL baseUrl = new URL("http://www.google.com/someFolder/");
 URL url = new URL(baseUrl, "../test.html");

【讨论】:

  • 这样做了,最终得到相同的 URL http://localhost:8080/someImage.jpg 而不是 http://localhost:8080/myWebApp/someImage.jpg
  • 它也适用于:new URL(baseUrl, "/otherFolder/test.html");,这将转化为:http://www.google.com/otherFolder/test.html
  • 为了使new URL(baseUrl, path) 能够正确解析,baseUrl 必须/ 结尾并且path 必须 是相对的(不是以/ 开头)。那时......你可以只附加字符串,它会同样有效,除非你还需要../ 处理。在那种一个的情况下,这种技术是有意义的。
【解决方案2】:

怎么样:

String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/someImage.jpg";

【讨论】:

  • 如果我想在没有可用请求的上下文中构建绝对 URL,例如在调度程序线程中生成包含绝对 URL 链接的电子邮件,该怎么办
【解决方案3】:

看起来您已经弄清楚了困难的部分,那就是您正在运行的主机。剩下的很简单,

String url = host + request.getContextPath() + "/someImage.jpg";

应该给你你需要的。

【讨论】:

    【解决方案4】:

    此代码适用于linux,它可以只组合路径,如果您想要更多,URI 的构造函数可能会有所帮助。

    URL baseUrl = new URL("http://example.com/first");
    URL targetUrl = new URL(baseUrl, Paths.get(baseUrl.getPath(), "second", "/third", "//fourth//", "fifth").toString());
    

    如果路径中包含需要转义的内容,请先使用URLEncoder.encode 进行转义。

    URL baseUrl = new URL("http://example.com/first");
    URL targetUrl = new URL(baseUrl, Paths.get(baseUrl.getPath(), URLEncoder.encode(relativePath, StandardCharsets.UTF_8), URLEncoder.encode(filename, StandardCharsets.UTF_8)).toString());
    

    示例:

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class Main {
        public static void main(String[] args) {
            try {
                URL baseUrl = new URL("http://example.com/first");
                Path relativePath = Paths.get(baseUrl.getPath(), "second", "/third", "//fourth//", "fifth");
                URL targetUrl = new URL(baseUrl, relativePath.toString());
                System.out.println(targetUrl.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
    
    

    输出

    http://example.com/first/second/third/fourth/fifth
    

    baseUrl.getPath()很重要,别忘了。

    一个错误的例子:

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class Main {
        public static void main(String[] args) {
            try {
                URL baseUrl = new URL("http://example.com/first");
                Path relativePath = Paths.get("second", "/third", "//fourth//", "fifth");
                URL targetUrl = new URL(baseUrl, relativePath.toString());
                System.out.println(targetUrl.toString());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
    
    

    输出

    http://example.com/second/third/fourth/fifth
    

    我们在 baseurl 中丢失了 /first

    【讨论】:

    • Paths.get() 是一个文件系统 API。我刚刚检查过,它使用文件系统本机分隔符,在 Windows 上是“\”。
    猜你喜欢
    • 2018-08-14
    • 2016-01-05
    • 1970-01-01
    • 2012-03-31
    • 2011-09-09
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多