【发布时间】:2017-02-21 18:29:46
【问题描述】:
我正在尝试下载托管方提供的一些图片。这是我使用的方法:
public static void downloadImage(String imageLink, File f) throws IOException
{
URL url = new URL(imageLink);
byte[] buffer = new byte[1024];
BufferedInputStream in = new BufferedInputStream(url.openStream(), buffer.length);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f), buffer.length);
while (in.read(buffer) > 0)
out.write(buffer);
out.flush();
out.close();
in.close();
}
但是,文件太大了。在我看来,80x60 jpg 的 5MB 太大了。
这可能是什么原因?
【问题讨论】:
-
帮自己一个忙,并为此使用库。我最喜欢的流复制是 Apache Commons IOUtils (commons.apache.org/proper/commons-io/javadocs/api-release/org/…)
-
不客气;感谢您的快速接受。
-
附注;我同意托马斯的观点:除非这是一些“教育练习”——你最好使用一些图书馆来做到这一点。
-
@GhostCat 现在我还在接受教育,所以我想在不使用库的情况下尽可能多地了解 java 的各个方面,但我也会牢记这个建议。
标签: java url filestream