繁体   English   中英

在Java中检测URL的编码

[英]Detect encoding of URL in Java

我在数据库中有一个混合数据的情况,我试图看看这是否是一个可以解决的问题。 我所拥有的是三种格式之一的部分URL:

/some/path?ugly=häßlich // case 1, Encoding: UTF-8 (plain)
/some/path?ugly=h%C3%A4%C3%9Flich // case 2, Encoding: UTF-8 (URL-encoded)
/some/path?ugly=h%E4%DFlich // case 3: Encoding: ISO-8859-1 (URL-encoded)

我的应用程序需要的是URL编码的UTF8版本

/some/path?ugly=h%C3%A4%C3%9Flich // Encoding: UTF-8 (URL-encoded)

数据库中的字符串都是UTF-8,但URL编码可能存在也可能不存在,可能是任何一种格式。

我有一个方法a将普通的UTF-8编码为URL编码的UTF-8,我有一个方法b将URL编码的ISO-8859-1解码为普通的UTF-8,所以基本上我打算做的是:

情况1:

String output = a(input);

案例2:

String output = input;

案例3:

String output = a(b(input));

如果我知道哪个是哪个,那么所有这些情况都可以正常工作,但是我有一个安全的方法来检测这样的字符串是2还是3? (我可以将参数中使用的语言限制为欧洲语言:德语,英语,法语,荷兰语,波兰语,俄语,丹麦语,挪威语,瑞典语和土耳其语,如果有任何帮助的话)。

我知道明显的解决方案是清理数据,但不幸的是,数据不是由我自己创建的,也不是那些拥有必要技术理解的人(并且有大量遗留数据需要工作)

如果您可以假设只编码字母数字,请按照以下方式进行操作:

  • “häßlich”
  • “H%C3%A4%C3%9Flich”
  • “H%E4%DFlich”

//首先检查:

public static boolean isUtf8Encoded(String url) {
    return isAlphaNumeric(url);
}

public static boolean isUrlUtf8Encoded(String url)
        throws UnsupportedEncodingException {
    return isAlphaNumeric(URLDecoder.decode(url, "UTF-8"));
}

public static boolean isUrlIsoEncoded(String url)
        throws UnsupportedEncodingException {
    return isAlphaNumeric(URLDecoder.decode(url, "ISO-8859-1"));
}

private static boolean isAlphaNumeric(String decode) {
    for (char c : decode.toCharArray()) {
        if (!Character.isLetterOrDigit(c)) {
            return false;
        }
    }
    return true;
}

您可以在首次解码然后进行编码时进行解决,如果您有未编码的URL,则不会受到解码的影响

 String url = "your url";
    url=URIUtil.decode(url, "UTF-8");
    url=URIUtil.encodeQuery(url, "UTF-8");

感谢接受的答案,但它不适用于URL,因为URL还包含控制字符,这是我的解决方案:

/**
 * List of valid characters in URL.
 */
private static final List VALID_CHARACTERS = Arrays.asList(
        '-', '.', '_', '~', ':', '/', '?', '#', '[', ']', '@', '!',
        '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='
);

/**
 * Check that decoding was successful or not.
 * @param url URL to check
 * @return True if it's valid.
 */
private static boolean isMalformed(final String url) {
    for (char c : url.toCharArray()) {
        if (VALID_CHARACTERS.indexOf(c) == -1 && !Character.isLetterOrDigit(c)) {
            return false;
        }
    }
    return true;
}

/**
 * Try to decode URL with specific encoding.
 * @param url URL
 * @param encoding Valid encoding
 * @return Decoded URL or null of encoding is not write
 * @throws java.io.UnsupportedEncodingException Throw if encoding does not support on your system.
 */
private static String _decodeUrl(final String url, final String encoding) {
    try {
        final String decoded = URLDecoder.decode(url, encoding);
        if(isMalformed(decoded)) {
            return decoded;
        }
    }
    catch (UnsupportedEncodingException ex) {
        throw new IllegalArgumentException("Illegal encoding: " + encoding);
    }
    return null;
}

/**
 * Decode URL with most popular encodings for URL.
 * @param url URL
 * @return Decoded URL or original one if encoding does not support.
 */
public static String decodeUrl(final String url) {
    final String[] mostPopularEncodings = new String[] {"iso-8859-1", "utf-8", "GB2312"};
    return decodeUrl(url, mostPopularEncodings);
}

/**
 * Decode URL with most popular encodings for URL.
 * @param url URL
 * @param encoding Encoding
 * @return Decoded URL or original one if encoding does not support.
 */
public static String decodeUrl(final String url, final String... encoding) {
    for(String e:encoding) {
        final String decoded;
        if((decoded = _decodeUrl(url, e)) != null) {
            return decoded;
        }
    }
    return url;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM