📄 runserver.java
字号:
/***************************************************
* 程序文件名称: RunServer.java
* 功能:接收和发送查询的天气信息
***************************************************/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.sql.*;
public class RunServer extends Thread{
boolean flag=true;
TextArea point;
ServerSocket server=null;
Socket you = null;
String s= null;
String ids=null;
DataOutputStream out = null;
DataInputStream in = null;
//创建主窗口的文本区
RunServer(TextArea po){
point=po;
}
// 线程,创建输入输出流,并在文本区显示相应信息
public void run(){
point.append("\n");
point.append("This is Server!");
try{
server=new ServerSocket(5000);
}
catch(IOException e){
System.out.println("Error:"+e);
}
try{
while(true){
you=server.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
//读取客户端传来的要查询的地区名称
s=in.readUTF();
point.append("\n");
point.append("收到查询>>>"+s);
flag=true;
//将地区名称转换为id编号
exCityId();
if(flag){
//格式化连接网站的网址字符串
formatSearch();
//连接网站并抓取信息
sendSearch();
//连接数据库,将查询地区名称转换为网站
//能识别的id编号,得到最终所需要的查询信息
exInfo();
//向客户端发送查询到的信息
out.writeUTF(s);
}
else{
s="没有这个城市的信息";
point.append("\n");
point.append(s);
out.writeUTF(s);
}
} // while end
}
catch(IOException e){
System.out.println(" "+e);
}
}
//连接数据库,将查询地区名称转换为网站能识别的id编号
//得到最终所需要的查询信息
private void exInfo(){
//查找网页中包含下列关键词
//<a class="font_sl" href="citydetail.jsp?sta_id=58724">
//引号字符
char[] tempC={'"'};
//设置关键位置特征字符串的起始字符
String tempS="<a class=";
//组合成关键位置的特征字符串
tempS=tempS+String.valueOf(tempC)+"font_sl"+String.valueOf(tempC)
+" href="+String.valueOf(tempC)+"citydetail.jsp?sta_id="
+ids+String.valueOf(tempC)+">";
//设置临时位置变量
int pos,pos2;
//定位s中temps第一次出现的位置,
//即上面关键字符串在返回的html文件字符串中的位置
pos=s.indexOf(tempS);
//位置pos指向特征字符串之后面
pos=pos+tempS.length();
//截取pos后面5个unicode字符
tempS=s.substring(pos,pos+5);
//临时标志
boolean x=true;
/*说明
*由于所要截取的html中的有用信息中,存在于两种不同的特征位置
*用临时标志x,以示区别
*/
//如果这5个字符是这个
if(tempS.equalsIgnoreCase("<span")){
//把位置后移动22
pos=pos+22;
//设置标志为否
x=false;
}
//保护位置
pos2=pos;
//从pos2位置开始,即是从pos位置向下不停对比,直到遇到符号"<"为止
while(true){
if(s.substring(pos2,pos2+1).equalsIgnoreCase("<")){
break;
}
//记录所在的位置
pos2++;
}
/*
* 这样就得到了一种有用消息的位置
*/
//截取我们所需要的信息
tempS=s.substring(pos,pos2);
//格式化
tempS=tempS+"\n";
//保护位置
pos=pos2;
//截取pos后的所有字符到s
s=s.substring(pos);
//定位特征字符的位置
pos=s.indexOf("alt=");
//跳过这个特征
pos=pos+5;
//保护位置
pos2=pos;
//从pos2,即从pos位置向下不停对比,直到遇到>为止
while(true){
if(s.substring(pos2,pos2+1).equalsIgnoreCase(">")){
break;
}
//记录所在的位置
pos2++;
}
//跳回一个字符,调整
pos--;
//截取我们需要的信息,加入到先前得到的信息中
tempS=tempS+s.substring(pos,pos2);
//截取新的需要分析的字符串
s=s.substring(pos2);
//得到特征字符的位置
pos2=s.indexOf("</td>");
//得到特征字符的位置
pos=s.indexOf("alt=");
//两个特征字符在html字符串中首次出现的先后将决定下面的消息是否用
if(pos<pos2){//如果pos先于pos2
//得到特征字符的位置
pos=s.indexOf("alt=");
//跳过5,调整
pos=pos+5;
//位置保护
pos2=pos;
//从pos2,即从pos位置向下不停对比,直到遇到>为止
while(true){
if(s.substring(pos2,pos2+1).equalsIgnoreCase(">")){
break;
}
//记录所在的位置
pos2++;
}
//跳回一个,调整
pos--;
//将得到的有用消息加入到先前的到的消息中
tempS=tempS+"~"+s.substring(pos,pos2);
//截取新的待分析字符串
s=s.substring(pos2);
}
//定位特征字符位置
pos=s.indexOf("class");
//跳过16,调整
pos=pos+16;
//截取新的待分析字符串
s=s.substring(pos);
//定位特征字符位置
pos=s.indexOf("</td>");
//有用消息的不同情况,不同的格式方法
if(x){
tempS=tempS+"温度:"+s.substring(0,pos);
}else{
tempS=tempS+"温度:"+s.substring(0,pos-8);
}
//定位特征字符位置
pos=tempS.indexOf("</td");
//清除掉所有的无用字符
if(pos>=0){
tempS=tempS.replaceAll("</td","");
}
//得到所需要的查询结果
s=tempS;
//显示结果
point.append(s);
}
//连接网站并抓取信息:
private void sendSearch(){
//服务器窗口显示
point.append("\n");
point.append("Server>>>服务器发送查询");
//临时字符串
String linStr="";
String conStr="";
//访问网站的url地址对象,s为网址以及访问信息
try{
URL myURL=new URL(s);
//缓冲
BufferedReader myURLReader=new BufferedReader(
new InputStreamReader(myURL.openStream()) //访问
);
//逐行读取到临时字符变量
while((linStr=myURLReader.readLine())!=null){
conStr=conStr+linStr+"\n";
}
//关闭连接
myURLReader.close();
//得到返回的html字符串变量
s=conStr;
}
catch(Exception err){
//错误显示
System.err.println(err);
}
//服务器显示
point.append("\n");
point.append("Server>>>服务器得到结果");
point.append("\n");
}
//格式化连接网站的网址字符串:
private void formatSearch(){
//服务器窗口显示
point.append("\n");
point.append(s+">>>准备格式化");
//查询字符串实例
String http="http://www.weatherXX.com/forecast/citydetail.jsp?sta_id=";
//生成标准字符串
http=http+s;
//服务器窗口显示
point.append("\n");
point.append(s+">>>准备格式化成功http://www.weatherXX.com/...");
point.append("\n");
s=http;
}
//将地区名称转换为id编号:
private void exCityId(){
//服务器窗口显示
point.append("\n");
point.append(s+">>>转换ID");
//临时字符串变量
String id="";
String city="";
Connection Ex1Con=null;
Statement Ex1Stmt=null;
try{
//数据库驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){ }
try{
//与数据库建立连接,
Ex1Con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=Info.mdb","","");
Ex1Stmt=Ex1Con.createStatement();
//建立SQL查询字符串
s="SELECT * FROM cityID WHERE city = '"+s+"'";
//得到查询后的数据集
ResultSet rs=Ex1Stmt.executeQuery(s);
rs.next();
//得到id编号
id=rs.getString("id");
//关闭数据连接
Ex1Con.close();
}
catch(SQLException e) //异常
{
System.out.println(e);
//如果查询有异常,标志为假,表示没有查到
flag=false;
//说明
//flag为全局变量
}
//服务器窗口显示
point.append("\n");
point.append(id+">>>得到ID");
//得到城市的id值
ids=id;
s=id;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -