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

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

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

  1. URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
  2. InputStream in = new BufferedInputStream(url.openStream());
  3. ByteArrayOutputStream out = new ByteArrayOutputStream();
  4. byte[] buf = new byte[1024];
  5. int n = 0;
  6. while (-1!=(n=in.read(buf)))
  7. {
  8. out.write(buf, 0, n);
  9. }
  10. out.close();
  11. in.close();
  12. byte[] response = out.toByteArray();

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

  1. FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
  2. fos.write(response);
  3. fos.close();
answered May 4 '11 at 10:32
planetjones

7,78912844
 
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

answered May 4 '11 at 10:31
Jigar Joshi

148k20236309
 

It works for me)

  1. URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/9/9c/Image-Porkeri_001.jpg");
  2. InputStream in = new BufferedInputStream(url.openStream());
  3. OutputStream out = new BufferedOutputStream(new FileOutputStream("Image-Porkeri_001.jpg"));
  4. for ( int i; (i = in.read()) != -1; ) {
  5. out.write(i);
  6. }
  7. in.close();
  8. out.close();
answered Apr 15 '14 at 21:11
G.F.

13828
 

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

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

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

answered Sep 9 '15 at 6:15
Alex

4,95511528
 

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.

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import org.apache.commons.io.FilenameUtils;
  12. public class ImageDownloader
  13. {
  14. public static void main(String[] arguments) throws IOException
  15. {
  16. downloadImage("https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg",
  17. new File("").getAbsolutePath());
  18. }
  19. public static void downloadImage(String sourceUrl, String targetDirectory)
  20. throws MalformedURLException, IOException, FileNotFoundException
  21. {
  22. URL imageUrl = new URL(sourceUrl);
  23. try (InputStream imageReader = new BufferedInputStream(
  24. imageUrl.openStream());
  25. OutputStream imageWriter = new BufferedOutputStream(
  26. new FileOutputStream(targetDirectory + File.separator
  27. + FilenameUtils.getName(sourceUrl)));)
  28. {
  29. int readByte;
  30. while ((readByte = imageReader.read()) != -1)
  31. {
  32. imageWriter.write(readByte);
  33. }
  34. }
  35. }
  36. }

how to download image from any web page in java 下载图片的更多相关文章

  1. 解读Web Page Diagnostics网页细分图

    解读Web Page Diagnostics网页细分图 http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostic ...

  2. 网页细分图结果分析(Web Page Diagnostics)

    Discuz开源论坛网页细分图结果分析(Web Page Diagnostics) 续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场 ...

  3. Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop

    In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...

  4. LR实战之Discuz开源论坛——网页细分图结果分析(Web Page Diagnostics)

    续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(An ...

  5. [转]How WebKit Loads a Web Page

    ref:https://www.webkit.org/blog/1188/how-webkit-loads-a-web-page/ Before WebKit can render a web pag ...

  6. web page diagnostics

      1.概念说明: DNS解析时间:显示使用最近的DNS服务器将DNS名称解析为IP地址所需的时间:DNS查找度量是指示DNS解析问题或DNS服务器问题的一个很好的指示器: Connect时间:显示与 ...

  7. Loadrunner Analysis之Web Page Diagnostics

    Loadrunner Analysis之Web Page Diagnostics 分类: LoadRunner 性能测试 2012-12-31 18:47 1932人阅读 评论(2) 收藏 举报 di ...

  8. How a web page loads

    The major web browsers load web pages in basically the same way. This process is known as parsing an ...

  9. 解读Loadrunner网页细分图(Web Page Diagnostics)

    [转载的地址]https://www.cnblogs.com/littlecat15/p/9456376.html 一.启用网页细分图 首先在Controller场景设计运行之前,需要在菜单栏中设置D ...

随机推荐

  1. mac版VMware fusion

    百度网盘链接:链接: https://pan.baidu.com/s/1o8BAsrg 安装教程网上很多的,首先要下载一个window 10或其他版本的iso镜像文件,然后很好安装的.

  2. Toast用法

    应用场景:弹出提示信息 主界面: 代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sa ...

  3. (一)html之基本结构

    一:HTML基本结构 1.1 HTML文档结构 1.1.1 外层结构 <!DOCTYPE HTML> <html> </html> DOCTYPE元素用于告诉浏览器 ...

  4. C#键盘事件处理

    键盘事件是在用户按下键盘上的一个键的时候发生的,可分为两类.第一类是KeyPress事件,当按下的键表示的是一个ASCII字符的时候就会触发这类事件,可通过他的KeyPressEventArgs类型参 ...

  5. java线程condition

    子线程先执行一段代码,再主线程再执行一段代码,两个线程都循环执行50遍.用2个condition来实现,一个是子线程的condition,一个是主线程的condition,代码如下: package ...

  6. MVC运行机制

    一,第一次程序运行时 1,第一次请求的时候 会获取配置文件,然后有个应用启动事件到global.asax.2,在Global.asax文件中,网站第一次运行会创建RouteTable对象,实现URL到 ...

  7. php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message

    exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...

  8. javascript 限制字符串字数换行 带BUG

    function chang(str ,len) { function lenStat(str) { function isChinese(str) { //判断是不是中文 var reCh = /[ ...

  9. ElasticSearch基础(4)-索引

    一.ES API常用规则 ES支持以Http协议的方式提供REST服务,以JSON格式发送请求返回响应. ES提供了大量的不管的数据操作,运维管理API,大量的api 这海量的api有一些通用的功能特 ...

  10. 如何安装VM Tool软件包

    在linux下,我们想把原windows操作系统下的一些文件拷入到新linux系统中,在windows下对文件(夹)进行复制,在linux下无法进行粘贴,何故?这是因为新装的linux操作系统未安装V ...