下载一堆从服务器的文件 [英] Download bunch of files from server

查看:128
本文介绍了下载一堆从服务器的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PHP服务器下载一堆文件,并将其存储在一个SD卡。但是,当我下载的文件,我收到以下错误:没有文件从服务器下载是** 500 ** -600后下载一些文件** 250 - 260 ** 。它提出了一个错误。

错误

  8月7日至10日:40:46.228:E /文件错误(1837):文件编号为287的文件路径是http://192.168.1.21/SOCH/upload/chapter_36/ 5.9.pdf同步路径是/ mnt / SD卡/ SOCH / 36 /
    八月7日至10日:40:46.268:W / System.err的(1837):java.util.concurrent.RejectedExecutionException:任务android.os.AsyncTask$3@434ef2c0从java.util.concurrent.ThreadPoolExecutor@4364f0d0 [拒绝运行,池大小= 128,活动线程= 128,排队任务= 128,完成的任务= 1]
    八月7日至10日:40:46.268:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor中的$ AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
    八月7日至10日:40:46.268:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
    八月7日至10日:40:46.268:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
    八月7日至10日:40:46.268:W / System.err的(1837):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589)
    八月7日至10日:40:46.268:W / System.err的(1837):在com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.app.ActivityThread.access $ 1900年(ActivityThread.java:130)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.os.Handler.dispatchMessage(Handler.java:99)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.os.Looper.loop(Looper.java:137)
    八月7日至10日:40:46.278:W / System.err的(1837):在android.app.ActivityThread.main(ActivityThread.java:4745)
    八月7日至10日:40:46.278:W / System.err的(1837):在java.lang.reflect.Method.invokeNative(本机方法)
    八月7日至10日:40:46.278:W / System.err的(1837):在java.lang.reflect.Method.invoke(Method.java:511)
    八月7日至10日:40:46.278:W / System.err的(1837):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
    八月7日至10日:40:46.278:W / System.err的(1837):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    八月7日至10日:40:46.278:W / System.err的(1837):在dalvik.system.NativeStart.main(本机方法)
    八月7日至10日:40:46.278:E /文件错误(1837):文件编号为288的文件路径是http://192.168.1.21/SOCH/upload/chapter_37/6.1.pdf同步路径是/ mnt / SD卡/ SOCH / 37 /
    八月7日至10日:40:46.308:W / System.err的(1837):java.util.concurrent.RejectedExecutionException:任务android.os.AsyncTask$3@43605840从java.util.concurrent.ThreadPoolExecutor@4364f0d0 [拒绝运行,池大小= 128,活动线程= 128,排队任务= 128,完成的任务= 1]
    八月7日至10日:40:46.308:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor中的$ AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
    八月7日至10日:40:46.308:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
    八月7日至10日:40:46.308:W / System.err的(1837):在java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:589)
    八月7日至10日:40:46.308:W / System.err的(1837):在com.soch.webservice.FileTransferService.onStartCommand(FileTransferService.java:146)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.app.ActivityThread.access $ 1900年(ActivityThread.java:130)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.os.Handler.dispatchMessage(Handler.java:99)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.os.Looper.loop(Looper.java:137)
    八月7日至10日:40:46.308:W / System.err的(1837):在android.app.ActivityThread.main(ActivityThread.java:4745)
    八月7日至10日:40:46.308:W / System.err的(1837):在java.lang.reflect.Method.invokeNative(本机方法)
    八月7日至10日:40:46.308:W / System.err的(1837):在java.lang.reflect.Method.invoke(Method.java:511)
    八月7日至10日:40:46.308:W / System.err的(1837):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
    八月7日至10日:40:46.308:W / System.err的(1837):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    八月7日至10日:40:46.308:W / System.err的(1837):在dalvik.system.NativeStart.main(本机方法)

任何人可以帮助我解决这个问题?我使用asynchtask做下载。

我的code是

包com.webservice;

 公共类FileTransferService延伸服务{        私有静态最终诠释CORE_POOL_SIZE = 1;
    私有静态最终诠释MAXIMUM_POOL_SIZE = 128;
    私有静态最终诠释KEEP_ALIVE = 128;    私有静态最后的BlockingQueue<&的Runnable GT; sPoolWorkQueue =新的LinkedBlockingQueue<&的Runnable GT;(MAXIMUM_POOL_SIZE);    私有静态最后的ThreadFactory sThreadFactory =新的ThreadFactory(){
            私人最终的AtomicInteger mCount =新的AtomicInteger(1);            公共主题newThread(Runnable的R){
                返回新线程(R的AsyncTask#+ mCount.getAndIncrement());
            }
        };
    公共静态最后的遗嘱执行人THREAD_POOL_EXECUTOR =新的ThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE,KEEP_ALIVE,TimeUnit.SECONDS,sPoolWorkQueue,sThreadFactory);            INT FileNo在= 0;            ArrayList的<串GT; FILEID =新的ArrayList<串GT;();
            ArrayList的<串GT; SERVERPATH =新的ArrayList<串GT;();
            ArrayList的<串GT; SynchPath =新的ArrayList<串GT;();
            ArrayList的<串GT; FileNameList =新的ArrayList<串GT;();            串FileTransferPath =,SDcardPath =;            @覆盖
            公共无效的onCreate(){
                super.onCreate();
                Log.d(服务创建,成功);                FileId.clear();
                ServerPath.clear();
                SynchPath.clear();
                FileNameList.clear();                。SDcardPath = Environment.getExternalStorageDirectory()的getPath()+/;                //获取文件传输链路从SD卡
                ......                //获得一个网址从DB                .....            } //结束的onCreate            @覆盖
            公众诠释onStartCommand(意向意图,诠释标志诠释startId){                Log.d(服务启动,成功);                Log.d(档案大小,+ FileId.size());
                        如果(FileId.size()大于0){                    ContentValues​​ mContentValues​​UpdatefileStatus =新ContentValues​​();
                    mContentValues​​UpdatefileStatus.put(状态,1);                        而(FileNo在< FileId.size()){                        dbh.updateRecord(\"Mst_FileTransfer\",mContentValuesUpdatefileStatus,\"FileTransferId\",FileId.get(FileNo));                        尝试{                            新DownloadFileFromURL()executeOnExecutor(THREAD_POOL_EXECUTOR,Integer.toString(FileNo在));
                            //新DownloadFileFromURL()执行(Integer.toString(FileNo在))。                        }赶上(例外五){
                            e.printStackTrace();
                            Log.e(文件错误,文件ID为+ FileId.get(FileNo在)+文件路径为+ ServerPath.get(FileNo在)+同步路径是+ SynchPath.get(FileNo在));
                        }                        FileNo在+ = 1;
                    }                }                Log.d(服务完成,成功);
                stopSelf();                返回super.onStartCommand(意向,旗帜,startId);
            }            类DownloadFileFromURL扩展的AsyncTask<字符串,字符串,字符串> {                诠释计数= 0;
                INT File_Completed = 0;
                INT FileNo在;
                串strServerPath,strSynchPath,strFileName;                @覆盖
                在preExecute保护无效(){
                    super.on preExecute();                }                @覆盖
                保护同步字符串doInBackground(字符串... PARAMS){                    FileNo在=的Integer.parseInt(PARAMS [0]);                    //获取文件路径
                    strServerPath = ServerPath.get(FileNo在).replaceAll(,%20);
                    strSynchPath = SynchPath.get(FileNo在);
                    strFileName = FileNameList.get(FileNo在);                    URLConnection的mConnection = NULL;                    Log.d(文件,strSynchPath + strFileName);                    文件SynchPath =新的文件(strSynchPath);
                    如果(!SynchPath.exists()){
                        SynchPath.mkdirs();
                    }                    尝试{                        网址URL =新的URL(strServerPath);
                        mConnection = url.openConnection();
                        mConnection.connect();                        //获取文件的长度
                        INT lenghtOfFile = mConnection.getContentLength();                        //输出流写入文件
                        文件OUTPUTFILE =新的文件(SynchPath,strFileName);
                        FileOutputStream中mFileOutputStream =新的FileOutputStream(
                                输出文件);                        InputStream的mInputStream = url.openStream();
                        字节的数据[] =新的字节[1024];                        长File_Completed_Size = 0;                        而((计数= mInputStream.read(数据))!= -1){                            File_Completed_Size + =计数;
                            File_Completed =(int)的((File_Completed_Size * 100)/ lenghtOfFile);                            //将数据写入到文件
                            mFileOutputStream.write(数据,0,计数);
                        }                        //冲洗输出
                        mFileOutputStream.flush();                        //关闭流
                        mFileOutputStream.close();
                        mInputStream.close();                    }赶上(IOException异常IO){                        Log.e(文件例外,文件ID为+ FileId.get(FileNo在)+文件路径为+ strServerPath +同步路径是+ strSynchPath +文件名+ strFileName);
                        IO.printStackTrace();                    }赶上(例外五){                        Log.e(文件例外,文件ID为+ FileId.get(FileNo在)+文件路径为+ strServerPath +同步路径是+ strSynchPath +文件名+ strFileName);
                        e.printStackTrace();
                    }
                    返回null;
                }                @覆盖
                保护无效onPostExecute(字符串结果){
                    super.onPostExecute(结果);                    如果(File_Completed == 100){
                        Log.d(文件Downlode状态+ SynchPath.get(FileNo在),+ File_Completed +%);
                    }
                }
            }            @覆盖
            公众的IBinder onBind(意向意图){
                返回null;
            }
        }


解决方案

一束是你的问题。你超过执行人池的限制(盲的猜测是,你有超过10个在同一时间运行)。使用 IntentServer 做你的下载顺序,或使用下载管理器如果你的目标已经支持该API。

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

解决方案

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.

这篇关于下载一堆从服务器的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆