Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用重音访问URL无效_Java_Url_Utf 8_Url Encoding - Fatal编程技术网

在Java中使用重音访问URL无效

在Java中使用重音访问URL无效,java,url,utf-8,url-encoding,Java,Url,Utf 8,Url Encoding,以下代码为我检索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(); Fil

以下代码为我检索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有一个不同的符号时,例如“/巴西利亚”,无法检索内容。DBpedia使用的URL编码是UTF-8,甚至使用
urlcoder.encode(URL,“UTF-8”)
我的代码也不起作用。但我的代码不会生成错误或异常,生成的文件(“temp”)结束时没有任何信息(除了“”和“”)

因此,如果URI是正确的,甚至可以使用其符号访问,为什么我不能检索其内容并将其保存到文件中


谢谢你的帮助

如果没有其他办法,也试试这些


如果没有其他办法,也试试这些