📄 getpagedata.java
字号:
+Information.column[40]+"');";
//执行SQL插入语句
stmt.executeUpdate(strSQL);
//输出刚刚执行的SQL语句,以便分析
System.out.println("表\"设备管理_设备状态_刷卡记录\"中成功添加记录:");
System.out.println(" 卡号="+Information.column[41]+
"\n 机房编号="+Information.column[1]+
"\n 开门起始时间="+Information.column[38]+
"\n 开门结束时间="+Information.column[39]+
"\n 间隔时间="+Information.column[40]+"\n");
}
}
//"实时图像"页面处理函数
void dealWeb3() throws Exception
{
//address="http://192.9.201.121/web1.3.htm"; //网页地址
address="file:///D:/学习/程序/网页提取/单片机网页/web1.3.htm"; //网页地址
Information.indexColumn=0; //数组指针复位
Information.currentPage=3; //标识当前正在处理的页面
String date;
String time;
String strSQL; //存储SQL语句
int i; //临时变量
System.out.println("\n\n======================处理\"实时图像\"页面=======================");
myScanWebPage.openConnection(address); //建立http连接
myScanWebPage.readFile(); //读取所需的页面数据
myScanWebPage.closeConnection(); //关闭http连接
System.out.println("\n------------提取到页面数据-----------");
for(i=0;i<Information.indexColumn;i++)
System.out.println(i+":"+Information.column[i]);
System.out.println("------------更新数据库----------------");
i=Information.column[2].indexOf("日");
date=Information.column[2].substring(0,i+1); //提取页面日期
time=Information.column[2].substring(i+2); //提取页面时间
//依次访问每一个活动图象区域
for(i=0;i<4;i++)
{
//判断当前图象区域是否处于活动状态
if(Information.column[i+3].equals("null")) continue;
//插入新纪录
strSQL="insert into 图象管理"
+"(机房名称,机房编号,图片地址,活动区域,图片日期,图片时间) values('"
+Information.column[0]+"','"+Information.column[1]
+"','"+Information.column[i+7]+"','"+Information.column[i+3]
+"','"+date+"','"+time+"');";
//执行SQL插入语句
stmt.executeUpdate(strSQL);
//输出刚刚执行的SQL语句,以便分析
System.out.println("表\"图象管理\"中成功添加记录:");
System.out.println(" 机房名称="+Information.column[0]+
"\n 机房编号="+Information.column[1]+
"\n 图片地址="+Information.column[i+7]+
"\n 活动区域="+Information.column[i+3]+
"\n 图片日期="+date+"\n 图片时间="+time+"\n");
}
}
//关闭数据库连接
void closeConnection() throws Exception
{
con.close();
}
}
//用于存储页面信息的数据结构
class Information
{
static int currentPage; //用于标识当前正在处理的页面
static String[] column=new String[50]; //用于存储页面信息
static int indexColumn; //用于标识当前正在处理页面所用String[]数组的长度
static String warning; //存储总告警信息
}
class ScanWebPage
{
String sCurrentLine;
String ID;
int IDindexStart,IDindexEnd;
BufferedReader buffR;
public String infor;
void openConnection(String address) throws Exception
{
URL fileURL= new URL(address); //创建URL对象
InputStream in = fileURL.openStream( );
// 为增加性能存储输入流
in = new BufferedInputStream(in);
// 将输入流连接到阅读器
buffR = new BufferedReader(new InputStreamReader(in));
}
void readFile() throws Exception
{
int i,j; //临时变量
char firstChar; //用于存储某一字符串的第一个字符
//每次从网页的html源文件中读取一行数据进行分析
while ((sCurrentLine = buffR.readLine()) != null)
{
//根据特定的id标志判断当前行是否有所需的数据
IDindexStart=sCurrentLine.indexOf("id=");
if(IDindexStart>1) //当前行存在id
{
//读取id值的首尾字符所在的位置
i=sCurrentLine.indexOf(">",IDindexStart);
j=sCurrentLine.indexOf(" ",IDindexStart+1);
if(i<0) IDindexEnd=j; //此行没有html标记>
else if(j<0) IDindexEnd=i; //此行没有空格
else if(i<j ) IDindexEnd=i; //两者中选择最近的
else IDindexEnd=j;
//3是字符串id=的长度
ID=sCurrentLine.substring(IDindexStart+3,IDindexEnd);
//首页只需获得总告警信息
if( Information.currentPage==0 )
{
if(ID.equals("p_alarm")) //总告警信息的id标志
{
i=sCurrentLine.indexOf("src=",IDindexEnd);
//5为字符串src="的长度
j=sCurrentLine.indexOf("\"",i+5);
Information.warning=sCurrentLine.substring(i+5,j);
}
else continue;
}
//提取ID字符串的第一个字符
firstChar=ID.charAt(0);
//System.out.println("firstChar="+firstChar);
//根据首字符判断其类型,以进行相应的处理
switch(firstChar)
{
//表示网页中为input类型,需要读取其value值
case 'i': //System.out.println("input类型");
dealInput(ID,sCurrentLine);
break;
//表示网页中为check 类型,需要判断是否checked
case 'c': //System.out.println("check类型");
dealCheck(ID,sCurrentLine);
break;
//表示网页中为select类型,需要判断下拉菜单中哪一项被selected
case 's'://System.out.println("select类型");
dealSelect(ID,sCurrentLine);
break;
case 'p'://System.out.println("img类型");
dealImage(ID,sCurrentLine);
break;
default ://System.out.println("一般类型");
dealOthers(ID,sCurrentLine);
}
}
}
}
void dealInput(String id,String sLine) throws Exception
{
String value; //记录ID所标识的数据信息
int i,j,m,n; //临时变量
i=sLine.indexOf("value=",IDindexEnd); //字符串value="的长度为7
j=sLine.indexOf("\"",i+7);
if(i<0||j<0)
value="null"; //若网页中没有值,设置为null
else
{
value=sLine.substring(i+7,j); //提取数据信息
}
//存储数据信息
Information.column[Information.indexColumn ++ ] = value;
}
void dealCheck(String id,String sLine) throws Exception
{
String value;
int i,j;
//判断被选中的项
i=sLine.indexOf("CHECKED",IDindexEnd);
j=sLine.indexOf("checked",IDindexEnd);
if(i+j<0)
value="null"; //若网页中没有值,设置为null
else
{
value=id.substring(id.length()-1); //提取数据信息
}
//存储数据信息
Information.column[Information.indexColumn ++ ] = value;
}
void dealSelect(String id,String sLine) throws Exception
{
String value;
int i,j;
//判断被选中的项
i=sLine.indexOf("SELECTED",IDindexEnd);
j=sLine.indexOf("selected",IDindexEnd);
if(i+j<0)
{
value="null"; //若网页中没有值,设置为null
}
else
{
i=sLine.indexOf(">",i+j);
j=sLine.indexOf("<",i);
value=sLine.substring(i+1,j); //提取数据信息
}
//存储数据信息
Information.column[Information.indexColumn ++ ] = value;
}
void dealImage(String id,String sLine) throws Exception
{
String value;
int i,j,m,n;
i=sLine.indexOf("src=",IDindexEnd); //字符串src="的长度为5
j=sLine.indexOf("\"",i+5);
//System.out.println("in dealInput() i="+i+" j="+j);
if(i<0||j<0)
value="null"; //若网页中没有值,设置为null
else
{
value=sLine.substring(i+5,j);
}
//存储数据信息
Information.column[Information.indexColumn ++ ] = value;
//i= Information.indexColumn -1 ; //测试代码
//System.out.println("select信息-"+i+"-"+Information.column[i]);
}
void dealOthers(String id,String sLine) throws Exception
{
String temp,value="";
int i,j;
i=sLine.indexOf(">",IDindexEnd);
j=sLine.indexOf("<",i);
temp=sLine.substring(i+1,j);
//去掉空格标志 程序
i=0;
j=temp.indexOf("&",i);
if(j<0) value=temp; //没有空格标志
else
{
value+=temp.substring(i,j);
while(j>0)
{
i=j+6;
j=temp.indexOf("&",j+1); //判断空格所在的位置
if(j > i) //处理空格中间有字符
value+=temp.substring(i,j);
if( ( j+8 )>temp.length()) //处理最后一个空格
{
value+=temp.substring(j+6,temp.length());
break;
}
}
}
//存储数据信息
Information.column[Information.indexColumn ++ ] = value;
}
void closeConnection() throws Exception
{
buffR.close(); //关闭数据流
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -