使用异步下载多个文件

时间:2019-10-25 13:51:48

标签: android

我正尝试使用asyc下载多个文件,但由于某种原因,它只能成功下载一个文件,而其他所有文件均为0字节。

仅成功下载首先添加到字符串数组的文件,其余文件以0字节的形式写入SD卡。

    class DownloadFileFromFTP extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String... f_url) {

            //code
            //Read SharedPref
            String str_dirRestoredImage_open = sharedPref.getString("SharedPref_dirRestoredImage_open", "");
            int count;
            OutputStream output = null;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();

                int lenghtOfFile = conection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream(), 8192);

                int totalFilesChecked = arrListStr_Files_Checked.size();

                for (int i=0;i<totalFilesChecked;i++)
                {
                    // Output stream
                    file_name_remote_abs = stringArr_Files_Checked[i];
                    file_name_remote = file_name_remote_abs.substring(file_name_remote_abs.lastIndexOf('/') + 1);
                    output = new FileOutputStream(str_dirRestoredImage_open+"/"+file_name_remote);

                    Log.d("LOG", "zzz_i: "+i +" :"+file_name_remote);

                    byte data[] = new byte[1024];

                    long total = 0;

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

                        publishProgress("" + (int) ((total * 100) / lenghtOfFile));

                        // writing data to file
                        output.write(data, 0, count);
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            // Stuff that updates the UI
                            Toast.makeText(getApplicationContext(), "File Restored: "+file_name_remote, Toast.LENGTH_LONG).show();
                        }
                    });
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        // Stuff that updates the UI
                        Toast.makeText(getApplicationContext(), "Failed To Restore: "+file_name_remote, Toast.LENGTH_LONG).show();
                    }
                });

            }

            return null;
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            //pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            //dismissDialog(progress_bar_type);

        }
    }

这就是我的称呼方式,其中stringArr_Files_Checked是文件绝对URL的字符串数组。

new DownloadFileFromFTP().execute(stringArr_Files_Checked);

0 个答案:

没有答案