【发布时间】:2016-11-16 20:02:52
【问题描述】:
我正在从 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);
【问题讨论】:
-
你好像在测量
fileName的长度,大概是文件名。 -
您正在打印文件 name 的长度。呵呵!
-
对不起,伙计们,那是愚蠢的。我是新来的!为业余问题道歉......那么这段代码中是否有任何对象可以测量文件而不是文件名?