1 /**
2 * 把字符串转成utf8编码,保证中文文件名不会乱码
3 * @param s
4 * @return
5 */
6 public static String toUtf8String(String s){
7 StringBuffer sb = new StringBuffer();
8 for (int i=0;i<s.length();i++){
9 char c = s.charAt(i);
10 if (c >= 0 && c <= 255){sb.append(c);}
11 else{
12 byte[] b;
13 try { b = Character.toString(c).getBytes("utf-8");}
14 catch (Exception ex) {
15 System.out.println(ex);
16 b = new byte[0];
17 }
18 for (int j = 0; j < b.length; j++) {
19 int k = b[j];
20 if (k < 0) k += 256;
21 sb.append("%" + Integer.toHexString(k).toUpperCase());
22 }
23 }
24 }
25 return sb.toString();
26 }
2 * 把字符串转成utf8编码,保证中文文件名不会乱码
3 * @param s
4 * @return
5 */
6 public static String toUtf8String(String s){
7 StringBuffer sb = new StringBuffer();
8 for (int i=0;i<s.length();i++){
9 char c = s.charAt(i);
10 if (c >= 0 && c <= 255){sb.append(c);}
11 else{
12 byte[] b;
13 try { b = Character.toString(c).getBytes("utf-8");}
14 catch (Exception ex) {
15 System.out.println(ex);
16 b = new byte[0];
17 }
18 for (int j = 0; j < b.length; j++) {
19 int k = b[j];
20 if (k < 0) k += 256;
21 sb.append("%" + Integer.toHexString(k).toUpperCase());
22 }
23 }
24 }
25 return sb.toString();
26 }