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. oAuth 使得第三方无需使用用户的用户名与密码就可以申请获得该用户资源的授权

    OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用户的用户名与密码就可 ...

  2. centos7下编译安装nginx1.10

    1.下载pcre 下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 解压到/usr/local/pcre8.3.9 2.下载ope ...

  3. Redis字符串类型相关操作命令

    string是redis最基本的类型,可以包括任何类型数据,如jpg图片或者序列化对象. 单个value最大上限是1G字节 如果只使用string类型,redis就可以被看做具有持久化特性的memca ...

  4. WI-FI: connection through CLI

    First, ensure that the computer/sbc has WI-FI parts. install wpa_supplicant, edit /etc/wpa_supplican ...

  5. 小团队git开发模式

    实验室要使用Git进行代码管理,但是git非常复杂,各种开发模式也是层出不穷.作为新手的偶们很是发囧啊!!网上搜了一下,发现很多并不适合我们小团队运作(它本身就是为Linux内核管理而开发的分布式代码 ...

  6. HDU猜数字

    G - 猜数字 Time Limit:10000MS       Memory Limit:32768KB       64bit IO Format:%I64d & %I64u Descri ...

  7. 微信小程序Tabbar文字在真机不显示

    按照官方文档在json中定义好了Tabbar后,在模拟器上显示没问题,而在真机上不显示Tabar文字. 让我很苦笑不得的原因是: 在app.json定义Tabbar文字选中态和非选中态颜色时我用了英文 ...

  8. thinkphp 匹配qq 表情

    <?php// 匹配qq表情    function replace_phiz($content){        preg_match_all('/\[.*?\]/is',$content,$ ...

  9. WEB前端组件思想【分页】

    DEMO1: 很早就想写一些功能性的组件,无奈技术有限一点一点的边工作,边学. 近日工作中用到分页功能,当然由于加快业务进度,第一选择肯定是选择插件,但是实用性来说,还是有那么一点不适合.毕竟插件是通 ...

  10. Xshell连接服务器密钥改成密码登录

    密钥登陆 直接点本地shell打开之后直接ssh ip连接 需要centos用户登录,选择机器密钥. 登录后 sudo su - root 修改root密码 passwd root 修改文件 vi / ...