首页 > 代码库 > Java 使用URL从网络读取图片

Java 使用URL从网络读取图片

import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.URL;public class DownLoadImageNetwork {    public static void main(String[] args) {        try {            URL url = new URL("http://www.baidu.com/img/logo_gif_be7507c6c688ba2bd085da2ad1015e7d.gif?v=19489737.gif");            InputStream ins = url.openStream();            File file = new File("c:\\test\\logo.gif");            if(!file.exists()){                file.createNewFile();            }            FileOutputStream fos = new FileOutputStream(file) ;            byte[] buffer = new byte[1024];            int hasRead = 0;            while((hasRead = ins.read(buffer)) > 0){                fos.write(buffer,0,hasRead);            }            ins.close();            fos.close();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

 

Java 使用URL从网络读取图片