Java学习笔记之网络编程基础-通过URL获取网络图片

package com.kkoolerter;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;

public class GetImageByURL {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        URL url = new URL(
                "http://hiphotos.baidu.com/baidu/pic/item/697e2a3fb95157a655e7234c.jpg");
        DataInputStream dis = new DataInputStream(url.openStream());
        OutputStream os = new FileOutputStream(new File("download.jpg"));
        byte buffer[] = new byte[1024];
        int len = -1;
        while((len = dis.read(buffer))!=-1){
            os.write(buffer, 0, len);
        }
        
        os.close();
        dis.close();
       
    }

}
 

你可能感兴趣的:(java)