在项目中集成 Google Desktop,提供全文检索能力 (转)(原文http://wakan.blog.51cto.com/59583/d-7) 2006-03-13 21:40:00 标签: desktop google 全文检索 在项目中,需要对客户资料和文件进行全文检索。考虑到 Google Desktop 强大的检索能力,决定把它集成到项目中,从而简化项目方案,加快进度。 1、用户从 Client 端浏览器中发出查询请求,例如: “网络管理” 2、search.jsp 构建出特殊的 URL,访问 Google Desktop: “” 其中: (1)127.0.0.1 是本机的 IP 地址,4664 是 Google Desktop 的监听端口号; (2)“search&s=C4gKVsZ-bAyBaEwxsW2FKoAAqr0”是 Google Desktop 的特殊参数,注:“s=”后边的内容,不同的机器内容不一样,暂且称之为 serial,下文中有说明; (3)“q=%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86”是被搜索的关键词,“网络管理”的 UTF-8 编码,再经过 java.net.URLEncoder.encode() 之后的结果; 3、Google Desktop 从本地硬盘上(假定是 E:\Docs)的资料中检索出符合条件的数据;(实际上这是事先就建好索引,并不是被调用时再去读取的) 4、Google Desktop 把结果返回给 search.jsp。这是一个 UTF-8 编码的大字符串; 5、search.jsp 把检索结果进行一系列处理后(例如去掉 Google 的 LOGO、权限过滤、格式调整、文件名变换、超级链接重定位等),返回给客户端浏览器。有一件事是要做的:把检索结果中形如“E:\Docs\abc.doc”的文件路径换成“/getfile.jsp?filename=abc.doc”,否则客户端打不开该链接; 6、客户单击了那些他感兴趣的文档资料的链接,该链接形如: “” 7、getfile.jsp 从本地硬盘(E:\Docs)中读出 abc.doc。(在读出之前,可以加入权限判断功能,检查当前用户是否具备访问 abc.doc 的权限) 8、getfile.jsp 以二进制的方式把文件内容(abc.doc)返回给 Client 端浏览器,浏览器将根据文件的类型自动调用相关联的 Windows 程序(MS Word)打开文件。 本方案的关键之处是在第 2 步中构建合理的 URL。而该 URL 中,比较困难的是 “search&s=C4gKVsZ-bAyBaEwxsW2FKoAAqr0”。该内容在不同的机器上,可能不一样,但在同一台机器上,却是固定的,所以只需要手工寻找一次即可。 寻找该 serial 的方法是: 1、在安装了 Google Desktop 的服务器上,双击屏幕右下角的 Google Desktop 图标,系统将自动打开 IE,并出现桌面搜索的主页面;同时在 IE 的地址栏中将出现形如“”的 URL; 2、在主页面上的空白处单击鼠标右键,“查看源文件”,并查找类似于“<FORM name=f method=GET action='/search&s=C4gKVsZ-bAyBaEwxsW2FKoAAqr0'>”的代码,看见了吧?“action=”后边的那一串,就是要寻找的目标; 本演示方案中,共涉及两个 JSP 文件: 1、search.jsp:负责构建正确的 URL,并对返回的结果作出分析、过滤、替换、权限检查等操作; 2、getfile.jsp:负责检查权限,然后从硬盘上读取相关的文件资料,返回给客户端; 由于我还不太会用赛迪的 BLOG,不知道如何实现图文混排(试了一次,好象不成功),所以在此只给出关键代码的示例,search.jsp 可仿照着写: import java.io.InputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.net.HttpURLConnection; import java.net.URL; public class GoogleDesktopConnect { /* 这就是双击屏幕右下角的 Google Desktop 图标后,出现在 IE 地址栏中的内容 */ private static String urlInit = ""; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String word = "网络管理"; GoogleDesktopConnect google = new GoogleDesktopConnect(); google.connect(urlInit, word); } private void connect(String strStartURL, String word){ try { /* 从首页中自动分析出 search&s=C4gKVsZ-bAyBaEwxsW2FKoAAqr0*/ String serial = getSerial(strStartURL); System.out.println("serial=" + serial); /* 构建合适的 URL */ String newWord = java.net.URLEncoder.encode(word, "UTF-8"); String strUrl = "" + serial + "&q=" + newWord; System.out.println("\n" + strUrl + "\n"); /* 连接 Google Desktop */ URL url = new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(false); con.setDoInput(true); con.setRequestMethod("GET"); /* 重要!如果不设置代理,Google Desktop 将返回错误信息 */ con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); con.connect(); /* 从 Google Desktop 中接收检索结果 */ InputStream is = con.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b = is.read(); while (b >= 0) { bos.write(b); b = is.read(); } String result = bos.toString("UTF-8"); con.disconnect(); // 接下来可以对 result 进行各种处理,例如权限检查、内容过滤、格式转换、URL 地址转换等 System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } /** 根据 urlInit,读取 Google Desktop 的初始页面,并从页面中分析出 serial */ private String getSerial(String strUrl) { String serial = null; try { URL url = new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(false); con.setDoInput(true); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); con.connect(); InputStream is = con.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int b = is.read(); while (b >= 0) { bos.write(b); b = is.read(); } String helloPage = bos.toString("UTF-8"); System.out.println(helloPage); /* 查找 action='/server&s= */ int p = helloPage.indexOf("action='/search&s="); /* 得到 serial */ serial = helloPage.substring(p+18, p+45); } catch (Exception e) { e.printStackTrace(); } return serial; } }http://127.0.0.1:4664/search&s=http://127.0.0.1:4664/&s=UNBXCjdtJHM6yrJXNAfPo4xw6eQhttp://127.0.0.1:4664/&s=UNBXCjdtJHM6yrJXNAfPo4xw6eQhttp://server:8080/getfile.jsp?filename=abc.dochttp://127.0.0.1:4664/search&s=C4gKVsZ-bAyBaEwxsW2FKoAAqr0&q=%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86http://server:8080/search.jsp?keyword= 本文出自 51CTO.COM技术博客