2013-07-16 22 views
1

下面的代碼的URL檢索了我一個網址的內容並把它保存到一個文件:訪問與口音在Java中不工作

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是正確的並且即使使用其符號也可以訪問,爲什麼我無法檢索其內容並將其保存到文件中?

謝謝你的幫助!

回答