package com.sunvote.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; /** * Created by Elvis on 2017/8/11. * Email:Eluis@psunsky.com * Description: * Encode工具类 */ public class EncodeUtils { private EncodeUtils() { throw new UnsupportedOperationException("No Impl"); } /** * URL编码 *
若想自己指定字符集,可以使用{@link #urlEncode(String input, String charset)}方法
* * @param input 要编码的字符 * @return 编码为UTF-8的字符串 */ public static String urlEncode(String input) { return urlEncode(input, "UTF-8"); } /** * URL编码 *若系统不支持指定的编码字符集,则直接将input原样返回
* * @param input 要编码的字符 * @param charset 字符集 * @return 编码为字符集的字符串 */ public static String urlEncode(String input, String charset) { try { return URLEncoder.encode(input, charset); } catch (UnsupportedEncodingException e) { return input; } } /** * URL解码 *若想自己指定字符集,可以使用 {@link #urlDecode(String input, String charset)}方法
* * @param input 要解码的字符串 * @return URL解码后的字符串 */ public static String urlDecode(String input) { return urlDecode(input, "UTF-8"); } /** * URL解码 *若系统不支持指定的解码字符集,则直接将input原样返回
* * @param input 要解码的字符串 * @param charset 字符集 * @return URL解码为指定字符集的字符串 */ public static String urlDecode(String input, String charset) { try { return URLDecoder.decode(input, charset); } catch (UnsupportedEncodingException e) { return input; } } }