简体   繁体   English

Concat Java中的两个URL

[英]Concat two url in java

I have a base url string and another string which should be appended to the base url to get the exact url to request. 我有一个基本url字符串和另一个应该附加到基本url的字符串,以获取要请求的确切url。 currently i am using string manipulation to achieve the result. 目前,我正在使用字符串操作来达到目的。 The code i implemented is as follows 我实现的代码如下

private String getUrl(String base, String className){
        try{
            if(!base.endsWith("/")){
                base= base + "/";
            }
            base= base+ base;
            return base;
        }

Is there any inbuilt method to concat the two string directly? 是否有任何内置方法可以直接连接两个字符串?

You can use the java.net.URI class: 您可以使用java.net.URI类:

URI baseURI = new URI(base + "/");
URI fullURI = baseURI.resolve(className);

URL url = fullURI.toURL(); // as URL
String urlString = fullURI.toString(); // as String

Especially if you can pre-create the base URI once and reuse it multiple times! 尤其是如果您可以一次创建基本URI并多次重用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM