[英]Encoding url in java avoiding special characters
我需要傳遞編碼后的網址,但也需要避免使用特殊字符。 那么我該如何編碼呢? 關於stackoverflow的答案都沒有對我有用。 可以幫忙嗎? 我想在java public String tweet_quote_func(Map attrs,FilterParam param)中做到這一點{
String url = param.getShorturi();
String text = attrs.get("display");
if (url != null && text != null) {
try {
text = StringEscapeUtils.unescapeHtml(text); // for double quotes
String encodedurl = "https://twitter.com/intent/tweet?url="+URLEncoder.encode(url, "UTF-8");
encodedurl = encodedurl + "&text=" + StringUtil.escapeUrl(text);
return "<span class=\"tweet_quote\"> <a href=" + encodedurl+ ">" + text.trim() + "<span></span></a></span>";
} catch (UnsupportedEncodingException e) {
System.err.println(e);
return null;
}
} else
return null;
}
您應該使用URLEncoder的所有URL查詢參數名稱和它們的值。 不管是不是StringUtils.escapeURL()。
我不知道您對“特殊字符“。”,“-”,“ *”和“ _””有何不同,但已定義URLEncoder來執行正確的操作。
對於URL路徑本身,您應該使用新的URI(null,path,null).toASCIIString()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.