📄 showweather.java
字号:
/*
*
*/
import java.io.*;
import java.text.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowWeather extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String sType = request.getParameter ("type");
String sTime=new String ();
String sPathName= new String ();
String sFileName=new String ();
if ((sType==null)||(sType.trim ().length ()==0))
{
return ;
}
sType = sType.trim ();
//查出当前库中最新天气数据
DBOperater DB = new DBOperater (sType);
ResultSet rs = DB.executeQuery ("select time from "+sType+" ORDER BY time DESC");
if(rs!=null){
try{
if( rs.next () )
sTime = rs.getString(1);
}
catch(Exception e){
DB.close ();
return;
}
}
DB.close ();
if ((sTime==null)||(sTime.trim ().length ()!=8))
{
return;
}
sTime=sTime.trim ();
sPathName=CommonMethods.sHtmlPath +"weather";
File fWeatherPath=new File (sPathName);
if (!fWeatherPath.exists() || !fWeatherPath.isDirectory() )
{ try{
fWeatherPath.mkdirs();
}catch(Exception e){
return;
}
}
sFileName=sPathName+"/"+sTime+sType+".htm";
File fWeatherFile=new File (sFileName);
// RandomAccessFile fWeatherHtmlFile= null ;
//判断本日天气的静态文件是否存在,如果不存在则创建此文件
if (!fWeatherFile.exists ()){
String sTitle = new String ();
String sYear = new String ();
String sMonth = new String ();
String sDay = new String ();
String sHours = new String ();
if (sType.compareTo ("m24")==0)
{
sTitle="早间城市24";
sHours ="7";
}
if (sType.compareTo ("n24")==0)
{
sTitle ="晚间城市24";
sHours = "19";
}
if (sType.compareTo ("n48")==0)
{
sTitle = "晚间城市48";
sHours = "19";
}
/////////////////////////////
//得到天气显示的模板
TemplateList tempList=CommonMethods.getShowWeatherTemplate ();
SingleTemplate st;
st = tempList.searchTemplate ("ROOT");
if(st==null)
{
return ;
}
int nKey = st.getKeyNum ();
String sWeatherShow="";
String sKey= new String ();
int nMonth,nDay;
sYear = sTime.substring (0,4);
sMonth = sTime.substring (4,6);
sDay = sTime.substring (6,8);
try{
nMonth= Integer.valueOf (sMonth).intValue ();
}catch(Exception e)
{
return;
}
try{
nDay= Integer.valueOf (sDay).intValue ();
}catch(Exception e)
{
return;
}
sMonth = nMonth+"";
sDay = nDay+"";
for(int i=0;i<nKey;i++){
sWeatherShow+=st.getStringAt (i);
sKey = st.getKeyAt (i);
if( sKey.compareTo("cgi-path")==0 )
sWeatherShow+= (CommonMethods.sHostNameCGI);
if( sKey.compareTo("标题")==0 )
sWeatherShow+=sTitle;
if (sKey.compareTo ("年")==0)
sWeatherShow+=sYear;
if (sKey.compareTo ("月")==0)
sWeatherShow+=sMonth;
if (sKey.compareTo ("日")==0)
sWeatherShow+=sDay;
if (sKey.compareTo ("时")==0)
sWeatherShow+=sHours;
if (sKey.compareTo ("天气情况")==0)
{
//查找并替换子模板
SingleTemplate st1;
st1 = tempList.searchTemplate ("天气预报");
int nKey1 = st1.getKeyNum ();
if(st1==null)
{
return;
}
String sCity = new String ();
String sCondition = new String ();
String sHigh = new String ();
String sLow = new String ();
String sWind = new String ();
String sKey1 = new String ();
DBOperater DB1 = new DBOperater (sType);
ResultSet rs1 = DB1.executeQuery ("select city,condition,high,low,wind from "+sType+" Where time='"+sTime+"'");
// ResultSet rs1 = DB1.executeQuery ("SELECT city, condition, high, low, wind FROM m24 WHERE time = '20000505'");
if(rs1!=null){
try{
while( rs1.next () )
{
sCity = rs1.getString (1);
sCondition = rs1.getString (2);
sHigh = rs1.getString (3);
sLow = rs1.getString (4);
sWind = rs1.getString (5);
if (sCity==null)
sCity = "";
if (sCondition==null)
sCondition = "";
if (sHigh ==null)
sHigh="";
if (sLow==null)
sLow="";
if (sWind ==null)
sWind = "";
for (int j=0;j<nKey1;j++)
{
sWeatherShow+=st1.getStringAt (j);
sKey1 = st1.getKeyAt (j);
if( sKey1.compareTo("cgi-path")==0 )
sWeatherShow+= (CommonMethods.sHostNameCGI);
if (sKey1.compareTo ("城市名")==0)
sWeatherShow+=sCity;
if (sKey1.compareTo ("天气")==0)
sWeatherShow+=sCondition;
if (sKey1.compareTo ("最高")==0)
sWeatherShow+=sHigh;
if (sKey1.compareTo ("最低")==0)
sWeatherShow+=sLow;
if (sKey1.compareTo ("风向")==0)
sWeatherShow+=sWind;
}
sWeatherShow+=st1.getEndString ();
}
}
catch(Exception e){
DB1.close ();
return;
}
}
DB1.close ();
}
}
sWeatherShow+=st.getEndString();
int fileLength;
fileLength=sWeatherShow.length();
byte[] b1=new byte[fileLength];
b1=sWeatherShow.getBytes ();
CommonMethods.WriteToDisk (b1,sFileName);
}
response.sendRedirect (CommonMethods.sWebRoot +"weather/"+sTime+sType+".htm");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -