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

标签 android

<分区>

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

最佳答案

由于某种原因,输入流过早关闭。这通常是因为一次输入流被多次读取,但我猜这不是这里的情况,即使出于某种原因你的 conexion 可用范围比我们在这里看到的更广。

您说 conexion = (URLConnection)url.openConnection(); 而不是预期的 URLConnection conexion = url.openConnection();。请确保您没有尝试两次获取输入流。

当我查看时,我唯一能看到的另一件奇怪的事情是您直接使用来自 conexion.getInputStream() 的输入流。尝试将其包装在缓冲输入流中:

InputStream input = new BufferedInputStream(connexion.getInputStream());

关于android - 下载文件时流结束错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8206150/

相关文章:

android - getResources 和 String Array 强制关闭

android - 为什么我没有收到已安装的应用程序广播?

android - 更改 TabLayout 中选项卡的选择颜色

java - 我想在我的申请中提供一些 pdf 文件

android - 如何防止在背面 fragment 导航中再次设置 View 模型

java - 错误: @dagger. 使用了android.ContributesAndroidInjector,但是找不到dagger.android.processor.AndroidProcessor

java - 按下按钮时未输入任何内容时 Android 应用程序崩溃

android - 如何用动画扩展布局高度?

android - 内容 ://sms/sent/not working

Android,在应用程序中使用 Realm 单例