package com.teamdev.jxbrowser.chromium.demo.k_spider.saveImg

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URL;
import java.net.URLConnection;

public class DownLoadImgUtil {
    public static void main(String[] args) {
        try {
            download("https://fuss10.elemecdn.com/b/94/35cd18dd8bb051f47f04a8a14e570jpeg.jpeg", "C:/1.jpg","D:/img/baidu/001");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

      
    public static void download(String urlString, String filename,String wjjmc) throws Exception { 
        File file =new File(wjjmc);    
        if  (!file .exists()  && !file .isDirectory())      
        {       
          //  System.out.println("//不存在");  
            file .mkdir();    
        } 
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    //System.out.println("download xml start.......");  
    URL url = new URL(urlString);  
    URLConnection con = url.openConnection();  
    InputStream is = con.getInputStream();  
      
      
    byte[] bs = new byte[1024];  
    int len;  
    OutputStream os = new FileOutputStream(filename);  
    while ((len = is.read(bs)) != -1) {  
    os.write(bs, 0, len);  
    }  
    os.close();  
    is.close();  
    //System.out.println("download xml over.......");  
    }  
}