为什么下载一个TXT文本下载的内容不全

问题描述:

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();

上传的不全,或者系统故障丢失了

你把os.flush();写到while循环里面去试试