2014-10-12 4 views
0

이 코드다운로드 우편 URL

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


    } 

} 

를 사용하여 URL에서 zip 파일을 다운로드하려고에서 파일하지만 난 파일의 URL을 붙여 넣을 때 내가 다운로드 할 수있는 웹 브라우저에 직접 비록 내가 FileNotFoundException이 오류가 다운로드 할 때 파일이 이미 존재한다는 것을 의미 파일 내 로그 캣입니다 :

 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 

많은 감사

+0

logcat 게시하시기 바랍니다. – Abdellah

+0

게시 된 logcat에 파일을 찾을 수없는 예외가없는 이유는 무엇입니까? 'http : // localhost/sent/file.zip'. 그게 당신이 사용하고있는 URL입니까? 그것은 결코 작동하지 않을 것입니다. Android 앱이 어디에서 실행되는지 알려주세요. 에뮬레이터/장치에서? – greenapps

+0

@greenapps 파일을 찾을 수 없습니다. 예외가 코드 내의 catch 블록에 나타납니다. 장치에서 응용 프로그램을 실행 중이고 IIs 로컬 서버를 사용하며 원격 서버에 파일을 게시했는데 두 경우 모두 파일을 찾을 수없는 예외가 발생했습니다. – Ayman

답변

0

오류는 파일 이름이 동의하지 않는 공백이 포함되어 있다는 것입니다 즉, 해결책은 파일 이름에서 공백을 제거하는 것입니다.