how to download image from any web page in java 下载图片

本文提供了几种使用Java从网页下载图片的方法。通过示例代码展示了如何利用标准Java库及第三方库JSoup完成图片的下载与保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/5882005/how-to-download-image-from-any-web-page-in-java

 

(throws IOException) Image image = null; try { URL url = new URL("http://www.yahoo.com/image_to_read.jpg"); image = ImageIO.read(url); } catch (IOException e) { }

See javax.imageio package for more info. That's using the AWT image. Otherwise you could do:

 URL url = new URL("http://www.yahoo.com/image_to_read.jpg"); 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();

And you may then want to save the image so do:

FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg"); fos.write(response); fos.close();
share improve this answer
 
2 
For Java7 modify the code snippet to use the try-with-resources statement, seedocs.oracle.com/javase/tutorial/essential/exceptions/… – Gonfi den Tschal Jul 7 '13 at 1:18

You are looking for a web crawler. You can use JSoup to do this, here is basic example

share improve this answer
 

It works for me)

URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/9/9c/Image-Porkeri_001.jpg"); InputStream in = new BufferedInputStream(url.openStream()); OutputStream out = new BufferedOutputStream(new FileOutputStream("Image-Porkeri_001.jpg")); for ( int i; (i = in.read()) != -1; ) { out.write(i); } in.close(); out.close();
share improve this answer
 

If you want to save the image and you know its URL you can do this:

try(InputStream in = new URL("http://example.com/image.jpg").openStream()){ Files.copy(in, Paths.get("C:/File/To/Save/To/image.jpg")); }

You will also need to handle the IOExceptions which may be thrown.

share improve this answer
 

The following code downloads an image from a direct link to the disk into the project directory. Also note that it uses try-with-resources.

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.FilenameUtils; public class ImageDownloader { public static void main(String[] arguments) throws IOException { downloadImage("https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg", new File("").getAbsolutePath()); } public static void downloadImage(String sourceUrl, String targetDirectory) throws MalformedURLException, IOException, FileNotFoundException { URL imageUrl = new URL(sourceUrl); try (InputStream imageReader = new BufferedInputStream( imageUrl.openStream()); OutputStream imageWriter = new BufferedOutputStream( new FileOutputStream(targetDirectory + File.separator + FilenameUtils.getName(sourceUrl)));) { int readByte; while ((readByte = imageReader.read()) != -1) { imageWriter.write

转载于:https://www.cnblogs.com/donaldlee2008/p/5281965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值