代码:
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 Integer year = c.get(Calendar.YEAR); Integer month = c.get(Calendar.MONTH)+1; Integer date = c.get(Calendar.DATE); Integer hour = c.get(Calendar.HOUR_OF_DAY); String dateCode = String.format("%02d", date); if(hour <= 12){ dateCode += "00"; }else { dateCode += "12"; } //区县地图编号 String stnm = "51644"; //数据日期拼接 StringBuffer sb = new StringBuffer(); sb.append(year); sb.append(month); sb.append(dateCode); //查询当前数据时间段类,是否已经有数据,如果有数据直接返回 List<DynaBean> dataList = this.queryUrlData(sb.toString(), stnm); if(dataList != null && dataList.size() > 0) { return; } String urlStr = "http://weather.uwyo.edu/cgi-bin/sounding?region=seasia&TYPE=TEXT%3ALIST&YEAR="+year+"&MONTH="+String.format("%02d", month)+"&FROM="+dateCode+"&TO="+dateCode+"&STNM="+stnm; // 网址 //创建一个url对象来指向要采集信息的网址 URL url = new URL(urlStr); //将读取到的字节转化为字符 InputStreamReader inStrRead = new InputStreamReader(url.openStream(),"utf-8"); //读取InputStreamReader转化成的字符 BufferedReader bufRead = new BufferedReader(inStrRead); //读到的内容不为空 String line = null; Integer iLine = 1; //转换数据 List<List<String>> dataAllList = new ArrayList<List<String>>(); //数据最大间隔数 List<String> dataBreakList = Arrays.asList("1,1,1,2,3,1,3,4,0,0,0".split(",")); try { while (((line = bufRead.readLine())!= null)) { //System.out.println(line); //如果是最后一行就结束 if("</PRE><H3>Station information and sounding indices</H3><PRE>".equals(line) || "<HR SIZE=\"1\">".equals(line)) { break; } //从数据第11行数据起始行开始 if (iLine >= 11){ List<String> iLineList = new ArrayList<String>(); String[] split = line.split(" "); List<String> dataList = Arrays.asList(split); //起始行数 Integer startIndex = 0; //结束行数 Integer endIndex = 0; //当前行数 Integer nowIndex = 0; //循环数据最大间隔 for (Integer i = 0;i < dataBreakList.size(); i++) { //默认数据位数 endIndex = startIndex + 3; String dataNum = dataBreakList.get(i); //是否拿到数据 Boolean dataD = false; //是否达到最大值 Boolean dataNumD = false; //是否补充空白 Boolean dataNullD = false; //循环数据 for(Integer j = startIndex;j < endIndex; j++) { //获取当前预留行数,为数据为空时做准备 nowIndex = j+1; String dataStr = dataList.get(j); if(StringUtils.isNotEmpty(dataStr)){ dataD = true; iLineList.add(dataStr); //System.out.print(dataStr+"_"); break; } if(dataD == false && !dataNumD && j == endIndex-1){ dataNumD = true; endIndex = endIndex+Integer.valueOf(dataNum); } if(j == endIndex-1 && dataNumD && !dataNullD) { dataNullD = true; switch (i) { case 3: endIndex = endIndex+3; break; case 4: endIndex = endIndex+2; break; case 5: endIndex = endIndex+4; break; case 9: endIndex = endIndex+5; break; default: break; } } } if(dataD == false){ iLineList.add("0"); //System.out.print("!_"); } startIndex = nowIndex; } dataAllList.add(iLineList); //System.out.println(); } iLine++; } } catch (NumberFormatException e) { new RuntimeException(e.getMessage()); } catch (IOException e) { new RuntimeException(e.getMessage()); }finally { bufRead.close(); } //返回数据 return dataAllList;
原文链接:url = this.url = (还没发布没有链接)