To encode an URI you can simply use Java’s URLEncoder’s encode method which has been available since JDK 1.4.
String encodedUri; try { encodedUri = URLEncoder.encode(uri, "UTF-8"); } catch (UnsupportedEncodingException exc) { // this should never happen logger.warn("UTF-8 is not a supported encoding? Not encoding for now...", exc); encodedUri = uri; }