我正在尝试通过 http 连接下载二进制文件。但是,我的代码会引发 java.io.FileOutputStream.write(Unknown Source) 错误。我不确定我做错了什么。
public void GetFileDownload(String URI) throws IOException{
/*
* Given a URI this method will grab the binary data from that page over the http protocol
*/
URL inputURI;
HttpURLConnection connect;
BufferedReader input;
inputURI = new URL(URI);
connect = (HttpURLConnection) inputURI.openConnection();
connect.setReadTimeout(10000);
connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401");
connect.setRequestMethod("GET");
input = new BufferedReader(new InputStreamReader(connect.getInputStream()));
byte[] buffer = new byte[4096];
int n = - 1;
String file = "test";
int i = 0;
OutputStream output = new FileOutputStream(file);
while (i != buffer.length - 1){
i++;
System.out.print(buffer[i]);
}
while ((n = input.read()) != -1)
output.write(buffer, 0, n);
output.close();
}