Building an absolute URL from a relative URL in Java

天涯浪子 提交于 2019-11-26 22:24:20

问题


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

Given

http://localhost:8080/myWebApp/someServlet

Inside the method:

   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.

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

http://localhost:8080/someImage.jpg

Help greatly appreciated


回答1:


Using java.net.URL

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



回答2:


How about:

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



回答3:


Looks like you already figured out the hard part, which is what host your are running on. The rest is easy,

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

Should give you what you need.




回答4:


this code work will on linux, it can just combine the path, if you want more, constructor of URI could be helpful.

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

if you path contain something need to escape, use URLEncoder.encode to escape it at first.

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());

example:

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();
        }
    }
}

output

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

baseUrl.getPath() are very important, don't forget it.

a wrong example:

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();
        }
    }
}

output

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

we have lost our /first in baseurl.



来源:https://stackoverflow.com/questions/1389184/building-an-absolute-url-from-a-relative-url-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!