Возобновить файл после сбоя сетевого подключения - PullRequest
0 голосов
/ 02 мая 2019

Я пишу программу для загрузки файла из Azure. Но он не возобновляет работу после сбоя сети. Файл загружается в фоновом режиме с помощью IntentService. Я упомянул this. Я хочу следить за тем, какие части файла я скачал, и в случае прерывания возобновить с того места, где я остановился. Я даже не могу найти что-либо на Google. Если кто-нибудь знает решение о том, как возобновить файл при возникновении сетевой ошибки. Помощь будет оценена.

try {
            byte data[] = new byte[1024 * 4];
            fileSize = body.contentLength();
            InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
            mFile = ContextCompat.getExternalFilesDirs(this, null)[1];
            File outputFile = new File(FILE_STORAGE_PATH + File.separator + "download", fileName);
            OutputStream output = new FileOutputStream(outputFile);
            long total = 0;
            long startTime = System.currentTimeMillis();
            int timeCount = 1;
            while ((count = bis.read(data)) != -1) {
                total += count;
                totalFileSize = (int) (fileSize / (Math.pow(1024, 2)));
                double current = Math.round(total / (Math.pow(1024, 2)));
                int progress = (int) ((total * 100) / fileSize);
                long currentTime = System.currentTimeMillis() - startTime;
                Download download = new Download();
                download.setTotalFileSize(totalFileSize);
                download.setFileSize(fileSize);
                download.setFileName(fileName);
                download.setFileVersion(fileVersion);
                if (currentTime > 1000 * timeCount) {
                    download.setCurrentFileSize((int) current);
                    download.setProgress(progress);
                    sendNotification(download);
                    timeCount++;
                }
                output.write(data, 0, count);
            }
            onDownloadComplete();
            output.flush();
            output.close();
            bis.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
...