4

如果我想在 Java 中粘贴以下 URL:

UNICODE 中 URL 的屏幕截图

...我应该对字符串使用什么句柄。

到目前为止,我一直无法处理那个字符串,我所拥有的只是????字符。

谢谢。

2012.09.09修改:

package pruebas;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Vector;

public class Prueba03
{
    public static void main(String argumentos[])
    {
        Vector<String> listaURLs = new Vector<String>();

        listaURLs.add("http://президент.рф/");
        listaURLs.add("http://www.中国政府.政务.cn");
        listaURLs.add("http://www.原來我不帥.cn/");
        listaURLs.add("http://وزارة-الأتصالات.مصر/");

        URL currentURL;
        URLConnection currentConnection;
        int currentSize;

        for(int i=0; i<listaURLs.size(); i++)
        {
            try
            {
                System.out.println(URLDecoder.decode(listaURLs.get(i), URLEncoder.encode(listaURLs.get(i), "UTF-8")));
            } // End of the try.
            catch(UnsupportedEncodingException uee)
            {
                uee.printStackTrace();
            } // End of the catch.
            catch(Exception e)
            {
                e.printStackTrace();
            } // End of the catch.

            try
            {
                currentURL = new URL(listaURLs.get(i));
                System.out.println("currentURL" + " = " + currentURL);

                currentConnection = currentURL.openConnection();
                System.out.println("currentConnection" + " = " + currentConnection);

                currentSize = currentConnection.getContentLength();
                System.out.println("currentSize" + " = " + currentSize);
            } // End of the try.
            catch(Exception e)
            {
                e.printStackTrace();
            } // End of the catch.
        } // End of the for.
    } // End of the main method.
} // End of the Prueba02 class.
4

4 回答 4

1

对于域名,您应该使用Punycode转换 unicode 主机名。Punycode 是一种将 unicode 字符串转换为 ascii 字符串的方法。

以下链接显示了将 Unicode 域名转换为国际域名的 JAVA 方法。 https://docs.oracle.com/javase/6/docs/api/java/net/IDN.html#toASCII(java.lang.String)

    URL u = new URL(url);
    String host = u.getHost();

    String[] labels = host.split("\\.");
    for (int i = 0; i < labels.length; i++) {
        labels[i] = java.net.IDN.toUnicode(labels[i]);
    }
    host = StringUtils.join(labels, ".");
    System.out.println(host);

此外,您可以使用在线 punycode 转换器测试一些 unicode URL。 https://www.punycoder.com/

例如,将“ http://www.中国政府.政务.cn ”转换为“ http://www.xn--fiqs8sirgfmh.xn--zfr164b.cn/ ”。

于 2015-10-19T12:07:38.510 回答
-1

您可以尝试以下代码:

import java.net.URLDecoder;
import java.net.URLEncoder;

public class Test7 {
public static void main(String[] args) throws Exception {
    String str = "http://www.中国政府.政务.cn";
    System.out.println(URLDecoder.decode(str, URLEncoder.encode(str,
            "UTF-8")));
    }
}
于 2012-09-09T03:19:35.317 回答
-2

不确定“解析”是什么意思——你打算用这些部分做什么?
据我所知,UTF-8 支持阿拉伯语和俄语。
不确定您的数据源是什么(可能是某种 Stream?),但String有一个接受所需编码的 CTOR。
您应该能够获得一个不包含 ??? 的字符串 当涉及到阿拉伯语和俄语时,如果您使用此 CTOR(带有“UTF-8”参数)

于 2012-09-09T03:07:50.270 回答
-2

您可以尝试使用以下内容:

String pageUrl = "http://www.中国政府.政务.cn";

try 
{
        URL url = new URL(pageUrl);
        System.out.println(url.toURI().toASCIIString());
}

catch (MalformedURLException e1) 
{
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

catch (URISyntaxException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

结果如预期: http://www.%E4%B8%AD%E5%9B%BD%E6%94%BF%E5%BA%9C.%E6%94%BF%E5%8A%A1。 cn

但是转换成URI有它自己的缺点,你应该手动替换'|', '"', '#'它的URL编码之类的特殊字符。

于 2013-07-22T05:09:22.800 回答