针对某个网页的快照以及某些重要信息的抓取代码解析
源代码下载地址:http://download.csdn.net/source/1747877
1.先写上配置文件:config.ini 配置几个必要的参数
#------------------------要读取的网页地址--------------------------- url=http://192.168.100.27:9080/server-status #---------------------------存放读取的网页文件地址------------------- path_url=d:/snapshot/url/27server_$.htm #------------------------截取某段重要信息的报告文件------------------- path_report=d:/snapshot/report/report.txt #------------------------日志文件---------------------------------- path_log=d:/snapshot/log/log.txt #-------------------------------截取内容规则----------------------- regx=Current Time(.*)//d{1,4} requests currently being processed #-------------------------------多少秒---------------------------- time=60 |
2.读取配置文件:
public static ArrayList<Config> getConfigIni(){ java.util.Properties p = new java.util.Properties(); ArrayList<Config> al=null; FileInputStream fs; try { Config c=new Config(); al=new ArrayList<Config>(); fs = new FileInputStream("config.ini"); p.load(fs); c.setUrl(p.getProperty("url")); c.setPath_url(p.getProperty("path_url")); c.setPath_report(p.getProperty("path_report")); c.setPath_log(p.getProperty("path_log")); c.setRegx(p.getProperty("regx")); c.setTime(p.getProperty("time")); al.add(c); fs.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } p=null; return al; } |
3.创建写文件方法并设置为私有:
/** * 创建新文�?并写入文件内�? * @param writerfilename 要创建的文件的路�? * @param temp 要写如文件的内容 */ public static synchronized void CreateAndWriterFile(String writerfilename,String temp){ String tempwritename_1=writerfilename.substring(0, writerfilename.lastIndexOf("/"));//针对文件路径�?/"截取 // String tempwritename_2=writerfilename.substring(writerfilename.lastIndexOf("/")+1, writerfilename.length()); Calendar c=Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); File writename=new File(writerfilename); File ifMkdir=new File(tempwritename_1); if(!((tempwritename_1.substring(0,tempwritename_1.indexOf(":"))+1).equals(writerfilename.substring(0,writerfilename.indexOf("/"))))){ if(!ifMkdir.isDirectory()){ ifMkdir.mkdir();//判断文件夹不存在,则创建文件夹 } } if(!writename.exists()){ try { writename.createNewFile();//判断文件不存�?则创建新文件 } catch (IOException e) { e.printStackTrace(); } } BufferedWriter bw=null; try { bw=new BufferedWriter(new FileWriter(writename,true)); bw.write(temp);//写如新文件内的内�? bw.newLine();//换行 bw.close(); } catch (IOException e) { e.printStackTrace(); } } |
4.创建读取网页内容的方法(这个方法是很简单的那种):
public static String getWebContent(String domain) { System.out.println("开始读取内容...(" + domain + ")"); StringBuffer sb = new StringBuffer(); try { java.net.URL url = new java.net.URL(domain); BufferedReader in = new BufferedReader(new InputStreamReader(url .openStream())); String line; while ((line = in.readLine()) != null) { sb.append(line); } in.close(); } catch (Exception e) { // Report any errors that arise // sb.append(e.toString()); System.err.println(e); System.err.println("Usage: java HttpClient <URL> [<filename>]"); } return sb.toString(); } |