当前位置 : 主页 > 编程语言 > java >

一个图片批量下载的java程序

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package mzitu;import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * http://pic.dofay.com/2015/01/0
gistfile1.txt
package mzitu;

import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.net.HttpURLConnection;  
import java.net.URL;  
/**
 * http://pic.dofay.com/2015/01/01x01.jpg  
 * 据此规律编写
 * 2015年6月1日
 */
public class Mzitu {  

    public static void main(String[] args) {  
    	find();
    }  
 
    public static void writeImageToDisk(byte[] img, String fileName){  
        try {  
            File file = new File("D:\\360Downloads\\mzitu\\" + fileName);  
            FileOutputStream fops = new FileOutputStream(file);  
            fops.write(img);  
            fops.flush();  
            fops.close();  
            System.out.println(fileName + ":图片已经写入");  
        } catch (Exception e) {  
            //e.printStackTrace();  
        }  
    }  

    public static byte[] getImageFromNetByUrl(String strUrl){  
        try {  
            URL url = new URL(strUrl);  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(50 * 1000);  
            InputStream inStream = conn.getInputStream();//通过输入流获取图片数据  
            byte[] btImg = readInputStream(inStream);//得到图片的二进制数据  
            return btImg;  
        } catch (Exception e) {  
            //e.printStackTrace();  
        }  
        return null;  
    }  
  
    public static byte[] readInputStream(InputStream inStream) throws Exception{  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        while( (len=inStream.read(buffer)) != -1 ){  
            outStream.write(buffer, 0, len);  
        }  
        inStream.close();  
        return outStream.toByteArray();  
    }
    
    public static String int2Str(int i){
    	String t = String.valueOf(i);
    	if (t.length()==1){
    		t = "0"+t;
    	}
    	return t;
    }
    
    public static void find(){
    	boolean haspic = true;
    	int num = 0;
    	String url = "http://pic.dofay.com/";
    	for (int yyyy = 2014 ; yyyy <= 2014 ; yyyy++){
    		for (int mm = 1 ; mm <= 12 ; mm++){
    			for (int dd = 1 ; dd <= 31 ; dd++){
    				haspic = true;
    				num = 0;
    				while(haspic){
    					num++;
    			        String picurl = url+yyyy+"/"+int2Str(mm)+"/"+int2Str(dd)+"x"+int2Str(num)+".jpg";  
    			        byte[] btImg = getImageFromNetByUrl(picurl);  
    			        if(null != btImg && btImg.length > 0){  
    			            String fileName = yyyy+"-"+int2Str(mm)+"-"+int2Str(dd)+"x"+int2Str(num)+".jpg";  
    			            //System.out.println(fileName+" 读取到:" + btImg.length + " 字节");  
    			            writeImageToDisk(btImg, fileName);  
    			        }else{  
    			        	haspic = false;;  
    			        }      					
    				}
    			}
    		}
    	}
    }
}
上一篇:简单的员工管理系统
下一篇:n*n 乘法表
网友评论