Here you can find the source of encode(String source)
public static String encode(String source)
//package com.java2s; /*// w ww. ja v a 2 s .co m * ==================================================================== * Copyright (c) 2004 TMate Software Ltd. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://tmate.org/svn/license.html. * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * ==================================================================== */ import java.io.IOException; import java.net.URLEncoder; import java.util.StringTokenizer; public class Main { 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) || "/".equals(token) || "(".equals(token) || ")".equals(token)) { dst.append(token); } 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(); } public static final String append(String path, String segment) { path = isEmpty(path) ? "" : removeTrailingSlash(path); segment = isEmpty(segment) ? "" : removeLeadingSlash(segment); if (path.endsWith("/")) { path = path.substring(0, path.length() - "/".length()); } if (segment.startsWith("/")) { segment = segment.substring("/".length()); } return path + '/' + segment; } public static final boolean isEmpty(String path) { return path == null || "".equals(path.trim()) || "/".equals(path.trim()); } public static final String removeTrailingSlash(String path) { if (isEmpty(path)) { return path; } if (path.charAt(path.length() - 1) == '/') { path = path.substring(0, path.length() - 1); } return path; } public static String removeLeadingSlash(String path) { if (path == null || path.length() == 0) { return ""; } if (path.charAt(0) == '/') { path = path.substring(1); } return path; } }