Java转译URL参数
生活随笔
收集整理的這篇文章主要介紹了
Java转译URL参数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java轉(zhuǎn)譯URL參數(shù)
//調(diào)用 String input = URIEncoderUtil.encodeURIComponent(String input); public class URIEncoderUtil {public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";/*** Description:轉(zhuǎn)譯URL* * @param input* @return* @see*/public static String encodeURIComponent(String input) {if (null == input || "".equals(input.trim())) {return input;}int l = input.length();StringBuilder o = new StringBuilder(l * 3);try {for (int i = 0; i < l; i++) {String e = input.substring(i, i + 1);if (ALLOWED_CHARS.indexOf(e) == -1) {byte[] b = e.getBytes("utf-8");o.append(getHex(b));continue;}o.append(e);}return o.toString();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return input;}private static String getHex(byte buf[]) {StringBuilder o = new StringBuilder(buf.length * 3);for (int i = 0; i < buf.length; i++) {int n = (int) buf[i] & 0xff;o.append("%");if (n < 0x10) {o.append("0");}o.append(Long.toString(n, 16).toUpperCase());}return o.toString();} }總結(jié)
以上是生活随笔為你收集整理的Java转译URL参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸿鹄元数正式加入openGauss社区
- 下一篇: eclipse开发安卓android程序