<%@ page contentType="text/html; charset=gb2312"%> <%@ page import="java.io.*"%> <%@ page import="java.util.*" %> 全文搜索



全文搜索



<%! //filestring = filterweb(filestring,'<','>'); //过滤网页中的HTML语句 String filterweb(String str, char c1, char c2) { int pos1=str.indexOf(c1); int pos2=str.indexOf(c2); while((pos1!=-1)&&(pos2!=-1)) { str=str.substring(0,pos1)+str.substring(pos2+1,str.length()); pos1=str.indexOf(c1); pos2=str.indexOf(c2); } return str; } %> <% String[] FileArray = new String[1000]; int pFileArray=0; String[] DirectoryQueue = new String[1000]; int pDirectoryQueue=0; String findstr=""; String realPath=""; String element=""; String tmps=""; Iterator iter=null; Collection set; set=application.getResourcePaths("/"); iter = set.iterator(); while (iter.hasNext()) { element = iter.next(); realPath=application.getRealPath("/"); realPath=realPath+element.substring(1,element.length()); tmps=realPath.substring(realPath.length()-1,realPath.length()); if(tmps.equals("/")) //如果为目录 { realPath=realPath.replace('/','\\'); realPath=realPath.substring(0,realPath.length()-1); //删除目录种最后的字符“\” DirectoryQueue[pDirectoryQueue++]=realPath; //入队列 } else { FileArray[pFileArray++]=realPath; } } //end while (iter.hasNext()) while(pDirectoryQueue!=0) //如果队列不为空 { pDirectoryQueue--; String CurPath=DirectoryQueue[pDirectoryQueue]; String relativePath =application.getRealPath("/"); //"D:\tomcat6\webapps\ROOT\" relativePath ="\\"+CurPath.replace(relativePath ,""); //获取相对路径 set=application.getResourcePaths(relativePath ); iter = set.iterator(); while(iter.hasNext()) { element = iter.next(); realPath=application.getRealPath("/"); //"D:\tomcat6\webapps\ROOT\" realPath=realPath+element.substring(1,element.length()); //构造绝对路径 tmps=realPath.substring(realPath.length()-1,realPath.length()); if(tmps.equals("/")) //如果为目录 { realPath=realPath.replace('/','\\'); realPath=realPath.substring(0,realPath.length()-1); //删除目录种最后的字符“\” DirectoryQueue[pDirectoryQueue++]=realPath; //入队列 } else { realPath=realPath.replace('/','\\'); //if(realPath.lastIndexOf(".htm")!=-1) { FileArray[pFileArray++]=realPath; //out.println(realPath+" --------> "+realPath.lastIndexOf(".htm")+"
"); } // else if(realPath.lastIndexOf(".html")!=-1) FileArray[pFileArray++]=realPath; // else if(realPath.lastIndexOf(".jsp")!=-1) FileArray[pFileArray++]=realPath; } } //end while (iter.hasNext()) }//end while(pDirectoryQueue!=0) //----------------- 依次检索站内的每个网页 ----------------------------- //out.println("站内一共 "+pFileArray+" 个文件

"); //if(1==1) return; int curFileNo=0; int htmcount=0; for(curFileNo=0;curFileNo"); //if(1==1) continue; //if(realPath.equals("D:\\tomcat6\\webapps\\ROOT\\ch6\\testsearch.htm")) return; FileInputStream fis = null; fis = new FileInputStream(realPath); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String filestring=""; String tempstring=""; while((tempstring=br.readLine())!=null) //把网页文件读入字符串 { filestring = filestring + tempstring ; } //------------------- 开始处理 ------------------------------- filestring = filestring.replace(" ",""); //过滤字符串 int loc1 = filestring.indexOf(""); int loc2 = filestring.indexOf(""); String title = "" ; if ( loc1 != -1 && loc2 != -1 ) { title = filestring.substring(loc1+7,loc2); //读取网页中的title项 } int pos1=filestring.indexOf(""); filestring=filestring.substring(pos1,pos2); //取 之间的部分 filestring = filterweb(filestring,'<','>'); //过滤网页中的HTML语句 filestring = filestring.replace(" ",""); //过滤特殊字符 filestring = filestring.replace(" ",""); //过滤特殊字符 filestring = filestring.replace(" ",""); //过滤空格 findstr="和平使命"; String ShowStr=""; int start=-1; boolean FindOK = false ; if((start=filestring.indexOf(findstr))!=-1) //找正文 { pos1=start-80; pos2=start+80; if(pos1<0) pos1=0; if(pos2>filestring.length()) pos2=filestring.length(); ShowStr=filestring.substring(pos1,pos2); ShowStr=ShowStr.replace(findstr,""+findstr+"")+"..."; FindOK=true; } if(!FindOK) //如果在正文中找不到,则找标题 { if(title.indexOf(findstr)!=-1) { ShowStr=title.replace(findstr,""+findstr+""); FindOK=true; } } if(FindOK) { // out.println("
------------------------------------------------------------------------------
"); String ROOTPath=application.getRealPath("/"); String relpath=realPath.replace(ROOTPath,""); //取相对路径 String httpstr="http://"+request.getServerName()+":"+request.getServerPort()+"/"+relpath; httpstr=httpstr.replace("\\","/"); %>
<%=title%>
<%=ShowStr%>


<% } //else out.println("没有找到!

"); //if((start=filestring.indexOf(findstr))!=-1) //if(1==1) return; // if(1==1) return; }//end for(curFileNo=0;curFileNo
"); %>
ssssssssssssssssss22