java实现urlencode

https://www.cnblogs.com/del88/p/6496825.html

******************************************************

    public static String urlEncode(String url) throws UnsupportedEncodingException {
        if(url == null) {
            return null;
        }
        
        final String reserved_char = ";/?:@=&";
        String ret = "";
        for(int i=0; i < url.length(); i++) {
            String cs = String.valueOf( url.charAt(i) );
            if(reserved_char.contains(cs)){
                ret += cs;
            }else{
                ret += URLEncoder.encode(cs, "utf-8");
            }
        }
        return ret.replace("+", "%20");
    }

 

posted @ 2018-10-29 15:50  zhao1949  阅读(7340)  评论(0编辑  收藏  举报