簡體   English   中英

如何衡量從URL下載的PDF的大小

[英]How to measure the size of a PDF downloaded from a URL

我正在從URL下載PDF並將其保存到本地驅動器。

下載代碼工作正常,問題是當我嘗試測量文件的大小時,它總是聲稱它是52個字節。 我很困惑......你能不能回顧我的代碼並告訴我,如果我錯過了什么?

try {
                 link = new URL("http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_" + entry[0] + "_2015.pdf");
                 // http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_BT_2015.pdf

                 InputStream in = new BufferedInputStream(link.openStream());
                 ByteArrayOutputStream out = new ByteArrayOutputStream();

                 byte[] buf = new byte[1024];
                 int n = 0;
                 while (-1!=(n=in.read(buf)))
                 {
                    out.write(buf, 0, n);
                 }
                 out.close();
                 in.close();
                 byte[] response = out.toByteArray();

                 FileOutputStream fos = new FileOutputStream(fileName);
                 fos.write(response);
                 fos.close();

            } catch (Exception e) {
                System.out.println("Couldn't retrieve : " + entry[1] + " " + year);
            }

            int bytes = fileName.length();
            System.out.println(bytes);

這里。 試試吧。

URL url = new URL("http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_" + entry[0] + "_2015.pdf");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.76");
int size = conn.getContentLength();

        if (size < 0) {
            System.out.println("File not found");
        } else {
            System.out.println("File size in Bytes: " + size);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM