从URL下载Zip文件

时间:2014-10-12 16:08:17

标签: android

我正在尝试使用此代码从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
非常感谢

1 个答案:

答案 0 :(得分:0)

错误是文件名包含URL中不接受的空格。简而言之,解决方法是从文件名中删除空格。