通过字节流下载网络图片到本地案例

package com.zhangxueliang.demo;

import java.io.*;
import java.net.URL;

public class URLDemo {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://f.hiphotos.baidu.com/image/pic/item/b3119313b07eca80787730f59f2397dda14483b5.jpg");
        String file = url.getFile();
        System.out.println("==========> "+file);
        InputStream is = url.openStream();
        File f = new File("D:\\tempppppppp6666\\123");
        if (!f.exists())
            f.mkdirs();
        OutputStream os = new FileOutputStream(new File(f,"a.jpg"));
        int len;
        byte[] bys = new byte[1024];
        while ((len=is.read(bys))!=-1){
            os.write(bys,0,len);
        }
        os.close();
        is.close();
    }
}

 

posted @ 2019-03-16 20:45  学亮编程手记  阅读(560)  评论(0编辑  收藏  举报