我只是尝试从网站下载文件,我检查了网址,一切似乎都很好,但当我尝试按照描述的方式进行操作时,打印出我的更新大小为-1 Bytes
,不知道为什么会这样。
如果有人有解决方案,我会很高兴在这里。
我的代码:
private void downloadFile(String link) throws MalformedURLException, IOException
{
URL url = new URL(link);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
long max = conn.getContentLength();
outText.setText(outText.getText()+"\n"+"Downloding file...\nUpdate Size(compressed): "+max+" Bytes");
System.out.println("Update Size --> " + max + " Bytes");
BufferedOutputStream fOut = new BufferedOutputStream(new FileOutputStream(new File("update.zip")));
byte[] buffer = new byte[32 * 1024];
int bytesRead = 0;
int in = 0;
while ((bytesRead = is.read(buffer)) != -1) {
in += bytesRead;
fOut.write(buffer, 0, bytesRead);
}
fOut.flush();
fOut.close();
is.close();
outText.setText(outText.getText()+"\nDownload Complete!");
}
非常感谢:)
答案 0 :(得分:0)