Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何测量从URL下载的PDF的大小_Java_Filesize - Fatal编程技术网

Java 如何测量从URL下载的PDF的大小

Java 如何测量从URL下载的PDF的大小,java,filesize,Java,Filesize,我正在从URL下载PDF并将其保存到本地驱动器 下载代码工作得很好,问题是当我试图测量文件的大小时,它总是声称它是52字节。我很困惑。。。你能检查一下我的代码,告诉我是否遗漏了什么吗 try { link = new URL("http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_" + entry[0] + "_2015.pdf"); // http:/

我正在从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);
        }
URL=新URL(“http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_“+条目[0]+”_2015.pdf”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.addRequestProperty(“用户代理”、“Mozilla/4.76”);
int size=conn.getContentLength();
如果(尺寸<0){
System.out.println(“未找到文件”);
}否则{
System.out.println(“以字节为单位的文件大小:“+size”);
}

这里。只要试试这个

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);
        }
URL=新URL(“http://www.annualreports.co.uk/HostedData/AnnualReports/PDF/LSE_“+条目[0]+”_2015.pdf”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.addRequestProperty(“用户代理”、“Mozilla/4.76”);
int size=conn.getContentLength();
如果(尺寸<0){
System.out.println(“未找到文件”);
}否则{
System.out.println(“以字节为单位的文件大小:“+size”);
}

您似乎正在测量
文件名的长度,这可能是文件名。您正在打印文件名的长度。哼!啊,对不起,伙计们,这太傻了。我是新手!为业余的问题道歉。。。那么,在这个代码中是否有任何对象会测量文件而不是文件名?您似乎在测量
文件名
的长度,这可能是文件名。您正在打印文件名的长度。哼!啊,对不起,伙计们,这太傻了。我是新手!为业余的问题道歉。。。那么,在这段代码中是否有任何对象会测量文件而不是文件名?很高兴它能为您工作。那么,你可以接受答案了很高兴它对你有用。那么,你可以接受答案了