簡體   English   中英

從URL下載Zip文件

[英]Download Zip file from URL

我正在嘗試使用此代碼從url下載zip文件

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

    String downDir = "";

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

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

        int count;
        try {

            URL url = new URL(f_url[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            int lenghtOfFile = connection.getContentLength();

            BufferedInputStream input = new BufferedInputStream(
                    url.openStream());
            downDir = Environment.getExternalStorageDirectory().toString()
                    + "/down/";

            if (new File(downDir).isDirectory()) {
                // Output stream
                OutputStream output = new FileOutputStream(downDir
                        + booknameOnly + ".jar");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = ((InputStream) input).read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress(""
                            + (int) ((total * 100) / lenghtOfFile));

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

                // flushing output
                output.flush();

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

            } else {

                boolean check = new File(downDir).mkdir();
                // Output stream
                OutputStream output = new FileOutputStream(downDir
                        + booknameOnly + ".jar");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress(""
                            + (int) ((total * 100) / lenghtOfFile));

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

                // flushing output
                output.flush();

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

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

        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
     * **/
    @SuppressWarnings("deprecation")
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after the file was downloaded
        dismissDialog(progress_bar_type);


    }

}

但是當我嘗試下載時,我得到了fileNotFoundException錯誤,盡管當我直接將文件url粘貼到Web瀏覽器時,我可以下載該文件,這意味着該文件已經存在,這是我的logCat:

     10-12 18:23:50.092: D/ActivityThread(13179): setTargetHeapUtilization:0.25
     10-12 18:23:50.092: D/ActivityThread(13179): setTargetHeapIdealFree:8388608
     10-12 18:23:50.092: D/ActivityThread(13179): setTargetHeapConcurrentStart:2097152
     10-12 18:23:50.512: D/AbsListView(13179): Get MotionRecognitionManager
     10-12 18:23:50.802: D/dalvikvm(13179): GC_FOR_ALLOC freed 167K, 13% free 13244K/15171K, paused 28ms, total 28ms
     10-12 18:23:51.352: D/libEGL(13179): loaded /system/lib/egl/libEGL_adreno200.so
     10-12 18:23:51.362: D/libEGL(13179): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
     10-12 18:23:51.372: D/libEGL(13179): loaded /system/lib/egl/libGLESv2_adreno200.so
     10-12 18:23:51.382: I/Adreno200-EGL(13179): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)
    10-12 18:23:51.382: I/Adreno200-EGL(13179): Build Date: 02/25/13 Mon
    10-12 18:23:51.382: I/Adreno200-EGL(13179): Local Branch: 
    10-12 18:23:51.382: I/Adreno200-EGL(13179): Remote Branch: quic/jb_rel_2.0.3
    10-12 18:23:51.382: I/Adreno200-EGL(13179): Local Patches: NONE
    10-12 18:23:51.382: I/Adreno200-EGL(13179): Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 +  NOTHING
    10-12 18:23:51.592: D/OpenGLRenderer(13179): Enabling debug mode 0
    10-12 18:23:51.752: I/Choreographer(13179): Skipped 34 frames!  The application may be doing too much work on its main thread.
    10-12 18:23:58.292: D/AbsListView(13179): Get MotionRecognitionManager
    10-12 18:23:59.872: D/AbsListView(13179): Get MotionRecognitionManager
    10-12 18:24:01.142: E/SpannableStringBuilder(13179): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    10-12 18:24:01.142: E/SpannableStringBuilder(13179): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    10-12 18:24:13.492: D/dalvikvm(13179): GC_CONCURRENT freed 317K, 9% free 19165K/20871K, paused 15ms+20ms, total 98ms
    10-12 18:24:13.582: I/Choreographer(13179): Skipped 457 frames!  The application may be doing too much work on its main thread.
   10-12 18:24:13.592: I/System.out(13179): Connecting to http://localhost/sent/file.zip
   10-12 18:24:13.722: E/Error:(13179): http://localhost/sent/file.zip

非常感謝

錯誤是文件名包含URL中不接受的空格。簡而言之,解決方案是從文件名中刪除空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM