抓取卫星云图

闲来无事,写了个抓取卫星云图的程序。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DownloadImages {
	public static void main(String[] args) throws Exception {
	 	start("201207310000","201208010000"); 
 	}
 	
	//指定时间范围内容抓取图片
 	private static void start(String startDate,String endDate) throws Exception {
 		SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmm");
 		Date d1 = formater.parse(startDate); 
 		Date d2 = formater.parse(endDate);
 		System.out.println("d1:"+d1+"  d2:"+d2);
 		
 		Calendar  ca=Calendar.getInstance();
 		while(d1.compareTo(d2)<=0){               
             ca.setTime(d1);
             //ca2.add(, amount)
             System.out.println(formater.format(d1));
             getImagesFromUrl(formater.format(d1));
             ca.add(ca.MINUTE,15);
             d1=ca.getTime();
      }  
 	}

 	//将内容制定url的内容下载到本地的D盘的weatherimages目录下,由于十区的缘故,图片名称和实际时间并不准确,
 	//需要将实际时间+8个小时
	public static void getImagesFromUrl(String urlStr) throws IOException, Exception{
		String str = "http://i.weather.com.cn/i/product/pic/l/sevp_nsmc_wxcl_asc_e99_achn_lno_py_"+urlStr+"00000.jpg";
 		URL url = new URL(str);
 		SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmm");
 		Date ddd = formater.parse(urlStr);
 		Calendar  ca=Calendar.getInstance();
 		ca.setTime(ddd);
 		ca.add(ca.HOUR_OF_DAY,8);
 		ddd  = ca.getTime();
 		String filename = formater.format(ddd);
		File outFile = new File("D:\\weatherimages\\"+filename+".jpg");
		OutputStream os = new FileOutputStream(outFile);
		InputStream is = url.openStream();
		byte[] buff = new byte[1024];
		while(true) {
			int readed = is.read(buff);
			if(readed == -1) {  
				break;
			}
			byte[] temp = new byte[readed];
			System.arraycopy(buff, 0, temp, 0, readed);
			os.write(temp); 
		}
		is.close(); 
        os.close();
 	}
}

猜你喜欢

转载自java-007.iteye.com/blog/1613617