JAVA运用URL类 openStream 一个路径中,文件名为中文的问题

来源:互联网 发布:用jq优化页面加载速度 编辑:程序博客网 时间:2024/06/04 18:10

url 中可能带有中文、空格或者单引号等字符,需要进行重编码,解决办法如下:

publich void  test(){  url = encodeURLChinese(url);}/**     * 获取按要求编码后的URL列表     *     * @param url     * @return     */    public static String encodeURLChinese(String url) {        if (StringUtils.isEmpty(url)) {            return null;        }        url = StringUtils.trim(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 = SystemConfig.getValue("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;    }
0 0
原创粉丝点击