使用Apache HttpClient下载PDF

时间:2014-08-17 00:58:56

标签: java apache httpclient

我正在编写一个程序,使用HttpClient从我的ISP下载所有月度报表。我可以登录该站点,访问页面和下载页面,但我无法下载我的PDF语句。它只是下载一些HTML。我开始使用this问题的答案。这是我尝试下载PDF的方法:

public void downloadPdf() throws ClientProtocolException, IOException  {
HttpGet httpget = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
    HttpResponse response = client.execute(httpget);

    System.out.println("Download response: " + response.getStatusLine());

    HttpEntity entity = response.getEntity();

    InputStream inputStream = null;
    OutputStream outputStream = null;

    if (entity != null) {
        long len = entity.getContentLength();
        inputStream = entity.getContent();

        outputStream = new FileOutputStream(new File("/home/bkurczynski/Desktop/statement.pdf"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        outputStream.close();
    }
}

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

HttpClient httpClient = HttpClientBuilder.create().build();
    try {
        HttpGet request = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();

        InputStream is = entity.getContent();
        String filePath = "hellow.txt";
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        int inByte;
        while ((inByte = is.read()) != -1)
            fos.write(inByte);
        is.close();
        fos.close();

    } catch (Exception ex) {

    }
相关问题