gpt4 book ai didi

java - 文件下载代码下载比原始文件大的文件

转载 作者:行者123 更新时间:2023-12-01 15:44:28 24 4
gpt4 key购买 nike

我正在下载一些 .zip 文件,但当我尝试解压缩它们时,我收到“数据错误”。现在我去看了下载的文件,它们比原始文件大。这可能是错误的原因吗?

下载文件的代码:

URL=intent.getStringExtra("DownloadService_URL");
FileName=intent.getStringExtra("DownloadService_FILENAME");
Path=intent.getStringExtra("DownloadService_PATH");

try{


URL url = new URL(URL);
URLConnection conexion = url.openConnection();

conexion.connect();
int lenghtOfFile = conexion.getContentLength();
lenghtOfFile/=100;

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Path+FileName);

byte data[] = new byte[1024];
long total = 0;

int count = 0;
while ((count = input.read(data)) != -1) {
output.write(data);
total += count;

notification.setLatestEventInfo(context, contentTitle, "Starting download " + FileName + " " + (total/lenghtOfFile), contentIntent);
mNotificationManager.notify(1, notification);
}


output.flush();
output.close();
input.close();

解压缩代码:

try {

String zipFile = Path + FileName;


FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);

ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
UnzipCounter++;
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(Path
+ ze.getName());
while ((Unziplength = zin.read(Unzipbuffer)) > 0) {
fout.write(Unzipbuffer, 0, Unziplength);
}
zin.closeEntry();
fout.close();

}

}
zin.close();
File f = new File(zipFile);
f.delete();
notification.setLatestEventInfo(context, contentTitle, "File successfully downloaded", contentIntent);
mNotificationManager.notify(1, notification);

} catch (Exception e) {

notification.setLatestEventInfo(context, contentTitle, "Problem in downloading file ", contentIntent);
mNotificationManager.notify(1, notification);

}

}

解压缩过程开始,但在提取一些出现该错误的文件后停止。我尝试了另一个 .zip 文件,但出现 CRC 失败错误。我用 winrar 测试了这两个 .zip 文件。

原始文件大小:3.67mb ..下载文件大小:3.93mb

最佳答案

您总是将完整的字节数组写入磁盘,而不检查读入的数据量。

另外,从性能的角度来看,任何 <1500 字节(即通常的以太网 MTU)都是一个非常糟糕的主意 - 尽管我认为 Java 无论如何都会缓冲这个值,但为什么要冒险呢?

关于java - 文件下载代码下载比原始文件大的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7371004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com