URL objUrl = new URL(url);
String tDir = System.getProperty("java.io.tmpdir");
String path = tDir + "tmp" + ".jpg";
File file =new File(path);
file.deleteOnExit();
FileUtils.copyURLToFile(objUrl, file);
1、fileutils使用的是apache类型common-io-x.jar
2、google guava库也能完成同样操作
Resources.asByteSource(url).copyTo(Files.asByteSink(file));
3、当然,你也可以通过以下方式去解决这个问题
URL url = new URL(url);
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("downloaded.jpg"));
byte[] buf = new byte[512];
while (true) {
int len = in.read(buf);
if (len == -1) {
break;
}
fos.write(buf, 0, len);
}
in.close();
fos.flush();
fos.close();
4、还可以通过imageio去处理
URL url = new URL(url);
BufferedImage img = ImageIO.read(url);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);