Download bunch of files from server

僤鯓⒐⒋嵵緔 提交于 2019-12-11 02:27:22

问题


I am trying to download a bunch of files from a PHP server and store them on an sdcard. But when I download the files, I am getting the following error : No of files download from server is **500 -600** after download some files **250 - 260**. It raised an error.

Error

    07-10 08:40:46.228: E/File Error(1837):   File id is 287  File path is http://192.168.1.21/SOCH/upload/chapter_36/5.9.pdf  Synch Path is /mnt/sdcard/SOCH/36/
    07-10 08:40:46.268: W/System.err(1837): java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@434ef2c0 rejected from java.util.concurrent.ThreadPoolExecutor@4364f0d0[Running, pool size = 128, active threads = 128, queued tasks = 128, completed tasks = 1]
    07-10 08:40:46.268: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
    07-10 08:40:46.268: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
    07-10 08:40:46.268: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
    07-10 08:40:46.268: W/System.err(1837):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589)
    07-10 08:40:46.268: W/System.err(1837):     at com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146)
    07-10 08:40:46.278: W/System.err(1837):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
    07-10 08:40:46.278: W/System.err(1837):     at android.app.ActivityThread.access$1900(ActivityThread.java:130)
    07-10 08:40:46.278: W/System.err(1837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    07-10 08:40:46.278: W/System.err(1837):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-10 08:40:46.278: W/System.err(1837):     at android.os.Looper.loop(Looper.java:137)
    07-10 08:40:46.278: W/System.err(1837):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    07-10 08:40:46.278: W/System.err(1837):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-10 08:40:46.278: W/System.err(1837):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-10 08:40:46.278: W/System.err(1837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    07-10 08:40:46.278: W/System.err(1837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    07-10 08:40:46.278: W/System.err(1837):     at dalvik.system.NativeStart.main(Native Method)
    07-10 08:40:46.278: E/File Error(1837):   File id is 288  File path is http://192.168.1.21/SOCH/upload/chapter_37/6.1.pdf  Synch Path is /mnt/sdcard/SOCH/37/
    07-10 08:40:46.308: W/System.err(1837): java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@43605840 rejected from java.util.concurrent.ThreadPoolExecutor@4364f0d0[Running, pool size = 128, active threads = 128, queued tasks = 128, completed tasks = 1]
    07-10 08:40:46.308: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
    07-10 08:40:46.308: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
    07-10 08:40:46.308: W/System.err(1837):     at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
    07-10 08:40:46.308: W/System.err(1837):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589)
    07-10 08:40:46.308: W/System.err(1837):     at com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146)
    07-10 08:40:46.308: W/System.err(1837):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
    07-10 08:40:46.308: W/System.err(1837):     at android.app.ActivityThread.access$1900(ActivityThread.java:130)
    07-10 08:40:46.308: W/System.err(1837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    07-10 08:40:46.308: W/System.err(1837):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-10 08:40:46.308: W/System.err(1837):     at android.os.Looper.loop(Looper.java:137)
    07-10 08:40:46.308: W/System.err(1837):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    07-10 08:40:46.308: W/System.err(1837):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-10 08:40:46.308: W/System.err(1837):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-10 08:40:46.308: W/System.err(1837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    07-10 08:40:46.308: W/System.err(1837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    07-10 08:40:46.308: W/System.err(1837):     at dalvik.system.NativeStart.main(Native Method)

Can anybody help me solve this problem? I am using asynchtask to do the download.

My Code is

package com.webservice;

public class FileTransferService extends Service {

        private static final int CORE_POOL_SIZE = 1;
    private static final int MAXIMUM_POOL_SIZE = 128;
    private static final int KEEP_ALIVE = 128 ;

    private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(MAXIMUM_POOL_SIZE);

    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
            private final AtomicInteger mCount = new AtomicInteger(1);

            public Thread newThread(Runnable r) {
                return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
            }
        };


    public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,TimeUnit.SECONDS, sPoolWorkQueue,sThreadFactory);

            int FileNo = 0;

            ArrayList<String> FileId = new ArrayList<String>();
            ArrayList<String> ServerPath = new ArrayList<String>();
            ArrayList<String> SynchPath = new ArrayList<String>();
            ArrayList<String> FileNameList = new ArrayList<String>();

            String FileTransferPath = "", SDcardPath = "";

            @Override
            public void onCreate() {
                super.onCreate();
                Log.d("Service Created", "Successfully");

                FileId.clear();
                ServerPath.clear();
                SynchPath.clear();
                FileNameList.clear();

                SDcardPath = Environment.getExternalStorageDirectory().getPath() + "/";

                // Get FileTransfer Link From The SD Card
                ......

                // Getting A url From DB

                .....

            }// End onCreate

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {

                Log.d("Service Started", "Successfully");

                Log.d("fileSize",""+FileId.size());
                        if (FileId.size() > 0) {

                    ContentValues mContentValuesUpdatefileStatus = new ContentValues();
                    mContentValuesUpdatefileStatus.put("Status", 1);

                        while (FileNo < FileId.size()) {

                        dbh.updateRecord("Mst_FileTransfer",mContentValuesUpdatefileStatus,"FileTransferId",FileId.get(FileNo));

                        try {

                            new DownloadFileFromURL().executeOnExecutor(THREAD_POOL_EXECUTOR,Integer.toString(FileNo));                         
                            //new DownloadFileFromURL().execute(Integer.toString(FileNo));                  

                        } catch (Exception e) {     
                            e.printStackTrace();
                            Log.e("File Error", "  File id is " + FileId.get(FileNo) + "  File path is " + ServerPath.get(FileNo) + "  Synch Path is " + SynchPath.get(FileNo));
                        }

                        FileNo +=1;
                    }

                }

                Log.d("Service Finish", "Successfully");
                stopSelf();

                return super.onStartCommand(intent, flags, startId);
            }

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

                int Count = 0;
                int File_Completed = 0;
                int FileNo;
                String strServerPath,strSynchPath,strFileName;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();

                }

                @Override
                protected synchronized String doInBackground(String... params) {

                    FileNo = Integer.parseInt(params[0]);

                    // Get File Path
                    strServerPath = ServerPath.get(FileNo).replaceAll(" ", "%20");
                    strSynchPath = SynchPath.get(FileNo);
                    strFileName = FileNameList.get(FileNo);

                    URLConnection mConnection = null;

                    Log.d("file", strSynchPath + strFileName);

                    File SynchPath = new File(strSynchPath);
                    if (!SynchPath.exists()) {
                        SynchPath.mkdirs();
                    }

                    try {

                        URL url = new URL(strServerPath);
                        mConnection = url.openConnection();
                        mConnection.connect();

                        // getting file length
                        int lenghtOfFile = mConnection.getContentLength();

                        // Output stream to write file
                        File outputFile = new File(SynchPath, strFileName);
                        FileOutputStream mFileOutputStream = new FileOutputStream(
                                outputFile);

                        InputStream mInputStream = url.openStream();
                        byte data[] = new byte[1024];

                        long File_Completed_Size = 0;

                        while ((Count = mInputStream.read(data)) != -1) {

                            File_Completed_Size += Count;
                            File_Completed = (int) ((File_Completed_Size * 100) / lenghtOfFile);

                            // writing data to file
                            mFileOutputStream.write(data, 0, Count);
                        }

                        // flushing output
                        mFileOutputStream.flush();

                        // closing streams
                        mFileOutputStream.close();
                        mInputStream.close();

                    } catch (IOException IO) {

                        Log.e("File Exception","  File id is " + FileId.get(FileNo) + "  File path is " + strServerPath + "  Synch Path is " + strSynchPath + "FileName " + strFileName);
                        IO.printStackTrace();

                    } catch (Exception e) {

                        Log.e("File Exception","  File id is " + FileId.get(FileNo) + "  File path is " + strServerPath + "  Synch Path is " + strSynchPath + "FileName " + strFileName);
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);

                    if (File_Completed == 100) {
                        Log.d("File Downlode Status for " + SynchPath.get(FileNo), "" + File_Completed + "%");
                    } 
                }
            }

            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
        }

回答1:


The bunch is your problem. You exceeded executor pool limits (blind guess is you got more than 10 running at the same time). Use IntentServer do your download in sequence, or use DownloadManager if you target API that already supports it.



来源:https://stackoverflow.com/questions/17326931/download-bunch-of-files-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!