从 Java 中的相对 URL 构建绝对 URL [英] Building an absolute URL from a relative URL in Java

查看:23
本文介绍了从 Java 中的相对 URL 构建绝对 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从相对 URL 构建绝对 URL 时遇到了麻烦,而无需求助于 Stringhackery...

I'm having trouble building an absolute URL from a relative URL without resorting to String hackery...

给定

http://localhost:8080/myWebApp/someServlet

方法内部:

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

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

What's the most "correct" way of building :

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

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

(Note, must be absolute, not relative)

目前,我是通过构建字符串来实现的,但一定有更好的方法.

Currently, I'm doing it through building the string, but there MUST be a better way.

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

I've looked at various combinations of new URI / URL, and I end up with

http://localhost:8080/someImage.jpg

非常感谢帮助

推荐答案

Using java.net.URL

Using java.net.URL

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

这篇关于从 Java 中的相对 URL 构建绝对 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆