0

我無法找到解決文件下載問題的解決方案。廣泛搜索後文件下載掛起

以下腳本旨在通過 WebViewClient從遠程(Hotmail)服務器下載csv文件。

登錄過程是通過他們的標準網站完成的,但我想使用以下下載類捕獲下載的csv文件並存儲在自定義位置。

它正常工作與直接鏈接到了如某個文件的URL,site.com/file.pdf,但不會上處理的URL,例如工作site.com/downloadFile.php?n=xxxx哪裏它只是掛起直到連接由遠程服務器

private class DownloadFile extends AsyncTask<String, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     mProgressDialog.setProgress(progress[0]); 
    } 

    @Override 
    protected void onPostExecute (String result){ 

    super.onPostExecute(result); 

    mProgressDialog.dismiss(); 
    mProgressDialog = null; 
    } 


    @Override 
    protected String doInBackground(String... sUrl) { 

     try { 

      Log.i("File download", "Started from :"+sUrl[0]); 

      URL url = new URL(sUrl[0]); 
      //URLConnection connection = url.openConnection(); 
      File myDir = new File(Environment.getExternalStorageDirectory() + "/myDir"); 

      // create the directory if it doesnt exist 
      if (!myDir.exists()) myDir.mkdirs();    

      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 


      //Follow redirects so as some sites redirect to the file location 
      connection.setInstanceFollowRedirects(true);   
      connection.setDoOutput(true);   

      connection.connect(); 

      File outputFile  = new File(myDir, "hotmail_contacts.csv"); 

      // this will be useful so that you can show a typical 0-100% 
      // progress bar 
      int fileLength  = connection.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(url.openStream());     
      OutputStream output = new FileOutputStream(outputFile); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 

      while ((count = input.read(data)) != -1) { 

       total += count; 
       // publishing the progress.... 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 


      connection.disconnect(); 
      output.flush(); 
      output.close(); 
      input.close(); 

      Log.i("File download", "complete"); 

     } catch (Exception e) { 

      Log.e("File download", "error: " + e.getMessage()); 

     } 
     return null; 
    } 
} 

上面的AsyncTask在onDownloadStart(....)方法調用如下復位:

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long length) { 

     Log.i("File download", "URL:" + url 
       + " UserAgent:" + userAgent 
       + "ContentDisposition:" + contentDisposition 
       + "Mime:"+ mimeType + "Length:" + Long.toString(length)); 



     // instantiate it within the onCreate method 
     mProgressDialog = new ProgressDialog(Email_import.this); 
     mProgressDialog.setMessage("File download"); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

     // start a new download 
     DownloadFile downloadFile = new DownloadFile(); 
     downloadFile.execute(url); 

    }// end onCreate 

所有相關權限都在清單中,如寫入外部存儲,互聯網和讀取網絡狀態。

我在這裏錯過了什麼嗎? 任何幫助,將不勝感激

回答

0

看上市[這裏]另一篇文章[1]

[1]後:Download a file from webview to a custom folder我做了一些調整,我的代碼來調用URL作爲HttpPost請求使用HttpClient對象如下所示,它看起來工作得很漂亮:

   // Create client and set our specific user-agent string 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost request = new HttpPost(sUrl[0]);    
      request.setHeader("User-Agent", sUrl[1]);    

      HttpResponse response = client.execute(request); 

      Log.i("File download", "Started from :" + sUrl[0]); 

      File myDir = new File(Environment.getExternalStorageDirectory() + "/myDir"); 

      // create the directory if it doesnt exist 
      if (!myDir.exists()) myDir.mkdirs(); 

      File outputFile = new File(myDir, "hotmail_contacts.csv"); 

      InputStream input = response.getEntity().getContent(); 
相關問題