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. sql server的优缺点

    sql server的优点众多,让其在数据库领域独占鳌头,成为最受欢迎的数据库系统,其优缺点也自然是喜爱者们所关注的,首先了解一下它的历史: sql server是一个关系型数据库管理系统,最初是由M ...

  2. 关于pl/sql数据库下拉中选项为空的问题

    1.可能是在配置环境变量TNS_ADMIN的时候后面多了一个分号,去掉分号就可以了

  3. STL中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...

  4. xp重装系统后恢复Linux启动

    我的电脑----右键-----属性----高级----启动与恢复故障----设置编辑 [boot loader]timeout=3default=multi(0)disk(0)rdisk(0)part ...

  5. Arduino Due, Maple and Teensy3.0 的 W5200性能测试

    开源平台中以太网连接方案里W5100是众所周知的,W5200正在此领域越来越受欢迎.这个测试结果是在Arduino Due(Atmel CortexM3-84Mhz), Maple(ST Cortex ...

  6. Android 设计随便说说之简单实践(合理组合)

    上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...

  7. Python系列之 - 线程基础

    一.什么是线程 线程:顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 所以,进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才 ...

  8. awk小例子_2_数值统计脚本

    通信公司工作,经常处理各种协议接口,在统计协议接口字段内容时,需要统计字段填写的内容是否正确,和占比是多少.要是单次统计,估计会把人累死,写个脚本统计,轻松便捷. 举例:接口内容 这是一条话单,这样的 ...

  9. 机器学习入门-数值特征-连续数据离散化(进行分段标记处理) 1.hist(Dataframe格式直接画直方图)

    函数说明: 1. .hist 对于Dataframe格式的数据,我们可以使用.hist直接画出直方图 对于一些像年龄和工资一样的连续数据,我们可以对其进行分段标记处理,使得这些连续的数据变成离散化 就 ...

  10. Linux命令之lsb_release - 查看当前系统的发行版信息

    用途说明 lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution informati ...