源字符串: a a 1 ~`!@#$%^&()_+-={}[];',.- + 编码后: a%20a%201%20~%60%21@%23$%25%5E&%28%29_+-=%7B%7D%5B%5D;%27,.-%20+
源字符串: 变 ~!@#¥%…………&()——+=-·{}:“;‘、《》?,。、-+A a 1 编码后: 变%20~%EF%BC%81@%23%EF%BF%A5%25%E2%80%A6%E2%80%A6%E2%80%A6%E2%80%A6&%EF%BC%88%EF%BC%89%E2%80%94%E2%80%94+=-%C2%B7%7B%7D%EF%BC%9A%E2%80%9C%EF%BC%9B%E2%80%98%E3%80%81%E3%80%8A%E3%80%8B%EF%BC%9F%EF%BC%8C%E3%80%82%E3%80%81-+A%20a%201
/*** 针对七牛含有特殊字符的文件名,对特殊字符编码处理* @param str* @return*/public static String encoderNew(String str){if(org.apache.commons.lang3.StringUtils.isEmpty(str)){return str;}StringBuilder res = new StringBuilder();//需要编码的字符Listlist = Arrays.asList(new String[]{" ", "`", "!", "#", "%", "^", "(", ")", "{", "[", "]", "}", "'", "*", "!", "¥", "…", "(", ")", "—", "·", ":", "“", ";", "‘", "、", "《", "》", "?", ",", "。", "、"});char[] cs = str.toCharArray();try{for(int i = 0; i < cs.length; i ++){String temp = String.valueOf(cs[i]);if(list.contains(temp)){if(isDbcCase(cs[i])){//包含特殊字符 - 半角res.append("%");res.append(Integer.toHexString((int)cs[i]).toUpperCase());}else {//包含特殊字符 - 全角res.append(URLEncoder.encode(temp, "UTF-8").toUpperCase());}}else {//不包含特殊字符 res.append(temp);}}}catch (Exception e){logger.error("encoderNew error msg=", e);}return res.toString();}/*** 半角、全角字符判断** @param c* 字符* @return true:半角; false:全角*/public static boolean isDbcCase(char c) {// 基本拉丁字母(即键盘上可见的,空格、数字、字母、符号)if (c >= 32 && c <= 127) {return true;}// 日文半角片假名和符号else if (c >= 65377 && c <= 65439) {return true;}return false;}