Download large pdf file with jsoup

I want to download a large pdf file using jsoup. I am trying to change the timeout and maxBodySize, but the largest file I could download was about 11 MB. I think if there is a way to do something like buffering. Below is my code.

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

    }
  }
}

I hope someone can help me!

+4
source share
1 answer

I think it's better to upload any binary via 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 is designed to parse and load HTML pages, not binary files.

+1
source

Source: https://habr.com/ru/post/1533879/


All Articles