如何处理UNICODE URL? - java

如果要在Java中使用以下URL:

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

到目前为止,我一直无法处理那个String,我只剩下?字符

谢谢。

于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.

参考方案

对于域名,您应该使用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/”。

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…

JAVA 8具有任何匹配属性的对象的过滤器列表 - java

我的要求是通过匹配任何属性的字符串来过滤对象列表。例如,假设Contact类具有三个属性:街道,城市,电话。我知道java流过滤器是如何工作的,在这里我必须将输入字符串与每个属性进行比较,如下所示:contactList.stream().filter(contact -> contact.getStreet().equals("dubai&…