4

次の URL を Java で貼り付けたい場合:

UNICODE の 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.
4

4 に答える 4

-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 でサポートされています。
あなたのデータのソースが何であるかはわかりませんが(おそらくストリームのようなものですか?)、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 に答える