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();
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)

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();
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:

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.

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.

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(readByte);
}
}
}
}

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. 激活JetBrains PhpStorm 2016.3.2和JetBrains WebStorm 2016.3.2

    1.打开 phpstorm 2.在激活界面选择license server 在线激活方式 输入:http://idea.imsxm.com/ 3.激活成功,打开使用

  2. es6 语法

    1.定义变量 使用let,const.let可以定义变量,相当于var,定义的变量只在局部范围内使用.const用来定义常量. 2.解构 自动解析数组或者对象中的值,比如一个函数要返回多个值,通常的做 ...

  3. hdu1664 bfs+余数判重

    input n 不超过50个例子,n==0结束输入 Sample Input 7 15 16 101 0 output 最少个不同数字的n的倍数的x,若不同数字个数一样,输出最小的x Sample O ...

  4. centos解压bz2文件出错

    出现的问题: 用tar 解压 tar.bz2文件出错 debian:/usr/src# tar jxf linux-2.6.26.tar.bz2tar: bzip2: Cannot exec: No ...

  5. try{}catch{}finally{}的手记

    try{ System.out.println("执行try"); int = 6 / 0; return 1; }catch(Exception e){ System.out.p ...

  6. TCP三次握手中,为什么需要第三次握手?

    为什么客户端A还要发送一次确认呢?(为什么需要第三次握手) 这主要是为了防止已失效的连接请求报文段突然又传送到了B(服务器端),因而产生错误. 所谓"已失效的连接请求报文段"是这样 ...

  7. C1FlexGrid小结(转自http://www.cnblogs.com/C1SupportTeam/archive/2012/12/11/2812316.html)

    C1FlexGrid控件来对一个表格格式中的数据进行显示,编辑,组和总结.该表格可以绑定到一个数据源,它可以对自己的数据进行管理. C1FlexGrid控件有一个包含以下元素的丰富的对象模型: 以下的 ...

  8. Microsoft Visual Studio 2012正式版官方下载

    Microsoft Visual Studio 2012正式版官方下载 首先声明,看到园子里还没有类似的新闻,所以斗胆发首页,如有不妥之处,请移除并谅解. 虽然之前已经又用到泄露的MSDN正式版,但今 ...

  9. 【ORACLE】“System.Exception: System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本。”解决办法

    我的电脑是win10.64位.问题如题,在网上找了很多办法都没搞好,如下: 1.给oracle目录设置“Authenticated Users”用户的“读取/写入”权限 2.IIS网站物理路径凭据添加 ...

  10. 修改VirtualBox虚拟机默认存储路径及虚拟机迁移方法

    修改默认安装路径 在安装完虚拟机以后发现我的虚拟的磁盘文件是放在C盘的,就想着有没有办法修改默认存储路径.后来发现确实可以修改,修改虚拟机方法如下:"管理"--->" ...