public static String urlEncode(String toEncode) {
        String encoded;
        try {
            encoded = URLEncoder.encode(toEncode, ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        // URLEncoder encodes spaces as '+', convert to hex encoding
        encoded = encoded.replaceAll("[+]", "%20");

        // Forwad slashes need not be encoded
        encoded = encoded.replaceAll("%2F", "/");

        return encoded;
    }
public static String urlEncode ( String toEncode ) { String encoded ; try { encoded = URLEncoder . encode ( toEncode , ENCODING ) ; } catch ( UnsupportedEncodingException e ) { throw new RuntimeException ( e ) ; } encoded = encoded . replaceAll ( "[+]" , "%20" ) ; encoded = encoded . replaceAll ( "%2F" , "/" ) ; return encoded ; }
Encodes characters within a string to allow them to be used within a URL.
Note that the entire URL should not be passed to this method as it will
encode characters like ':' and '/'.

@param toEncode String to encode
@return encoded string