论坛首页 Java企业应用论坛

关于网页快照的处理方式

浏览 1876 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-03   最后修改:2009-07-03

最近在用Heritriex+Lucene做搜索引擎的项目,中间有一个功能要实现类似于百度和谷歌中"网页快照"的功能;

秀一下我自己的做法吧!

(1):页面中的处理很简单;

var url = "webSnap?url="+url+"&fresh="+Math.random();//我这个是提交给了servlet了。

window.open(url);

(2):servlet的处理方式:

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setCharacterEncoding("UTF-8");
  response.setContentType("text/html");
  //得到页面传递过来的url
  String urlAddress = JspUtils.getParameter(request, "url", "");
  //把提交过来的网页地址直接定位到本地抓取磁盘中对应的页面

  String conLocalAddress = getLocalAddress(urlAddress);

  //获取本地抓取的页面中的编码方式

  String charset = BaeeqUtil.getCharset(new File(conLocalAddress));

  response.setContentType("text/html;charset="+charset+""); 

  //通过字符流的形式读文件 
  String localStr = FileRead(conLocalAddress);
  PrintWriter out = response.getWriter();

  //直接响应输出
  out.write(localStr);
 }

 

 

  /**
  * 读磁盘中某个文件

  * @param fname
  * @return String
  */

 public String FileRead(String fname) {
  String content = new String();
  try {
   FileReader fr = new FileReader(fname);
   BufferedReader br = new BufferedReader(fr);
   String str = new String();
   while((str = br.readLine()) != null) {
    content +=str;
   }
   br.close();
   fr.close();
   } catch(FileNotFoundException e) {
    System.out.println("Connt find " + e);
   } catch(IOException e) {
    System.out.println("Error reading input file" + e);
   }
   return content;
 }

 

 

  /**
  * 获得字符编码
  * @param htmlFile
  * @return
  */
 public static String getCharset(File htmlFile){
  String str = loadFileToString(htmlFile);
  String regex = "charset=(\\w+\\d?)";
  Pattern pattern = Pattern.compile(regex);//编译正则
  Matcher matcher = pattern.matcher(str);//产生正则匹配对象。
  if(matcher.find()){
   String s = matcher.group(1);
   System.out.println(s);
   return s;
  }
  return "UTF8";
 }

 

 

 /***
  * 得到本地磁盘上的mirror的地址
  * @param printStr
  * @return
  */
 public String getLocalAddress(String printStr) {
  return BaeeqUtil.switchUrl2Path(printStr, txtMirrorPath);
 }

 

 

  /**
  * 测试方法
  * @param args
  * @return void
  */

 public static void main(String[]args) {
  String demo = "http://bbs.club.sina.com.cn/tableforum/App/index.php?tree=0&bbsid=272&subid=0";
  System.out.println(BaeeqUtil.switchUrl2Path(demo, "e:\\"));
 }

 

用这个方法做的网页快照。不知道大家还有其他的什么好的办法了没。欢迎指点……………………

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics