我尝试使用以下代码从url下载pdf文件。当文件大小低于8k时它工作正常,如果文件高达8k,它会下载pdf文件,但文件不可读。
代码
InputStream in = new BufferedInputStream(url.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(new File("C:\\temp\\TEST.pdf"));
fos.write(response);
fos.close();
System.out.println("Download Finished");