Autonomy JAVA 开发

最近刚接触Autonomy java开发。在网上找了很久相关资料,但是重点的资料很少。因此写此篇文章与大家分享,有不对的地方望指导。

首先安装和向IDOL库中导数据就不说了。因为刚接触,就写了一个测试小例子(1个java类,1个JSP页面)。 另外还需要一个Autonomy的jar包一个(autonomyAPI.jar)。

JSP 页面:

<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head><title>AUTONOMY TEST</title></head>
	<body style="font-size: 12px;">
		<form action="q" method="get">
			<input name="w" value="新浪"><input type="submit" value="查询">   <input name="r" size="6" value="6">
		</form>
		<%
			Object obj = request.getAttribute("result");
			String str = obj == null ? "" : obj.toString();
			if(!"".equals(str)){
				%><%=str%><%
			}
		%>
	</body>
</html>


Java 代码(其实就是个servlet):

package com.wxh.autonomy.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.autonomy.client.aciObject;
import com.autonomy.client.client;

public class QueryServlet extends HttpServlet {
	String u = "http://10.1.10.53:9000/a=query&maxresults=20&text=";

	private static final long serialVersionUID = 1L;

	public QueryServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	private String nextHit(aciObject acioEntry) {
		StringBuffer resu = new StringBuffer();
		if (acioEntry != null) {
			String sEntryName = acioEntry.paramGetString(aciObject.ACI_DATA_NODE_NAME);
			if (sEntryName != null && !sEntryName.equals("autn:hit")) {
				if (!"autn:warning".equals(sEntryName)) {
					resu.append("共找到 " + acioEntry.getTagValue("autn:totalhits", "0") + " 条记录,当前显示 " + acioEntry.getTagValue("autn:numhits", "0")
							+ " 条。<br/><br/>");
				}
				acioEntry = acioEntry.findFirstOccurrence("autn:hit");
				resu.append(nextHit(acioEntry));
			} else {
				resu.append("<a href='" + acioEntry.getTagValue("autn:reference") + "' target='_black'>" + acioEntry.getTagValue("autn:title") + "</a><br/>");
				resu.append("    <font size='2'>" + acioEntry.getTagValue("autn:summary") + "</font><br/><br/>");
				resu.append(nextHit(acioEntry.aciObjectNextEntry()));
			}
		}
		return resu.toString();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		response.setContentType("text/html;charset=GBK");
		client client = new client();
		aciObject aciConnection = client.aciObjectCreate(aciObject.ACI_CONNECTION);
		aciConnection.paramSetString(aciObject.ACI_HOSTNAME, "localhost");
		aciConnection.paramSetString(aciObject.ACI_PORTNUMBER, "9000");
		aciConnection.paramSetString(aciObject.ACI_CONN_SENDENCODING, "GBK");
		aciConnection.paramSetString(aciObject.ACI_CONN_RECVENCODING, "GBK");
		aciConnection.setTimeout(6000);

		aciObject acioCommand = client.aciObjectCreate(aciObject.ACI_COMMAND);
		acioCommand.paramSetBool(aciObject.ACI_COM_USE_POST, true);
		// set command
		acioCommand.paramSetString(aciObject.ACI_COM_COMMAND, "query");// 设置command要执行的命令
		acioCommand.paramSetBool(aciObject.ACI_COM_USE_POST, true); // 设置http请求是否采用POST方法,默认是POST方法

		// Set query parameters
		acioCommand.paramSetString("OutputEncoding", "ChineseSimplified");
		String text = request.getParameter("w");
		String r = request.getParameter("r");
		if (text != null) {
			text = text.replaceAll("\\%", "");

			acioCommand.paramSetString("text", new String(text.getBytes("GBK"), "ISO-8859-1"));// 要查询的文本
			acioCommand.paramSetString("minscore", String.valueOf(90));// 最小相关性
			acioCommand.paramSetString("maxresults", (r == null || "".equals(r)) ? "6" : r);// 每页显示数据
			acioCommand.paramSetBool("totalresults", true);// 返回结果xml中是否显示总数据数量
			acioCommand.paramSetBool("predict", false);// 返回的总记录数是否为估算值(否表示为精确数字)
			acioCommand.paramSetString("languagetype", "chineseCHINESESIMPLIFIED");
			acioCommand.paramSetString("characters", "300");// 摘要最多包含几个字符
			acioCommand.paramSetString("fieldrestriction", "dretitle");

			acioCommand.paramSetString("Sentences", "3");// 摘要最多包括几个句子
			acioCommand.paramSetBool("SpellCheck", true); //拼写检查
			acioCommand.paramSetString("Summary", "context");// 设定摘要的类型
			acioCommand.paramSetInt("Start", 1);//开始页

			// acioCommand.paramSetString("OutputEncoding", "GBK");
			acioCommand.paramSetString("Combine", "simple");
			acioCommand.paramSetString("Highlight", "terms");// 设定高亮显示的类型
			acioCommand.paramSetString("AnyLanguage", "true");// 是否在所有语言中匹配结果
			// acioCommand.paramSetString("databasematch", "oracle"); // 查询的目标数据库,默认为全部
			acioCommand.paramSetString("printfields", "CATEGORY+DREDATE");
			acioCommand.paramSetString("querysummary", "true");// 是否对查询结果生成总的摘要
			// acioCommand.paramSetString("print", "noresults");//不显示结果集
			acioCommand.paramSetString("print", "all");// 结果集显示全部
			// acioCommand.paramSetString("DontMatchReference","");
			if (aciConnection.isAlive()) {
				aciObject aciResponse = aciConnection.aciObjectExecute(acioCommand);
				if (aciResponse != null) {
					if (aciResponse.checkForSuccess()) {
						request.setAttribute("result", nextHit(aciResponse.findFirstOccurrence("autnresponse")));
					} else {
						System.out.println("Request failed.");
					}
				}
			} else {
				System.out.println("Could not connect to server");
			}
		}
		request.getRequestDispatcher("index.jsp").forward(request, response);
		// doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String text = request.getParameter("w");
		if (text != null) {
			URL url = new URL(u + text);
			HttpURLConnection urlC = (HttpURLConnection) url.openConnection();
			// urlC.setDoOutput(true);
			// urlC.setRequestMethod("GET");
			// urlC.setRequestProperty("Content-type", "text/xml");
			// urlC.setRequestProperty("Accept-Charset", "GBK");
			// urlC.setRequestProperty("contentType", "GBK");
			BufferedReader bs = new BufferedReader(new InputStreamReader(urlC.getInputStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			String str = null;
			while ((str = bs.readLine()) != null) {
				sb.append(str);
				System.out.println("str=" + str);
			}
			request.setAttribute("result", sb);
		}
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}

由代码可以看出,点击页面上的查询会进servlet的doGet方法。


运行结果:



至此:本文完,欢迎给出宝贵的意见!


ps:autonomyAPI.jar 包下载 地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值