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

前端 未结 4 1141
渐次进展
渐次进展 2020-12-10 06:51

I am building an Android Application and I need to download a file from a url, which is 33 MB large.

Here the download task:

try {
            int MA         


        
相关标签:
4条回答
  • 2020-12-10 07:37

    i have a answer for this. you should add a header to your connection.

    connection.setRequestProperty("Accept-Encoding", "identity");
    

    this will help you.say me if it help...

    0 讨论(0)
  • 2020-12-10 07:48

    Setting a chunk size seemed to work for me.

    connection.setChunkedStreamingMode(1048576);
    
    0 讨论(0)
  • 2020-12-10 07:54

    While you catch the exception, I try the method downContinue(). I can show my code:

    private void downloadApk() {
        thread1 = new Thread() {
            public void run() {
                File oFile = null;
                try {
                    URL url = new URL(PQGLApplication.resrootURL + "apk/PQGLMap.apk");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    ReadableByteChannel channel =
                            Channels.newChannel(urlConnection.getInputStream());
                    oFile =
                            new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                                    + "/" + "hy_ht_new/" + "test2" + ".apk");
                    oFile.setWritable(true);
                    oFile.setReadable(true);
                    if (oFile.exists()) {
                        oFile.delete();
                    }
                    FileOutputStream fos = new FileOutputStream(oFile);
                    fileSize = urlConnection.getContentLength();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int noOfBytes = 0;
                    byte[] data = null;
                    sendApkMessage(0, 0);
                    while ((noOfBytes = channel.read(buffer)) > 0) {
                        data = new byte[noOfBytes];
                        System.arraycopy(buffer.array(), 0, data, 0, noOfBytes);
                        buffer.clear();
                        fos.write(data, 0, noOfBytes);
                        downLoadFileSize += noOfBytes;
                        sendApkMessage(1, downLoadFileSize);
                    }
                    fos.flush();
                    fos.close();
                    channel.close();
                    sendApkMessage(2, oFile.getAbsolutePath());
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    downContinue();
                }
            };
        };
        thread1.start();
    }
    
    private void downContinue() {
        continueTime++;
        try {
            if (continueTime == 3) {
                continueTime = 0;
                sendApkMessage(4, 0);
                Log.e("what is the continuetime", "continueTime" + continueTime);
            } else {
                URL url = new URL(PQGLApplication.resrootURL + "apk/PQGLMap.apk");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                File oFile =
                        new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
                                + "hy_ht_new/" + "test2" + ".apk");
                RandomAccessFile oSavedFile = new RandomAccessFile(oFile, "rw");
                FileOutputStream fos = new FileOutputStream(oFile);
                ReadableByteChannel channel = Channels.newChannel(urlConnection.getInputStream());
                // oSavedFile.seek(nPos);
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                byte[] data = null;
                int temp = 0;
                sendApkMessage(3, oFile.getAbsolutePath());
                while ((temp = channel.read(buffer)) > 0) {
                    data = new byte[temp];
                    System.arraycopy(buffer.array(), 0, data, 0, temp);
                    buffer.clear();
                    fos.write(data, 0, temp);
                }
                fos.flush();
                fos.close();
                oSavedFile.close();
                sendApkMessage(2, oFile.getAbsolutePath());
                continueTime = 0;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.e("what is the exception", e.toString() + continueTime);
            downContinue();
        }
    
    }
    

    This downContinue method is used to solve this problem. At least, the file is downloaded successfully!

    0 讨论(0)
  • 2020-12-10 07:56

    For large files you need to set the connection time out manually by using the following code. I have set the time out to 3 minutes

       connection.setConnectTimeout(180000);
       connection.setReadTimeout(180000);
    
    0 讨论(0)
提交回复
热议问题