Decode a string in Java
2015-12-30 17:18
375 查看
java中decode字符串: public class Utils { private static Pattern validStandard = Pattern.compile("%([0-9A-Fa-f]{2})"); private static Pattern choppedStandard = Pattern.compile("%[0-9A-Fa-f]{0,1}$"); private static Pattern validNonStandard = Pattern.compile("%u([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])"); private static Pattern choppedNonStandard = Pattern.compile("%u[0-9A-Fa-f]{0,3}$"); public static String resilientUrlDecode(String input) { String cookedInput = input; if (cookedInput.indexOf('%') > -1) { // Transform all existing UTF-8 standard into UTF-16 standard. cookedInput = validStandard.matcher(cookedInput).replaceAll("%00%$1"); // Discard chopped encoded char at the end of the line (there is no way to know what it was) cookedInput = choppedStandard.matcher(cookedInput).replaceAll(""); // Handle non standard (rejected by W3C) encoding that is used anyway by some // See: http://stackoverflow.com/a/5408655/114196 if (cookedInput.contains("%u")) { // Transform all existing non standard into UTF-16 standard. cookedInput = validNonStandard.matcher(cookedInput).replaceAll("%$1%$2"); // Discard chopped encoded char at the end of the line cookedInput = choppedNonStandard.matcher(cookedInput).replaceAll(""); } } try { return URLDecoder.decode(cookedInput,"UTF-16"); } catch (UnsupportedEncodingException e) { // Will never happen because the encoding is hardcoded return null; } } }
相关文章推荐
- 利用ReentrantLock的Condition实现线程之间的通信
- Java查询网址
- java知识--IO流详解
- Java9新特性介绍
- java Thread ,Thread.currentThread().getName() 的含义 & 普通成员变量是何时被赋值的
- Spring 学习笔记 (IV) 之 Spring 的事务
- java proxy
- Java Collections 框架
- Java NIO学习总结三(Selector)
- Spring注解@ResponseBody
- spring缓存实例
- 【算法基础】java中的移位运算符和位运算符
- Java Web(9)struts 2 validation result设置
- JVM(Java虚拟机)优化大全和案例实战
- java内存模型的三个代
- Java 泛型通配符?解惑
- Java总结篇系列:Java泛型
- spring 与 mybatis整合,mybatis注解不起作用
- AIDL中使用java类
- CSDN博客文章之Java资源