2014-03-28 79 views
4

我想用jsoup下載大型pdf文件。我試圖改變超時和maxBodySize,但我可以下載的最大文件大約是11MB。我認爲如果有什麼辦法可以做緩衝之類的事情。以下是我的代碼。用jsoup下載大型pdf

public class Download extends Activity { 

static public String nextPage; 
static public Response file; 
static public Connection.Response res; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Bundle b = new Bundle(); 
    b = getIntent().getExtras(); 
    nextPage = b.getString("key"); 
    new Login().execute(); 
    finish(); 
} 

private class Login extends AsyncTask<Void, Void, Void> { 

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

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      res = Jsoup.connect("http://www.eclass.teikal.gr/eclass2/") 
        .ignoreContentType(true).userAgent("Mozilla/5.0") 
        .execute(); 

      SharedPreferences pref = getSharedPreferences(
        MainActivity.PREFS_NAME, MODE_PRIVATE); 
      String username1 = pref.getString(MainActivity.PREF_USERNAME, 
        null); 
      String password1 = pref.getString(MainActivity.PREF_PASSWORD, 
        null); 
      file = (Response) Jsoup 
        .connect("http://www.eclass.teikal.gr/eclass2/") 
        .ignoreContentType(true).userAgent("Mozilla/5.0") 
        .maxBodySize(1024*1024*10*2) 
        .timeout(70000*10) 
        .cookies(res.cookies()).data("uname", username1) 
        .data("pass", password1).data("next", nextPage) 
        .data("submit", "").method(Method.POST).execute(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

    @Override 
    protected void onPostExecute(Void result) { 

     String PATH = Environment.getExternalStorageDirectory() 
       + "/download/"; 
     String name = "eclassTest.pdf"; 
     FileOutputStream out; 
     try { 

      int len = file.bodyAsBytes().length; 
      out = new FileOutputStream(new File(PATH + name)); 
      out.write(file.bodyAsBytes(),0,len); 
      out.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
    } 
} 

我希望有人能幫助我!

+0

您是否嘗試設置'''.maxBodySize(0)'''和'''.timeout(0)'''以設置這些參數沒有限制? – luksch

+0

@luksch感謝您的回覆,我已經嘗試過,但我可以下載的最大文件是11-12 MB。 –

+0

您正在將此作爲Android活動運行嗎?你確定這是jsoup的問題,而不僅僅是當操作系統試圖釋放內存時被殺的活動? – asp

回答

1

我認爲,最好通過的HttpConnection下載任何二進制文件:

InputStream input = null; 
    OutputStream output = null; 
    HttpURLConnection connection = null; 
    try { 
     URL url = new URL("http://example.com/file.pdf"); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 

     // expect HTTP 200 OK, so we don't mistakenly save error report 
     // instead of the file 
     if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
      return "Server returned HTTP " + connection.getResponseCode() 
        + " " + connection.getResponseMessage(); 
     } 

     // this will be useful to display download percentage 
     // might be -1: server did not report the length 
     int fileLength = connection.getContentLength(); 

     // download the file 
     input = connection.getInputStream(); 
     output = new FileOutputStream("/sdcard/file_name.extension"); 

     byte data[] = new byte[4096]; 
     int count; 
     while ((count = input.read(data)) != -1) { 
      output.write(data, 0, count); 
     } 
    } catch (Exception e) { 
     return e.toString(); 
    } finally { 
     try { 
      if (output != null) 
       output.close(); 
      if (input != null) 
       input.close(); 
     } catch (IOException ignored) { 
     } 

     if (connection != null) 
      connection.disconnect(); 
    } 

Jsoup是解析和加載HTML網頁,而不是二進制文件。

+0

我沒有問題使用HTTPConnection,但我不知道如何登錄(使用Cookie),而且您不寫。如果你知道如何做到這一點,寫,我會嘗試。 謝謝你的迴應! –

+0

考慮如何使用HttpURLConnection使用cookie,而不是被動地等待回答者提供更多的幫助。如果你自己找不到答案,請提出另一個問題!這一次,我搜索了你,並[這個答案](https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-請求/ 2793153#2793153)可能會有所幫助。 –