2
A
回答
1
一個相當簡單的例子(非常類似於諾埃爾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();
1
public String buildUrl(String url, List<String> params) {
StringBuilder builder = new StringBuilder(url);
if(params != null && params.size() > 0) {
builder.append("?");
for(Iterator<String> i = params.iterator(); i.hasNext();) {
String s = i.next();
builder.append(s);
if(i.hasNext()) {
builder.append("&");
}
}
}
return builder.toString();
}
3
這個簡單的類建築物的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("https://stackoverflow.com/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
希望這會有所幫助。
相關問題
- 1. 如何在java中追加文件
- 2. 如何在Java中追加到DataOutputStream?
- 3. 如何在java中追加xml文件
- 4. Firefox在window.open中追加URL
- 5. 如何追加href url中的列表?
- 6. 在java中追加文件
- 7. 如何通過dynatree中的ajax調用在URL中追加錨?
- 8. 如何將/(正斜槓)追加到URL?
- 9. Javascript:如何將變量追加到url?
- 10. 新的URL正在追加localhost url
- 11. 追加id到url
- 12. URL追加問題
- 13. index.php追加到url
- 14. 在PHP中追加Var到URL
- 15. 在JSP中追加兩次的URL
- 16. index.cfm在MURA中追加SES URL
- 17. 在圖像源中追加URL
- 18. Node.js:如何將文件追加到url url(URL)
- 19. 如何在jquery中追加變量到url?
- 20. 如何在AngularJS中追加查詢字符串到url?
- 21. Java - 追加excel
- 22. 如何加快在Python中追加?
- 23. 如何在Java中的雙向鏈表中追加節點?
- 24. 在java中的ArrayList中追加對象
- 25. 在java中追加到xml文件中
- 26. 追加一些在Java
- 27. 如何追加www開始的url,如果它不在那裏?
- 28. 如何使用 - >在Laravel中追加v。protected $追加
- 29. 如何追加
- 30. 將參數追加到Spring引導Java中的現有url?
我懷疑你會找到一個服務。這個實現非常簡單。 – 2010-07-15 06:50:55
不是JDK的標準部分,但[此前一個問題](http://stackoverflow.com/questions/883136/is-there-a-good-url-builder-for-java)提供了有關使用Apache HTTPClient用於構建URL。 – mikej 2010-07-15 07:01:40