code: public static String encodeURL(String url, String enc) {
  		try {
  
  			if (enc == null || enc.length() == 0) {
  				enc = "UTF-8";
  			}
  
  			return URLEncoder.encode(url, enc);
  
  		} catch (Exception e) {
  			System.err.print(e);
  		}
  		return null;
  	}
docs: Use the new URLEncoder.encode() method from Java 1.4 if available, else
  use the old deprecated version. This method uses reflection to find the
  appropriate method; if the reflection operations throw exceptions, this
  will return the url encoded with the old URLEncoder.encode() method.
  
  @param enc
  The character encoding the urlencode is performed on.
  @return String The encoded url.