在Java中使用重音访问URL无效
以下代码为我检索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
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是正确的,甚至可以使用其符号访问,为什么我不能检索其内容并将其保存到文件中
谢谢你的帮助 如果没有其他办法,也试试这些