gpt4 book ai didi

android - 下载文件时流结束错误?

转载 作者:行者123 更新时间:2023-11-30 04:27:35 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Android:“Unexpected end of stream” exception downloading large files

我正在下载一个包含以下代码的文件。该文件是约。大小为 5MB。但是,当下载大约 60-90% 时,我收到 java.io.IOException“流的意外结束”错误

我不知道如何解决它,这让我发疯。

编辑:如果它能在您的手机上成功下载文件,至少有人可以测试一下。这将使我能够确定问题出在我的手机上还是代码上。

try {
URL url = new URL(full_url);
conexion = (URLConnection)url.openConnection();

conexion.setReadTimeout(20000);
conexion.connect();
File file = new File(root.getAbsolutePath()+"/", fileName);

int lenghtOfFile = conexion.getContentLength();
System.out.println("content-length-header is: " + lenghtOfFile);
InputStream input = conexion.getInputStream();

OutputStream output = new FileOutputStream(file);

byte data[] = new byte[8192];
long total = 0;
contentView.setTextViewText(R.id.status_text,"Downloading file " + (78 - GlobalData.missingFiles.size()) + " of " + 77);
int downloadProgress = (int)((total*100)/lenghtOfFile);

int lastProgressUpdate=0;

while ((count = input.read(data)) != -1) {
System.out.println("available bytes:" + input.available());
total += count;

downloadProgress = (int)((total*100)/lenghtOfFile);
Log.e("totaltotal","" + (int)((total*100)/lenghtOfFile));

output.write(data,0,count);
if(downloadProgress%20==0 && downloadProgress != lastProgressUpdate) {
notification.contentView.setProgressBar(R.id.status_progress, 100,downloadProgress, false);
notificationManager.notify(1,notification);
lastProgressUpdate=downloadProgress;
}
if(downloadProgress == 100){
GlobalData.downloadFiles.add("" +fileName);
GlobalData.missingFiles.remove(fileName);
}
}

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

if(downloadProgress != 100){
File temp_file = new File(root.getAbsolutePath()+"/", fileName);
try{
if(temp_file.exists()){
boolean del_main = temp_file.delete();
Log.e("File","Does file exists: " + del_main);
Log.e("FilePath","PATH: " + temp_file.getAbsolutePath());
}else{
Log.e("File Exists NOT","NOT EXISTING");
}
}catch(Exception e){
Log.e("FileDelete","deleting is giving problems");
}
}
} catch (Exception e) {
Log.e("PRINTSTACK","STACK:" + e.getMessage());
e.printStackTrace();
System.out.println("Downloading didn't work");

killService();
}

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