从Java中的相对URL构建绝对URL [英] Building an absolute URL from a relative URL in Java
本文介绍了从Java中的相对URL构建绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法从相对网址构建绝对网址而无法使用字符串hackery ...
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
帮助非常感谢
推荐答案
使用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屋!
查看全文