java 含有%文字 decode 异常 处理

阅读更多
含有百分号文字如:
欧洲7月电动车销量跌4% 高尔夫GTE最惨
docName = URLDecoder.decode(docName, "utf-8");


decode时会抛出异常

IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern


解决方法 : 将%替换为 %25

docUrl = docUrl.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            docName = docName.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            content = content.replaceAll("%(?![0-9a-fA-F]{2})", "%25");

            docUrl = URLDecoder.decode(docUrl, "utf-8");
            docName = URLDecoder.decode(docName, "utf-8");
            content = URLDecoder.decode(content, "utf-8");



参考资料:
http://stackoverflow.com/questions/6067673/urldecoder-illegal-hex-characters-in-escape-pattern-for-input-string

你可能感兴趣的:(java)