字节流批量下载图片代码实现

package com.zhangxueliang.demo;

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

public class URLDemo {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        InputStream resourceAsStream = Object.class.getResourceAsStream("/imgs.properties");
        properties.load(resourceAsStream);
        InputStream is=null;
        OutputStream os = null;
        for (int i=1;i<=properties.size();i++){
            URL url = new URL((String) properties.get("img" + i));
            String file = url.getFile();
            System.out.println("==========> "+file);
            is = url.openStream();
            File f = new File("D:\\zhangxueliang\\images");
            if (!f.exists())
                f.mkdirs();
            os = new FileOutputStream(new File(f,i+".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 21:11  学亮编程手记  阅读(228)  评论(0编辑  收藏  举报