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

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

shareimprove this answer

edited Jul 6 ‘13 at 2:01

Community

1

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

add a comment


up vote6down vote

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


shareimprove this answer

answered May 4 ‘11 at 10:31

Jigar Joshi

148k20236309

 
add a comment

up vote5down vote

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

shareimprove this answer

edited Apr 15 ‘14 at 21:22

answered Apr 15 ‘14 at 21:11

G.F.

13828

 
add a comment

up vote3down vote

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.


shareimprove this answer

answered Sep 9 ‘15 at 6:15

Alex

4,95511528

 
add a comment

up vote0down vote

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

时间: 2024-10-18 18:39:03

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

java web 从服务器上下载图片资料

package com.Action; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpUtils {

How a web page loads

The major web browsers load web pages in basically the same way. This process is known as parsing and is described by the HTML5 specification. A high-level understanding of this process is critical to writing web pages that load efficiently. Parsing

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

续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(Analysis)这部分了. LoadRunner结果分析图表功能中最重要图表分析之一,就是网页诊断细分图,在Controller场景设计运行之前,需要在菜单栏中设置启用网页诊断功能(诊断-网页诊断-启动),如图: 网页细分图,是显示每个页面及其组件的相关下载时间和大小,主要用来评估页面内容是否影响事务

解读Web Page Diagnostics网页细分图

http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostics (以下简称WPD),这是LR Analysis中非常重要的一块,搞清楚这部分的内容会让你少走很多弯路,很多环境问题都可以通过它来定位,比如客户端,网络.通过它可以你可以比较好的来定位是环境的问题还是应用本身的问题,当然更重要的是Web页面本身的问题. Web Page Diagnostics:页面诊断图,也叫页面分解总图 “页面分解”显示某一具体事务

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

Discuz开源论坛网页细分图结果分析(Web Page Diagnostics) 续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(Analysis)这部分了. LoadRunner结果分析图表功能中最重要图表分析之一,就是网页诊断细分图,在Controller场景设计运行之前,需要在菜单栏中设置启用网页诊断功能(诊断-网页诊断-启动),如图: 网页

Web Page Diagnostics网页细分图解读

Web Page Diagnostics (以下简称WPD),这是LR Analysis中非常重要的一块,搞清楚这部分的内容会让你少走很多弯路,很多环境问题都可以通过它来定位,比如客户端,网络.通过它可以你可以比较好的来定位是环境的问题还是应用本身的问题,当然更重要的是Web页面本身的问题. Web Page Diagnostics:页面诊断图,也叫页面分解总图 “页面分解”显示某一具体事务在测试过程的响应情况,进而分析相关的事务运行是否正常. 此视图下面的文件结构大体上与scripts中act

How to save content/text of a web page by forcing save-as option

I would like to know what would be the best way to save contents from a web page. I mean to force save-as option by clicking a link or button. I often found that javascript would not be an option because it's only working for IE, so I thought that it

loadrunner学习---网页细分图web page diagnostics

在场景中打开Diagnostics菜单下的Web Page Diagnostics功能,就能得到网页分析组图.通过这个图,可以对事务的组成进行抽丝剥茧的分析,得到组成这个页面的每一个请求时间分析,进一步了解响应时间中有关网络和服务器处理时间的分配关系.通过这个功能,可以实现对网站的前端性能分析,明确系统响应时间较长时由服务器端(后端)处理能力不足还是短连接到服务器的网络(前端)消耗导致的. 1.Web Page Diagnostics(网页分析) 添加该图先会得到整个场景运行后虚拟用户访问的Pa

调试android chrome web page简明备忘

必备工具 adb tools.android chrome 先开启手机调试模式 adb forward tcp:9919 localabstract:chromedevtoolsremote 成功会提示 * daemon not running. starting it now on port 5037 * * daemon started successfully * 不成功的话用这个命令再试 adb kill-server 接着打开下面的链接,这里的9919是上面adb命令中指定的 http