JAVA运⽤URL类openStream⼀个路径中,⽂件名为中⽂的问题url 中可能带有中⽂、空格或者单引号等字符,需要进⾏重编码,解决办法如下:
publich void  test(){
url = encodeURLChinese(url);
}
/**
* 获取按要求编码后的URL列表
*
* @param url
* @return
*/
public static String encodeURLChinese(String url) {
if (StringUtils.isEmpty(url)) {
return null;
}
url = im(url);
try {
if (!needEncoding(url)) {
// 不需要编码
return url;
} else {
// 需要编码
String allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%\"";
/
/              String  allowChars = ".!*'();:@&=+_\\-$,/?#\\[\\]{}|\\^~`<>%\"";
// UTF-8 ⼤写
return encode(url, ENCODE_FORMAT_UTF8, allowChars, false);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 判断⼀个url是否需要编码,按需要增减过滤的字符
*
* @param url
* @return
*/
public static boolean needEncoding(String url) {
// 不需要编码的正则表达式
//      String allowChars = Value("ENCODING_ALLOW_REGEX",
//              Constants.ENCODING_ALLOW_REGEX);
if (url.matches("^[0-9a-zA-Z.:/?=&%~`#()-+]+$")) {
return false;
}
return true;
}
/**
* 对字符串中的特定字符进⾏编码
*
* @param s
*            待编码的字符串
* @param enc
*            编码类型
* @param allowed
*            不需要编码的字符
* @param lowerCase
*            true:⼩写 false:⼤写
* @return
* @throws java.io.UnsupportedEncodingException
*/
public static final String encode(String s, String enc, String allowed,
boolean lowerCase) throws UnsupportedEncodingException {
byte[] bytes = s.getBytes(enc);
int count = bytes.length;
/*
* From RFC 2396:
*
* mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" reserved =
* ";" | "/" | ":" | "?" | "@" | "&" | "=" | "+" | "$" | ","
*/
// final String allowed = "=,+;.'-@&/$_()!~*:"; // '?' is omitted
char[] buf = new char[3 * count];
int j = 0;
for (int i = 0; i < count; i++) {
if ((bytes[i] >= 0x61 && bytes[i] <= 0x7A) || // a..z
(bytes[i] >= 0x41 && bytes[i] <= 0x5A) || // A..Z
(bytes[i] >= 0x30 && bytes[i] <= 0x39) || // 0..9
(allowed.indexOf(bytes[i]) >= 0)) {
buf[j++] = (char) bytes[i];
} else {
buf[j++] = '%';
if (lowerCase) {
buf[j++] = Character.forDigit(0xF & (bytes[i] >>> 4), 16);
buf[j++] = Character.forDigit(0xF & bytes[i], 16);
} else {
buf[j++] = lowerCaseToUpperCase(Character.forDigit(
0xF & (bytes[i] >>> 4), 16));
buf[j++] = lowerCaseToUpperCase(Character.forDigit(
0xF & bytes[i], 16));
}
}
}
return new String(buf, 0, j);
}
public static char lowerCaseToUpperCase(char ch) {
if (ch >= 97 && ch <= 122) { // 如果是⼩写字母就转化成⼤写字母
ch = (char) (ch - 32);
}
return ch;
}url编码和utf8区别

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。