//方法一
String str="http://www.baidu.com/api/?action=s2s_install&a={appsunion}&b=3&c=300294cf2fb33ab47691cde6fed03836";
URL url = new URL(str);
// System.out.println("URL is " + url.toString());
// System.out.println("protocol is "
// + url.getProtocol());
// System.out.println("authority is "
// + url.getAuthority());
// System.out.println("file name is " + url.getFile());
// System.out.println("host is " + url.getHost());
// System.out.println("path is " + url.getPath());
// System.out.println("port is " + url.getPort());
// System.out.println("default port is "
// + url.getDefaultPort());
// System.out.println("query is " + url.getQuery());
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
System.out.println("*********" + uri.toString());
//方法二 使用google guava api 方法
str = UrlEscapers.urlFragmentEscaper().escape(str);
System.out.println("*********" + str);
转载于:https://my.oschina.net/lzm3/blog/525296