码迷,mamicode.com
首页 > Web开发 > 详细

从网页获取图片

时间:2017-03-27 22:14:39      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:read   mkdir   取图   exists   parse   map   close   equals   string   

从中央气象台获取卫星云图的例子,代码如下:

url:地址

技术分享
package com.jointsky.jointframe.test;

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.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 从中国气象台网站获取卫星云图的例子
 * @author Administrator
 *
 */
public class GetSatelliteCloudImageTest {
        //格式化时间
        private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        private static SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
        private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        private static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd/");
        private static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyMMdd");
        private static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        
        /**
         * 主函数
         * @param args
         */
        public static void main(String[] args) {

        //每日卫星云图第一张数据的时间是"03:59:59",但是接近整点的时候才会发布;最后一张图片是23:45的,放在00:00:00之后到00:10:00之间取数据
        //不取数据的开始时间(时间在每天的00:10:00到03:59:59秒之间不取数据)
        String beginTime = " 00:10:00";
        //不去数据的结束时间
        String endTime = " 03:59:59";
        
        Date date = new Date();
        //当前时间(无时分秒)
        String currentTime1 = sdf1.format(date);
        Date begin = new Date();
        Date end =  new Date();
        Date current =  new Date();
        String b = currentTime1+beginTime;
        String e = currentTime1+endTime;
        try {
            begin = sdf.parse(b);
            end = sdf.parse(e);
            current = date;
            //current = sdf.parse(currentTime1+" "+currentTime2);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        //before方法:begin时间在当前时间current返回true
        if (begin.before(current)&&current.before(end)) {
            return;
        }else {
            try {
                String url = getImageUrl(date);
                String filename = getImageName(date,getTimeCode(date));
                Map<String, Object> map = download(url, filename);
                //图片名称
                String imageName = (String) map.get("filename");
                //预报时间
                String forecastTime = (String) map.get("forecastTime");
                //图片路径
                String imagePath = (String) map.get("imagePath");
                System.out.println("图片名称:"+imageName+";预报时间:"+forecastTime+";路径:"+imagePath);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    
    
    /**
     * 获取卫星云图的url地址
     * @return
     * @throws Exception
     */
    public static String getImageUrl(Date date) throws Exception {
        //"http://image.nmc.cn/product/2017/03/24/WXCL/medium/SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY_20170324071500000.JPG"
        String url = "http://image.nmc.cn/product/";


        //图片名称
        String imageName = "SEVP_NSMC_WXCL_ASC_E99_ACHN_LNO_PY_";
        //时间编码
        String timeCode = getTimeCode(date);
        
        Integer code = Integer.parseInt(timeCode);
        
        //url前部出现的时间
        String time = sdf3.format(date);
        String name_time = "";
        
        
        if (code>=1945) {
            //前一天
            Date timex = new Date(date.getTime() - 86400000);
            name_time = sdf4.format(timex);
        }else {
            name_time = sdf4.format(date);
        }
        
        //图片格式
        String imgLayout = "00000.JPG";
        url = url + time + "WXCL/medium/" + imageName + name_time + timeCode + imgLayout;
        return url;
    }

    /**
     * 获取预报时间对应的时间代码
     * @param date
     * @return
     * @throws Exception
     */
    public static String getTimeCode(Date date) throws Exception {
        //当前日期
        String currentDate = sdf1.format(date);
        if (sdf.parse(currentDate+" 03:59:00").before(date)&&(sdf.parse(currentDate+" 04:30:00")).after(date)||(sdf.parse(currentDate+" 04:30:00")).equals(date)) {
            return "1945";
        }else if (sdf.parse(currentDate+" 04:30:00").before(date)&&(sdf.parse(currentDate+" 04:59:59")).after(date)||(sdf.parse(currentDate+" 04:59:59")).equals(date)) {
            return "2015";
        }else if (sdf.parse(currentDate+" 04:59:59").before(date)&&(sdf.parse(currentDate+" 05:30:00")).after(date)||(sdf.parse(currentDate+" 05:30:00")).equals(date)) {
            return "2045";
        }else if (sdf.parse(currentDate+" 05:30:00").before(date)&&(sdf.parse(currentDate+" 05:59:59")).after(date)||(sdf.parse(currentDate+" 05:59:59")).equals(date)) {
            return "2115";
        }else if (sdf.parse(currentDate+" 05:59:59").before(date)&&(sdf.parse(currentDate+" 06:30:00")).after(date)||(sdf.parse(currentDate+" 06:30:00")).equals(date)) {
            return "2145";
        }else if (sdf.parse(currentDate+" 06:30:00").before(date)&&(sdf.parse(currentDate+" 06:59:59")).after(date)||(sdf.parse(currentDate+" 06:59:59")).equals(date)) {
            return "2215";
        }else if (sdf.parse(currentDate+" 06:59:59").before(date)&&(sdf.parse(currentDate+" 07:30:00")).after(date)||(sdf.parse(currentDate+" 07:30:00")).equals(date)) {
            return "2245";
        }else if (sdf.parse(currentDate+" 07:30:00").before(date)&&(sdf.parse(currentDate+" 07:59:59")).after(date)||(sdf.parse(currentDate+" 07:59:59")).equals(date)) {
            return "2315";
        }else if (sdf.parse(currentDate+" 07:59:59").before(date)&&(sdf.parse(currentDate+" 08:30:00")).after(date)||(sdf.parse(currentDate+" 08:30:00")).equals(date)) {
            return "2345";
        }else if (sdf.parse(currentDate+" 08:30:00").before(date)&&(sdf.parse(currentDate+" 08:30:00")).after(date)||(sdf.parse(currentDate+" 08:59:59")).equals(date)) {
            return "0015";
        }else if (sdf.parse(currentDate+" 08:59:59").before(date)&&(sdf.parse(currentDate+" 09:30:00")).after(date)||(sdf.parse(currentDate+" 09:30:00")).equals(date)) {
            return "0045";
        }else if (sdf.parse(currentDate+" 09:30:00").before(date)&&(sdf.parse(currentDate+" 09:59:59")).after(date)||(sdf.parse(currentDate+" 09:59:59")).equals(date)) {
            return "0115";
        }else if (sdf.parse(currentDate+" 09:59:59").before(date)&&(sdf.parse(currentDate+" 10:30:00")).after(date)||(sdf.parse(currentDate+" 10:30:00")).equals(date)) {
            return "0145";
        }else if (sdf.parse(currentDate+" 10:30:00").before(date)&&(sdf.parse(currentDate+" 10:59:59")).after(date)||(sdf.parse(currentDate+" 10:59:59")).equals(date)) {
            return "0215";
        }else if (sdf.parse(currentDate+" 10:59:59").before(date)&&(sdf.parse(currentDate+" 11:30:00")).after(date)||(sdf.parse(currentDate+" 11:30:00")).equals(date)) {
            return "0245";
        }else if (sdf.parse(currentDate+" 11:30:00").before(date)&&(sdf.parse(currentDate+" 11:59:59")).after(date)||(sdf.parse(currentDate+" 11:59:59")).equals(date)) {
            return "0315";
        }else if (sdf.parse(currentDate+" 11:59:59").before(date)&&(sdf.parse(currentDate+" 12:30:00")).after(date)||(sdf.parse(currentDate+" 12:30:00")).equals(date)) {
            return "0345";
        }else if (sdf.parse(currentDate+" 12:30:00").before(date)&&(sdf.parse(currentDate+" 12:59:59")).after(date)||(sdf.parse(currentDate+" 12:59:59")).equals(date)) {
            return "0415";
        }else if (sdf.parse(currentDate+" 12:59:59").before(date)&&(sdf.parse(currentDate+" 13:30:00")).after(date)||(sdf.parse(currentDate+" 13:30:00")).equals(date)) {
            return "0445";
        }else if (sdf.parse(currentDate+" 13:30:00").before(date)&&(sdf.parse(currentDate+" 13:59:59")).after(date)||(sdf.parse(currentDate+" 13:59:59")).equals(date)) {
            return "0515";
        }else if (sdf.parse(currentDate+" 13:59:59").before(date)&&(sdf.parse(currentDate+" 14:30:00")).after(date)||(sdf.parse(currentDate+" 14:30:00")).equals(date)) {
            return "0545";
        }else if (sdf.parse(currentDate+" 14:30:00").before(date)&&(sdf.parse(currentDate+" 14:59:59")).after(date)||(sdf.parse(currentDate+" 14:59:59")).equals(date)) {
            return "0615";
        }else if (sdf.parse(currentDate+" 14:59:59").before(date)&&(sdf.parse(currentDate+" 15:30:00")).after(date)||(sdf.parse(currentDate+" 15:30:00")).equals(date)) {
            return "0645";
        }else if (sdf.parse(currentDate+" 15:30:00").before(date)&&(sdf.parse(currentDate+" 15:59:59")).after(date)||(sdf.parse(currentDate+" 15:59:59")).equals(date)) {
            return "0715";
        }else if (sdf.parse(currentDate+" 15:59:59").before(date)&&(sdf.parse(currentDate+" 16:30:00")).after(date)||(sdf.parse(currentDate+" 16:30:00")).equals(date)) {
            return "0745";
        }else if (sdf.parse(currentDate+" 16:30:00").before(date)&&(sdf.parse(currentDate+" 16:59:59")).after(date)||(sdf.parse(currentDate+" 16:59:59")).equals(date)) {
            return "0815";
        }else if (sdf.parse(currentDate+" 16:59:59").before(date)&&(sdf.parse(currentDate+" 17:30:00")).after(date)||(sdf.parse(currentDate+" 17:30:00")).equals(date)) {
            return "0845";
        }else if (sdf.parse(currentDate+" 17:30:00").before(date)&&(sdf.parse(currentDate+" 17:59:59")).after(date)||(sdf.parse(currentDate+" 17:59:59")).equals(date)) {
            return "0915";
        }else if (sdf.parse(currentDate+" 17:59:59").before(date)&&(sdf.parse(currentDate+" 18:30:00")).after(date)||(sdf.parse(currentDate+" 18:30:00")).equals(date)) {
            return "0945";
        }else if (sdf.parse(currentDate+" 18:30:00").before(date)&&(sdf.parse(currentDate+" 18:59:59")).after(date)||(sdf.parse(currentDate+" 18:59:59")).equals(date)) {
            return "1015";
        }else if (sdf.parse(currentDate+" 18:59:59").before(date)&&(sdf.parse(currentDate+" 19:30:00")).after(date)||(sdf.parse(currentDate+" 19:30:00")).equals(date)) {
            return "1045";
        }else if (sdf.parse(currentDate+" 19:30:00").before(date)&&(sdf.parse(currentDate+" 19:59:59")).after(date)||(sdf.parse(currentDate+" 19:59:59")).equals(date)) {
            return "1115";
        }else if (sdf.parse(currentDate+" 19:59:59").before(date)&&(sdf.parse(currentDate+" 20:30:00")).after(date)||(sdf.parse(currentDate+" 20:30:00")).equals(date)) {
            return "1145";
        }else if (sdf.parse(currentDate+" 20:30:00").before(date)&&(sdf.parse(currentDate+" 20:59:59")).after(date)||(sdf.parse(currentDate+" 20:59:59")).equals(date)) {
            return "1215";
        }else if (sdf.parse(currentDate+" 20:59:59").before(date)&&(sdf.parse(currentDate+" 21:30:00")).after(date)||(sdf.parse(currentDate+" 21:30:00")).equals(date)) {
            return "1245";
        }else if (sdf.parse(currentDate+" 21:30:00").before(date)&&(sdf.parse(currentDate+" 21:59:59")).after(date)||(sdf.parse(currentDate+" 21:59:59")).equals(date)) {
            return "1315";
        }else if (sdf.parse(currentDate+" 21:59:59").before(date)&&(sdf.parse(currentDate+" 22:30:00")).after(date)||(sdf.parse(currentDate+" 22:30:00")).equals(date)) {
            return "1345";
        }else if (sdf.parse(currentDate+" 22:30:00").before(date)&&(sdf.parse(currentDate+" 22:59:59")).after(date)||(sdf.parse(currentDate+" 22:59:59")).equals(date)) {
            return "1415";
        }else if (sdf.parse(currentDate+" 22:59:59").before(date)&&(sdf.parse(currentDate+" 23:30:00")).after(date)||(sdf.parse(currentDate+" 23:30:00")).equals(date)) {
            return "1445";
        }else if (sdf.parse(currentDate+" 23:30:00").before(date)&&(sdf.parse(currentDate+" 22:59:59")).after(date)||(sdf.parse(currentDate+" 23:59:59")).equals(date)) {
            return "1515";
        }else if (sdf.parse(currentDate+" 23:59:59").before(date)&&(sdf.parse(currentDate+" 23:59:59")).after(date)||(sdf.parse(currentDate+" 23:59:59")).equals(date)) {
            return "1545";
        }else if ((sdf.parse(currentDate+" 00:00:00").before(date)||sdf.parse(currentDate+" 00:00:00").equals(date))&&(sdf.parse(currentDate+" 03:59:59").after(date))||sdf.parse(currentDate+" 03:59:59").equals(date)) {
            return "1545";
        }

        return null;
    }

    /**
     * 根据时间和时间代码获取到新命名的卫星云图文件名
     * @param date
     * @param timeCode
     * @return
     * @throws Exception
     */
    public static String getImageName(Date date,String timeCode) throws Exception {
        String time = sdf1.format(date);
        if ("1945".equals(timeCode)) {
            return time+"-03-45-00.JPG";
        }else if ("2015".equals(timeCode)) {
            return time+"-04-15-00.JPG";
        }else if ("2045".equals(timeCode)) {
            return time+"-04-45-00.JPG";
        }else if ("2115".equals(timeCode)) {
            return time+"-05-15-00.JPG";
        }else if ("2145".equals(timeCode)) {
            return time+"-05-45-00.JPG";
        }else if ("2215".equals(timeCode)) {
            return time+"-06-15-00.JPG";
        }else if ("2245".equals(timeCode)) {
            return time+"-06-45-00.JPG";
        }else if ("2315".equals(timeCode)) {
            return time+"-07-15-00.JPG";
        }else if ("2345".equals(timeCode)) {
            return time+"-07-45-00.JPG";
        }else if ("0015".equals(timeCode)) {
            return time+"-08-15-00.JPG";
        }else if ("0045".equals(timeCode)) {
            return time+"-08-45-00.JPG";
        }else if ("0115".equals(timeCode)) {
            return time+"-09-15-00.JPG";
        }else if ("0145".equals(timeCode)) {
            return time+"-09-45-00.JPG";
        }else if ("0215".equals(timeCode)) {
            return time+"-10-15-00.JPG";
        }else if ("0245".equals(timeCode)) {
            return time+"-10-45-00.JPG";
        }else if ("0315".equals(timeCode)) {
            return time+"-11-15-00.JPG";
        }else if ("0345".equals(timeCode)) {
            return time+"-11-45-00.JPG";
        }else if ("0415".equals(timeCode)) {
            return time+"-12-15-00.JPG";
        }else if ("0445".equals(timeCode)) {
            return time+"-12-45-00.JPG";
        }else if ("0515".equals(timeCode)) {
            return time+"-13-15-00.JPG";
        }else if ("0545".equals(timeCode)) {
            return time+"-13-45-00.JPG";
        }else if ("0615".equals(timeCode)) {
            return time+"-14-15-00.JPG";
        }else if ("0645".equals(timeCode)) {
            return time+"-14-45-00.JPG";
        }else if ("0715".equals(timeCode)) {
            return time+"-15-15-00.JPG";
        }else if ("0745".equals(timeCode)) {
            return time+"-15-45-00.JPG";
        }else if ("0815".equals(timeCode)) {
            return time+"-16-15-00.JPG";
        }else if ("0845".equals(timeCode)) {
            return time+"-16-45-00.JPG";
        }else if ("0915".equals(timeCode)) {
            return time+"-17-15-00.JPG";
        }else if ("0945".equals(timeCode)) {
            return time+"-17-45-00.JPG";
        }else if ("1015".equals(timeCode)) {
            return time+"-18-15-00.JPG";
        }else if ("1045".equals(timeCode)) {
            return time+"-18-45-00.JPG";
        }else if ("1115".equals(timeCode)) {
            return time+"-19-15-00.JPG";
        }else if ("1145".equals(timeCode)) {
            return time+"-19-45-00.JPG";
        }else if ("1215".equals(timeCode)) {
            return time+"-20-15-00.JPG";
        }else if ("1245".equals(timeCode)) {
            return time+"-20-45-00.JPG";
        }else if ("1315".equals(timeCode)) {
            return time+"-21-15-00.JPG";
        }else if ("1345".equals(timeCode)) {
            return time+"-21-45-00.JPG";
        }else if ("1415".equals(timeCode)) {
            return time+"-22-15-00.JPG";
        }else if ("1445".equals(timeCode)) {
            return time+"-22-45-00.JPG";
        }else if ("1515".equals(timeCode)) {
            return time+"-23-15-00.JPG";
        }else if ("1545".equals(timeCode)) {
            return time+"-23-45-00.JPG";
        }
        return null;
    }
    
    /**
       * 下载文件到本地
       * @param urlString 被下载的文件地址
       * @param filename 本地文件名
       * @throws Exception 各种异常
       */
    public static Map<String, Object> download(String urlString, String filename) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String path = "";
        Date time = sdf5.parse(filename);
        String forecastTime = sdf.format(time);
        String imagePath = "D:/Jointsky/JointFrame/satelliteCloudImage/";
        String[] filenames = filename.split("\\.");
        String[] strs = filenames[0].split("-");
        for (int i = 0; i < strs.length; i++) {
            imagePath = imagePath + strs[i]+"/";
        }
        
        path = imagePath;
        imagePath = imagePath + filename;
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        // 输入流
        InputStream is = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        //判断如果文件夹存在则保存,不存在则创建
        File file = new File(path);
        if (!file.exists()) {
            if (!file.isDirectory()) {
                file.mkdirs();
            }
            try {    
                file.createNewFile();    
            } catch (IOException e) {        
                e.printStackTrace();    
            }    
        }
        // 输出的文件流
        OutputStream os = new FileOutputStream(imagePath);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
          os.write(bs, 0, len);
        }
        // 完毕,关闭所有链接
        os.close();
        is.close();
        //预测时间
        if (!forecastTime.equals("")&&forecastTime!=null) {
            map.put("forecastTime", forecastTime);
        }
        //图片名称
        if (!filename.equals("")&&filename!=null) {
            map.put("filename", filename);
        }
        //图片存放路径
        if (!path.equals("")&&path!=null) {
            if (!filename.equals("")&&filename!=null) {
                map.put("imagePath", path+filename);
            }
        }
        return map;
    }

    public static SimpleDateFormat getSdf1() {
        return sdf1;
    }


    public static void setSdf1(SimpleDateFormat sdf1) {
        GetSatelliteCloudImageTest.sdf1 = sdf1;
    }


    public static SimpleDateFormat getSdf2() {
        return sdf2;
    }


    public static void setSdf2(SimpleDateFormat sdf2) {
        GetSatelliteCloudImageTest.sdf2 = sdf2;
    }


    public static SimpleDateFormat getSdf() {
        return sdf;
    }


    public static void setSdf(SimpleDateFormat sdf) {
        GetSatelliteCloudImageTest.sdf = sdf;
    }


    public static SimpleDateFormat getSdf3() {
        return sdf3;
    }


    public static void setSdf3(SimpleDateFormat sdf3) {
        GetSatelliteCloudImageTest.sdf3 = sdf3;
    } 
    
    
}
View Code

 

从网页获取图片

标签:read   mkdir   取图   exists   parse   map   close   equals   string   

原文地址:http://www.cnblogs.com/shuilangyizu/p/6628453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!