Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中附加URL_Java_Url_Append - Fatal编程技术网

如何在java中附加URL

如何在java中附加URL,java,url,append,Java,Url,Append,您知道Java1.5中有任何JDK服务知道如何处理URL附加吗?(注意放置“?”或“&”取决于查询参数是否为第一个参数) 谢谢, Shay公共字符串构建url(字符串url,列表参数){ StringBuilder=新的StringBuilder(url); if(params!=null&¶ms.size()>0){ 生成器。附加(“?”); for(迭代器i=params.Iterator();i.hasNext();){ 字符串s=i.next(); 建造商。附加; if(i.ha

您知道Java1.5中有任何JDK服务知道如何处理URL附加吗?(注意放置“?”或“&”取决于查询参数是否为第一个参数)

谢谢, Shay

公共字符串构建url(字符串url,列表参数){
StringBuilder=新的StringBuilder(url);
if(params!=null&¶ms.size()>0){
生成器。附加(“?”);
for(迭代器i=params.Iterator();i.hasNext();){
字符串s=i.next();
建造商。附加;
if(i.hasNext()){
建筑商。追加(“&”);
}
}
}
返回builder.toString();
}

一个相当简单的例子(非常类似于Noel M的例子,他在我写这篇文章时发布了自己的观点)是:

StringBuilder sb = new StringBuilder(url);
url.indexOf("?") > -1 ? sb.append("&") : sb.append("?");

// loop over your paramaters and append them like in Noel M's example

url = sb.toString();

这个用于构建URL的简单类为我节省了很多时间:

public class LinkBuilder {

    protected String scheme;
    protected String host;
    protected int port;
    protected Map<String, String> args = new HashMap<String, String>();
    protected String path;
    protected String hash;

    public LinkBuilder() {
        this(null, null, 0, new HashMap<String, String>(), null, null);
    }

    protected LinkBuilder(LinkBuilder other) {
        this.scheme = other.scheme;
        this.host = other.host;
        this.port = other.port;
        this.args = new HashMap<String, String>();
        if (other.args != null) {
            this.args.putAll(other.args);
        }
        this.path = other.path;
        this.hash = other.hash;
    }

    protected LinkBuilder(String schema, String host, int port, Map<String, String> args, String path, String hash) {
        this.scheme = schema;
        this.host = host;
        this.port = port;
        this.args = new HashMap<String, String>();
        if (args != null) {
            this.args.putAll(args);
        }
        this.path = path;
        this.hash = hash;
    }

    public LinkBuilder(URI url) {
        String query = url.getRawQuery();
        if (query != null) {
            for (String argLine : query.split("&")) {
                if (argLine.length() > 0) {
                    int i = argLine.indexOf('=');
                    if (i != -1) {
                        args.put(argLine.substring(0, i), argLine.substring(i + 1));
                    }
                    else {
                        args.put(argLine, null);
                    }
                }
            }
        }
        this.scheme = url.getScheme();
        this.host = url.getHost();
        this.port = url.getPort();
        this.path = url.getRawPath();
        this.hash = url.getRawFragment();
    }

    public LinkBuilder url(URI url) {
        return new LinkBuilder(url);
    }

    public LinkBuilder scheme(String schema) {
        return new LinkBuilder(schema, host, port, args, path, hash);
    }

    public LinkBuilder host(String host) {
        if (host.indexOf('/') != -1) {
            throw new IllegalArgumentException("Wrong host name: " + host);
        }
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder port(int port) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder hash(String hash) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder path(String path) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder arg(String name) {
        return arg(name, null);
    }

    public LinkBuilder arg(String name, Object value) {
        Map<String, String> newArgs = new HashMap<String, String>(args);
        newArgs.put(name, value == null ? null : value.toString());
        return new LinkBuilder(scheme, host, port, newArgs, path, hash);
    }

    public String build() {
        StringBuilder buf = new StringBuilder();
        if (scheme != null) {
            buf.append(scheme);
        }
        buf.append("://");
        if (host != null) {
            buf.append(host);
        }
        if (port > 0 && !"https".equals(scheme)) {
            buf.append(':').append(port);
        }
        if (path != null) {
            if (path.charAt(0) != '/') {
                buf.append('/');
            }
            buf.append(path);
        }
        else if (args.size() > 0 || hash != null) {
            buf.append('/');
        }
        if (args.size() > 0) {
            buf.append('?');
            boolean first = true;
            for (Entry<String, String> arg : args.entrySet()) {
                if (!first) {
                    buf.append('&');
                }
                else {
                    first = false;
                }
                buf.append(URLEncoder.encode(arg.getKey(), "UTF-8"));
                if (arg.getValue() != null && arg.getValue().length() > 0) {
                    buf.append("=").append(URLEncoder.encode(arg.getValue(), "UTF-8"));
                }
            }
        }
        if (hash != null) {
            buf.append('#').append(hash);
        }
        return buf.toString();
    }

    public String toString() {
        return build();
    }
}

希望这能有所帮助。

我怀疑您是否能找到这方面的服务。它的实现非常简单,不是JDK的标准部分,但提供了使用ApacheHttpClient构建URL的答案。
public class LinkBuilder {

    protected String scheme;
    protected String host;
    protected int port;
    protected Map<String, String> args = new HashMap<String, String>();
    protected String path;
    protected String hash;

    public LinkBuilder() {
        this(null, null, 0, new HashMap<String, String>(), null, null);
    }

    protected LinkBuilder(LinkBuilder other) {
        this.scheme = other.scheme;
        this.host = other.host;
        this.port = other.port;
        this.args = new HashMap<String, String>();
        if (other.args != null) {
            this.args.putAll(other.args);
        }
        this.path = other.path;
        this.hash = other.hash;
    }

    protected LinkBuilder(String schema, String host, int port, Map<String, String> args, String path, String hash) {
        this.scheme = schema;
        this.host = host;
        this.port = port;
        this.args = new HashMap<String, String>();
        if (args != null) {
            this.args.putAll(args);
        }
        this.path = path;
        this.hash = hash;
    }

    public LinkBuilder(URI url) {
        String query = url.getRawQuery();
        if (query != null) {
            for (String argLine : query.split("&")) {
                if (argLine.length() > 0) {
                    int i = argLine.indexOf('=');
                    if (i != -1) {
                        args.put(argLine.substring(0, i), argLine.substring(i + 1));
                    }
                    else {
                        args.put(argLine, null);
                    }
                }
            }
        }
        this.scheme = url.getScheme();
        this.host = url.getHost();
        this.port = url.getPort();
        this.path = url.getRawPath();
        this.hash = url.getRawFragment();
    }

    public LinkBuilder url(URI url) {
        return new LinkBuilder(url);
    }

    public LinkBuilder scheme(String schema) {
        return new LinkBuilder(schema, host, port, args, path, hash);
    }

    public LinkBuilder host(String host) {
        if (host.indexOf('/') != -1) {
            throw new IllegalArgumentException("Wrong host name: " + host);
        }
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder port(int port) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder hash(String hash) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder path(String path) {
        return new LinkBuilder(scheme, host, port, args, path, hash);
    }

    public LinkBuilder arg(String name) {
        return arg(name, null);
    }

    public LinkBuilder arg(String name, Object value) {
        Map<String, String> newArgs = new HashMap<String, String>(args);
        newArgs.put(name, value == null ? null : value.toString());
        return new LinkBuilder(scheme, host, port, newArgs, path, hash);
    }

    public String build() {
        StringBuilder buf = new StringBuilder();
        if (scheme != null) {
            buf.append(scheme);
        }
        buf.append("://");
        if (host != null) {
            buf.append(host);
        }
        if (port > 0 && !"https".equals(scheme)) {
            buf.append(':').append(port);
        }
        if (path != null) {
            if (path.charAt(0) != '/') {
                buf.append('/');
            }
            buf.append(path);
        }
        else if (args.size() > 0 || hash != null) {
            buf.append('/');
        }
        if (args.size() > 0) {
            buf.append('?');
            boolean first = true;
            for (Entry<String, String> arg : args.entrySet()) {
                if (!first) {
                    buf.append('&');
                }
                else {
                    first = false;
                }
                buf.append(URLEncoder.encode(arg.getKey(), "UTF-8"));
                if (arg.getValue() != null && arg.getValue().length() > 0) {
                    buf.append("=").append(URLEncoder.encode(arg.getValue(), "UTF-8"));
                }
            }
        }
        if (hash != null) {
            buf.append('#').append(hash);
        }
        return buf.toString();
    }

    public String toString() {
        return build();
    }
}
new LinkBuilder()
   .scheme("http")
   .host("stackoverflow.com")
   .path("/questions/3253058/how-to-append-url-in-java/3253350")
   .hash("3253350")
   .build(); // Generates link to this post

new LinkBuilder(new URI("http://www.google.com/search"))
   .arg("q", "Bugs Bunny")
   .arg("ie", "UTF-8")
   .build(); // Results in http://www.google.com/search?q=Bugs+Bunny&ie=UTF-8