public static String encode(String source) { StringBuffer sb = new StringBuffer(); encode(source, sb); return sb.toString();
private static void encode(String urlPart, StringBuffer dst) { for(StringTokenizer tokens = new StringTokenizer(urlPart, " /+", true); tokens.hasMoreTokens();) { String token = tokens.nextToken(); if (" ".equals(token)) { dst.append("%20"); } else if ("+".equals(token)) { dst.append("+"); } else if ("/".equals(token)) { dst.append("/"); } else { try { dst.append(URLEncoder.encode(token, "UTF-8")); } catch (IOException e) { dst.append(token); } } }
    public static String encode(String source) {        StringBuffer sb = new StringBuffer();        encode(source, sb);        return sb.toString();    }