try {
// 1.以要下載文件的 URL 為參數創建 URL 對象
URL url = new URL(sfPath);
// 2.打開連接下載文件
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
String filename = sfPath.substring(sfPath.lastIndexOf("/") + 1);
File file = new File(sfFilePath);
if (!file.exists()) {
file.mkdir();
}
File sfFile = new File(sfFilePath + filename);
sfFile.createNewFile();
OutputStream os = new FileOutputStream(sfFile);
byte[] buff = new byte[1024 * 4];
while (is.read(buff) != -1) {
os.write(buff);
}
is.close();
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
為什麼下載文本內容不全,求大神指導下。。。。。。。。。
byte[] buff = new byte[1024 * 4];
int len = -1;
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);//使用這個方法,能防止下載的文件變大
}
os.close();//這裡要先關閉輸出字節流
is.close();