我在不求助于 String 黑客的情况下从相对 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
非常感谢帮助
最佳答案
使用 java.net.URL
URL baseUrl = new URL("http://www.google.com/someFolder/");
URL url = new URL(baseUrl, "../test.html");
关于java - 在 Java 中从相对 URL 构建绝对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1389184/