【问题标题】:Accessing a URL with accent in Java is not working在 Java 中访问带有重音符号的 URL 不起作用
【发布时间】:2013-07-14 02:24:18
【问题描述】:

以下代码为我检索 URL 内容并将其保存到文件中:

URI obj_uri = new URI(uri);

URLConnection connection = obj_uri.toURL().openConnection();
connection.connect();

byte[] buffer = new byte[1024];
String filename = "temp";

try (InputStream in = connection.getInputStream(); FileOutputStream writer = new FileOutputStream( new File(filename) )) {
    int len;
    while ( (len = in.read(buffer)) != -1) {
        writer.write( buffer, 0, len );
    }
}

return filename;

我的问题是,当这个 URI 有不同的符号时,例如“http://dbpedia.org/resource/Brasília”,内容无法检索。 DBpedia 使用的 URL 编码是 UTF-8,即使使用 URLEncoder.encode( url, "UTF-8") 我的代码也不起作用。但是我的代码不会产生错误或异常,生成的文件(“temp”)以没有信息结尾(除了“”和“”)。

那么,如果 URI 是正确的并且即使使用其符号也可以访问,为什么我无法检索其内容并将其保存到文件中?

感谢您的帮助!

【问题讨论】:

    标签: java url utf-8 url-encoding


    【解决方案1】:
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    相关资源
    最近更新 更多