<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest topics for the forum "網頁程式設計 Web Development"]]></title>
		<link>https://forum.andowson.com/forums/show/5.page</link>
		<description><![CDATA[The newest discussed topics in the forum "網頁程式設計 Web Development"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>回覆:JSP精選實用範例(二):檔案上傳</title>
				<description><![CDATA[ dream1978您好: 
<br>
 請在Eclipse的Project名稱上面點一下滑鼠右鍵，點選Properties會跳出一個對話框，調整這邊的Java Compiler設定看看。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/197/1392.page</guid>
				<link>https://forum.andowson.com/posts/preList/197/1392.page</link>
				<pubDate><![CDATA[Fri, 23 Jun 2017 13:38:01]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>jQuery圖形動態鍵盤使用範例</title>
				<description><![CDATA[ 下面介紹jQuery Keypad的使用 (網站：http://keith-wood.name/keypad.html)，範例畫面如附件圖1 
<br>
<br>
【範例】 
<br>
Step 1：至http://keith-wood.name/keypad.html下載整包套件，主要使用到的檔案有以下幾個： 
<br>
[code]jquery.plugin.js 
<br>
jquery.keypad.js 
<br>
jquery.keypad.alt.css 
<br>
keypad.png[/code] 
<br>
<br>
Step2：編輯html，新增二個文字輸入方塊，帳號及密碼 
<br>
[code] 
<br>
<br>
<br>
<p></p>
<br>
<p></p>
<br>
<br>
[/code] 
<br>
<br>
Step 3：將jquery.keypad.alt.css、jquery.plugin.js、jquery.keypad.js加入至html中，另外需加入google api提供的jquery.min.js。(PS.如果網站為https，則需將google api網址也改為https) 
<br>
[code] 
<br>
<br>
<br>
[/code] 
<br>
<br>
Step 4：在html中編輯javascript 
<br>
[code][/code] 
<br>
註1：如果想要使每次鍵盤上的文字位置不同，除上述方式外(randomiseOther: true, randomiseNumeric: true, randomiseAlphabetic: true, randomiseOther: true)，也可改用 randomiseAll: true，但數字鍵就不會固定在右方區塊。 
<br>
註2：「showOn: 'button', buttonImageOnly: true, buttonImage: 'keypad.png'」：點選鍵盤圖示才顯示動態鍵盤，若想改為當游標出現在輸入框時也顯示，則改成「showOn: 'both', buttonImage: 'keypad.png'」。 
<br>
註3：「showAnim: ''」：指鍵盤顯示時不使用特效。 
<br>
<br>
<br>
Step 5：如想更換鍵盤外觀，可使用google api提供的主題，先加入以下css，以smoothness為例 
<br>
[code][/code] 
<br>
<br>
其他主題可參考jQuery keypad網站之Styling頁籤中，「ThemeRoller styling:」的地方，將上面css網址改為其他名稱即可，例如欲改為blitzer，則修改成下面的路徑： 
<br>
[code]http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/blitzer/jquery-ui.css[/code] 
<br>
<br>
 再加入以下參數至.keypad()中，結果如附件圖2 
<br>
[code]useThemeRoller: true[/code] 
<br>
<br>
完整html如下： 
<br>
[code] 
<br>
<br>
<br>
<br>
jQuery Keypad 
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p></p>
<br>
<p></p>
<br>
<br>
[/code] 
<br>
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/642/1341.page</guid>
				<link>https://forum.andowson.com/posts/preList/642/1341.page</link>
				<pubDate><![CDATA[Wed, 14 Sep 2016 16:12:22]]> GMT</pubDate>
				<author><![CDATA[ bious3927]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(四):檔案傳輸</title>
				<description><![CDATA[ johnny0917您好: 
<br>
問題一：Windows Batch File 只能呼叫外部指令來執行ftp，例如ftp.exe，所以原則上是不行的，不過您可以參考這篇的做法: 
<br>
http://www.andowson.com/posts/list/42.page 
<br>
<br>
問題二：JSP開頭的函式庫是做引入(import)的動作，讓底下的Java Object可以被編譯器解析到，這些函式庫跟寫Java程式一樣的用法，一般來說以 java. 開頭的就是Java程式語言內建的函式庫，非 java. 開頭的就要另外配置對應的 .jar 檔到作業系統的 CLASSPATH 上。如果您不熟悉Java程式，那可能要先去看一下相關的入門文章。 
<br>
<br>
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/199/1320.page</guid>
				<link>https://forum.andowson.com/posts/preList/199/1320.page</link>
				<pubDate><![CDATA[Sat, 8 Nov 2014 17:51:22]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>遇到Tomcat錯誤訊息:嚴重: Error listenerStart的除錯方法</title>
				<description><![CDATA[ 今天遇到這個錯誤訊息: 
<br>
[code=text]九月 26, 2014 8:30:56 上午 org.apache.catalina.core.StandardContext startInternal 
<br>
嚴重: Error listenerStart[/code] 
<br>
Tomcat除了顯示這些以外，沒有任何更進一步的資訊可以知道是哪個listener出了問題，或在哪邊出了什麼問題。 
<br>
網路上找到一篇文章的說明，可以在該webapp的WEB-INF/classes目錄下建立一個logging.properties檔，裡面填寫這幾行即可: 
<br>
[code]org.apache.catalina.core.ContainerBase.[Catalina].level = INFO 
<br>
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler[/code] 
<br>
重新啟動Tomcat時就可以看到錯誤訊息了。 
<br>
<br>
參考資料: 
<br>
http://blog.trifork.com/2011/03/18/debugging-the-dreaded-severe-error-listenerstart-and-severe-error-filterstart-tomcat-error-messages/]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/629/1318.page</guid>
				<link>https://forum.andowson.com/posts/preList/629/1318.page</link>
				<pubDate><![CDATA[Sat, 27 Sep 2014 00:03:54]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(三):檔案下載</title>
				<description><![CDATA[ andowson你好我稍早所描述的問題已經解決了，不好意思打擾你了，感恩！！]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/198/1278.page</guid>
				<link>https://forum.andowson.com/posts/preList/198/1278.page</link>
				<pubDate><![CDATA[Thu, 24 Jan 2013 16:46:46]]> GMT</pubDate>
				<author><![CDATA[ Siao]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(五):抓取網頁</title>
				<description><![CDATA[ 如果是在公司內部環境使用HttpClient 4去連外部網站須走proxy才能通時， 
<br>
將原來這行: 
<br>
[code]HttpClient httpclient = new DefaultHttpClient();[/code] 
<br>
修改為(假設proxy是http://10.160.3.88:8080/): 
<br>
[code]HttpHost proxy = new HttpHost("10.160.3.88", 8080); 
<br>
HttpClient httpclient = new DefaultHttpClient(); 
<br>
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);[/code] 
<br>
同時補上宣告: 
<br>
[code]&lt;% @page import="org.apache.http.HttpHost" %&gt; 
<br>
&lt;% @page import="org.apache.http.conn.params.ConnRoutePNames" %&gt;[/code] 
<br>
即可。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/215/1226.page</guid>
				<link>https://forum.andowson.com/posts/preList/215/1226.page</link>
				<pubDate><![CDATA[Fri, 11 May 2012 17:52:17]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>Re:Help me level menu ...</title>
				<description><![CDATA[ Wow, thank you very much andowson :oops:]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/574/1212.page</guid>
				<link>https://forum.andowson.com/posts/preList/574/1212.page</link>
				<pubDate><![CDATA[Mon, 5 Mar 2012 15:50:59]]> GMT</pubDate>
				<author><![CDATA[ newbiejforum]]></author>
			</item>
			<item>
				<title>have problem menu and sub menu in java servlet + freemarker ...</title>
				<description><![CDATA[ Hi, i am learning java servlet + freemarker, and i have a problem like this subject, 
<br>
This is my source, i want to show all categories in browser like file categories.txt in file testmenu.zip, how can i do next step?? my english is bad :oops: , i need your help ... 
<br>
<br>
this is my testmenu.zip 
<br>
<br>
http://www.mediafire.com/?qvq9r3wjg27dhch 
<br>
<br>
thank you very much :oops: 
<br>
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/573/1207.page</guid>
				<link>https://forum.andowson.com/posts/preList/573/1207.page</link>
				<pubDate><![CDATA[Fri, 3 Feb 2012 00:23:49]]> GMT</pubDate>
				<author><![CDATA[ newbiejforum]]></author>
			</item>
			<item>
				<title>回覆:如何用程式寫出一個文字檔案，編碼為Big-5</title>
				<description><![CDATA[ [code]Signal += "年度號：" + node.getChildText("年度號")+lineSep+ "分類號：" + node.getChildText("分類號")+lineSep+ "案次號：" + node.getChildText("案次號")+lineSep;[/code] 
<br>
<br>
區域變數 node 已超出使用範圍拉, 只能用在for回圈中!! 
<br>
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/572/1206.page</guid>
				<link>https://forum.andowson.com/posts/preList/572/1206.page</link>
				<pubDate><![CDATA[Thu, 2 Feb 2012 11:25:57]]> GMT</pubDate>
				<author><![CDATA[ bious3927]]></author>
			</item>
			<item>
				<title>回覆:關於try catch 的問題?</title>
				<description><![CDATA[ 依我的了解，您應該是想要在輸入的內容是空白或是不存在時都顯示一個錯誤訊息來提醒使用者吧？ 
<br>
<br>
設計的想法是： 
<br>
利用一個boolean變數found，初始值為false，然後比對輸入的字串s，如果不是null時程式會利用for迴圈拜訪每個XML文件樹的節點，如果內容等於我們讀取到的分類號時，才將found設為true，並輸出結果，並等待使用者按下Enter才顯示下一筆。 
<br>
以下是參考範例ReadXMLFile.java: 
<br>
[code] 
<br>
import java.io.*; 
<br>
import java.io.File; 
<br>
import java.io.IOException; 
<br>
import java.util.Scanner; 
<br>
import java.util.List; 
<br>
<br>
import org.jdom.Document; 
<br>
import org.jdom.Element; 
<br>
import org.jdom.JDOMException; 
<br>
import org.jdom.input.SAXBuilder; 
<br>
<br>
public class ReadXMLFile { 
<br>
 public static void main(String[] args) throws IOException { 
<br>
<br>
 SAXBuilder builder = new SAXBuilder(); 
<br>
 File xmlFile = new File("C:\\JAVA_WORK\\test1.xml"); 
<br>
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
<br>
 System.out.print("輸入分類號："); 
<br>
 String s = br.readLine(); 
<br>
 boolean found = false; 
<br>
<br>
 if (s != null) { 
<br>
 try { 
<br>
 Document document = (Document) builder.build(xmlFile); 
<br>
 Element rootNode = document.getRootElement(); 
<br>
 List list = rootNode.getChildren("ROW"); 
<br>
<br>
 for (int i = 0; i &lt; list.size(); i++) 
<br>
 { 
<br>
 Element node = (Element) list.get(i); 
<br>
<br>
 if (node.getChildText("分類號").equals(s)) 
<br>
 { 
<br>
 System.out.println("年度號 : " + node.getChildText("年度號")); 
<br>
 System.out.println("分類號 : " + node.getChildText("分類號")); 
<br>
 System.out.println("案次號 : " + node.getChildText("案次號")); 
<br>
 Scanner input = new Scanner(System.in); 
<br>
 input.hasNextLine(); 
<br>
 found = true;; 
<br>
 } 
<br>
 } 
<br>
 } catch (IOException io) { 
<br>
 System.out.println(io.getMessage()); 
<br>
 } catch (JDOMException jdomex) { 
<br>
 System.out.println(jdomex.getMessage()); 
<br>
 } 
<br>
 } 
<br>
 if (!found) { 
<br>
 System.out.println("查無此分類號！請重新輸入..."); 
<br>
 } 
<br>
 } 
<br>
} 
<br>
[/code] 
<br>
以下是參考用的輸入檔test1.xml: 
<br>
[code=xml] 
<br>
&lt;ROWSET&gt; 
<br>
 &lt;ROW num="1"&gt; 
<br>
 &lt;年度號&gt;0080&lt;/年度號&gt; 
<br>
 &lt;分類號&gt;010302&lt;/分類號&gt; 
<br>
 &lt;案次號&gt;002&lt;/案次號&gt; 
<br>
 &lt;/ROW&gt; 
<br>
 &lt;ROW num="2"&gt; 
<br>
 &lt;年度號&gt;0078&lt;/年度號&gt; 
<br>
 &lt;分類號&gt;011602&lt;/分類號&gt; 
<br>
 &lt;案次號&gt;002&lt;/案次號&gt; 
<br>
 &lt;/ROW&gt; 
<br>
 &lt;ROW num="3"&gt; 
<br>
 &lt;年度號&gt;0070&lt;/年度號&gt; 
<br>
 &lt;分類號&gt;019302&lt;/分類號&gt; 
<br>
 &lt;案次號&gt;011&lt;/案次號&gt; 
<br>
 &lt;/ROW&gt; 
<br>
 &lt;ROW num="1225"&gt; 
<br>
 &lt;年度號&gt;0099&lt;/年度號&gt; 
<br>
 &lt;分類號&gt;018719&lt;/分類號&gt; 
<br>
 &lt;案次號&gt;111&lt;/案次號&gt; 
<br>
 &lt;/ROW&gt; 
<br>
&lt;/ROWSET&gt; 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/571/1204.page</guid>
				<link>https://forum.andowson.com/posts/preList/571/1204.page</link>
				<pubDate><![CDATA[Thu, 26 Jan 2012 18:24:03]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JAVA讀取XML一問 for JDOM</title>
				<description><![CDATA[ 您可以參考一下這個連結的說明： 
<br>
http://caterpillar.onlyfun.net/Gossip/JavaEssence/WhatClasspath.html 
<br>
<br>
先弄清楚什麼是CLASSPATH後，這個問題您應該就可以自行處理了。]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/570/1202.page</guid>
				<link>https://forum.andowson.com/posts/preList/570/1202.page</link>
				<pubDate><![CDATA[Tue, 24 Jan 2012 15:16:35]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JSP精選實用範例(一):郵件發送</title>
				<description><![CDATA[ [color=red]寄件者[/color]只能有一個，可以透過Email類別的setFrom的method來設定，可接受三種參數呼叫方式: 
<br>
[code]setFrom(String email) 
<br>
setFrom(String email, String name) 
<br>
setFrom(String email, String name, String charset)[/code] 
<br>
如果要[color=blue]收件者[/color]，可以透過Email類別的addTo的method來增加，類似setFrom有三種參數呼叫方式: 
<br>
[code]addTo(String email) 
<br>
addTo(String email, String name) 
<br>
addTo(String email, String name, String charset)[/code] 
<br>
收件者可以有多個，只要連續呼叫多次的addTo即可(例如透過迴圈)或者先將收件者整理成一個清單(List)，再呼叫setTo一次設定好 
<br>
[code]setTo(Collection aCollection)[/code] 
<br>
<br>
至於您的錯誤訊息，可能是charset編碼問題，也可能是其他問題，得看到程式碼才能得知。 
<br>
<br>
<br>]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/196/1184.page</guid>
				<link>https://forum.andowson.com/posts/preList/196/1184.page</link>
				<pubDate><![CDATA[Mon, 19 Dec 2011 12:05:17]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:如何用httpclient4去存取SSL的網頁資料?</title>
				<description><![CDATA[ 如果要輸出檔案的話,也非常簡便,可以使用HttpEntity 提供的方法: writeTo() 
<br>
<br>
比以下常用的寫法節省不少程式, 也避免了特殊字無法正常輸出的情況: 
<br>
[code] 
<br>
FileOutputStream fos = new FileOutputStream(request.getParameter("path")); 
<br>
ByteArrayInputStream bais = new ByteArrayInputStream(httpclient.execute(httpget, responseHandler).getBytes()); 
<br>
<br>
byte[] bytes = new byte[1024]; 
<br>
int count = 0; 
<br>
<br>
while (bais!=null &amp;&amp; (count = bais.read(bytes))!=-1) { 
<br>
 fos.write(bytes, 0, count); 
<br>
} 
<br>
[/code] 
<br>
<br>
可於版主提供的程式105行中,加入以下程式 
<br>
<br>
[code] 
<br>
entity.writeTo(new FileOutputStream(request.getParameter("path"))); 
<br>
[/code]]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/484/1018.page</guid>
				<link>https://forum.andowson.com/posts/preList/484/1018.page</link>
				<pubDate><![CDATA[Thu, 2 Jun 2011 15:01:07]]> GMT</pubDate>
				<author><![CDATA[ bious3927]]></author>
			</item>
			<item>
				<title>回覆:iText在產生pdf檔時，遇到一些特殊中文字(例如:珉、峯、喆等)，都會有unicode字碼(&amp;#xxxxx;)輸出，該怎麼辦?</title>
				<description><![CDATA[ 我猜測這個現象會發生的原因可能如下， 
<br>
1.如果資料庫內碼是使用Big5編碼時，透過網頁上輸入的中文字(UTF-8)進入到應用伺服器時，被轉了一次碼(ISO-8859-1)，再存到資料庫時，又轉了一次碼(Big5)，例如下列的順序： 
<br>
UTF-8-&gt;ISO-8859-1-&gt;Big5 
<br>
當UTF-8轉成ISO-8859-1時，Unicode被轉成10進制的HTML Entity格式（如珉），然後跟其他可以轉成Big5的字元一起存到資料庫去。 
<br>
<br>
2.等到從資料庫取回這個字串時，就會得到一個夾雜著HTML Entity格式的Big5字串，如果把這個字串直接傳給iText 去產生 pdf 檔，這些HTML Entity字串應該就會當做英文字母一樣輸出。於是就產生類似您所附上的sample.pdf 般的內容了。 
<br>
<br>
好，了解原因後，要解決這個問題就很簡單了，只要在步驟2時補上將HTML Entity字串轉碼回原始編碼，再傳給iText去產生pdf即可。而要將HTML Entity字串轉碼回原始編碼只要呼叫 
<br>
[url=http://commons.apache.org/lang/]Apache Commons Lang[/url]的[url=http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html#unescapeHtml(java.lang.String)]StringEscapeUtils.unescapeHtml(java.lang.String)[/url] method即可。 
<br>
<br>
在此以修改[url=http://itextpdf.com/book/index.php]iText in Action 2nd Edition[/url]所提供的[url=http://examples.itextpdf.com/src/part1/chapter01/HelloWorld.java]HelloWorld.java[/url]作為範例 
<br>
HelloWorld.java: 
<br>
[code] 
<br>
/* 
<br>
 * This class is part of the book "iText in Action - 2nd Edition" 
<br>
 * written by Bruno Lowagie (ISBN: 9781935182610) 
<br>
 * For more info, go to: http://itextpdf.com/examples/ 
<br>
 * This example only works with the AGPL version of iText. 
<br>
 */ 
<br>
<br>
package com.andowson.pdf; 
<br>
<br>
import java.io.FileOutputStream; 
<br>
import java.io.IOException; 
<br>
<br>
import org.apache.commons.lang.StringEscapeUtils; 
<br>
<br>
import com.itextpdf.text.Document; 
<br>
import com.itextpdf.text.DocumentException; 
<br>
import com.itextpdf.text.Font; 
<br>
import com.itextpdf.text.Paragraph; 
<br>
import com.itextpdf.text.pdf.BaseFont; 
<br>
import com.itextpdf.text.pdf.PdfWriter; 
<br>
<br>
/** 
<br>
 * First iText example: Hello World. 
<br>
 */ 
<br>
public class HelloWorld { 
<br>
<br>
 /** Path to the resulting PDF file. */ 
<br>
 public static final String RESULT 
<br>
 = "D:/hello.pdf"; 
<br>
<br>
 /** 
<br>
 * Creates a PDF file: hello.pdf 
<br>
 * @param args no arguments needed 
<br>
 */ 
<br>
 public static void main(String[] args) 
<br>
 throws DocumentException, IOException { 
<br>
 new HelloWorld().createPdf(RESULT); 
<br>
 } 
<br>
<br>
 /** 
<br>
 * Creates a PDF document. 
<br>
 * @param filename the path to the new PDF document 
<br>
 * @throws DocumentException 
<br>
 * @throws IOException 
<br>
 */ 
<br>
 public void createPdf(String filename) 
<br>
 throws DocumentException, IOException { 
<br>
 // step 1 
<br>
 Document document = new Document(); 
<br>
 // step 2 
<br>
 PdfWriter.getInstance(document, new FileOutputStream(filename)); 
<br>
 // step 3 
<br>
 document.open(); 
<br>
<br>
 // Assumes that source string comes from database 
<br>
 String source = "測試物品-珉彣峯喆献"; 
<br>
 // Unescapes the Unicode characters from HTML entities (decimal format) to string 
<br>
 String output = StringEscapeUtils.unescapeHtml(source); 
<br>
<br>
 // Specifies the file path to the Chinese font file 
<br>
 BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/kaiu.ttf", 
<br>
 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
<br>
 Font font = new Font(bfChinese, 12); // The font size is 12. 
<br>
<br>
 // step 4 
<br>
 document.add(new Paragraph(output, font)); 
<br>
 // step 5 
<br>
 document.close(); 
<br>
 } 
<br>
} 
<br>
[/code] 
<br>
<br>
下載函式庫： 
<br>
http://itextpdf.com/ 
<br>
http://commons.apache.org/lang/ 
<br>
<br>
參考網址： 
<br>
* Unicode 編碼字集 
<br>
http://theorem.ca/~mvcorks/cgi-bin/unicode.pl.cgi?start=4E00&amp;end=9FFF 
<br>
* 解決iText輸出中文問題 
<br>
http://blog.yam.com/rexmen/article/1888474 
<br>
* Hello World example 
<br>
http://itextpdf.com/examples/iia.php?id=12]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/483/1008.page</guid>
				<link>https://forum.andowson.com/posts/preList/483/1008.page</link>
				<pubDate><![CDATA[Tue, 17 May 2011 23:24:47]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
			<item>
				<title>回覆:JAVA實作Search 一問 for Lucene</title>
				<description><![CDATA[ 關於您問到的「如果在搜尋裡面增加，所搜尋到的文件且呈現內文應該怎麼打？ 」 
<br>
我想可以用高亮方式來處理： 
<br>
Searcher.java: 
<br>
[code] 
<br>
import java.io.BufferedInputStream; 
<br>
import java.io.File; 
<br>
import java.io.FileInputStream; 
<br>
import java.io.IOException; 
<br>
import java.io.StringReader; 
<br>
<br>
import org.apache.lucene.analysis.TokenStream; 
<br>
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
<br>
import org.apache.lucene.document.Document; 
<br>
import org.apache.lucene.queryParser.ParseException; 
<br>
import org.apache.lucene.queryParser.QueryParser; 
<br>
import org.apache.lucene.search.IndexSearcher; 
<br>
import org.apache.lucene.search.Query; 
<br>
import org.apache.lucene.search.ScoreDoc; 
<br>
import org.apache.lucene.search.TopDocs; 
<br>
import org.apache.lucene.search.highlight.Highlighter; 
<br>
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException; 
<br>
import org.apache.lucene.search.highlight.QueryScorer; 
<br>
import org.apache.lucene.search.highlight.Scorer; 
<br>
import org.apache.lucene.search.highlight.SimpleHTMLFormatter; 
<br>
import org.apache.lucene.store.Directory; 
<br>
import org.apache.lucene.store.SimpleFSDirectory; 
<br>
import org.apache.lucene.util.Version; 
<br>
<br>
public class Searcher { 
<br>
 public static void main(String[] args) throws IOException, ParseException, InvalidTokenOffsetsException { 
<br>
 String Idx = "C:\\test\\Idx"; 
<br>
 String dataDir = "C:\\test\\Data"; 
<br>
 Directory dir = new SimpleFSDirectory(new File(Idx)); 
<br>
 IndexSearcher indexSearch = new IndexSearcher(dir); 
<br>
<br>
 QueryParser queryParser = new QueryParser(Version.LUCENE_30, 
<br>
 "contents", new StandardAnalyzer(Version.LUCENE_30)); 
<br>
 Query query = queryParser.parse("鼻涕"); // key Query term 
<br>
 TopDocs hits = indexSearch.search(query, 500); 
<br>
 System.out.println("找到" + hits.totalHits + "個"); 
<br>
 for (int i = 0; i &lt; hits.scoreDocs.length; i++) { 
<br>
 ScoreDoc sdoc = hits.scoreDocs[i]; 
<br>
 Document doc = indexSearch.doc(sdoc.doc); 
<br>
 System.out.println(doc.get("filename")); 
<br>
 Scorer scorer = new QueryScorer(query); 
<br>
 SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color="\&quot;red\&quot;">", "</font></b>"); 
<br>
 Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer); 
<br>
 String content = readFileAsString(dataDir+File.separator+doc.get("filename")); 
<br>
 TokenStream tokenStream = queryParser.getAnalyzer().tokenStream( 
<br>
 "contents", new StringReader(content)); 
<br>
<br>
 String fragment = highlighter.getBestFragment(tokenStream, content); 
<br>
 System.out.println(fragment != null ? fragment : content); 
<br>
<br>
 } 
<br>
 indexSearch.close(); 
<br>
 } 
<br>
 private static String readFileAsString(String filePath) throws java.io.IOException{ 
<br>
 byte[] buffer = new byte[(int) new File(filePath).length()]; 
<br>
 BufferedInputStream f = null; 
<br>
 try { 
<br>
 f = new BufferedInputStream(new FileInputStream(filePath)); 
<br>
 f.read(buffer); 
<br>
 } finally { 
<br>
 if (f != null) try { f.close(); } catch (IOException ignored) { } 
<br>
 } 
<br>
 return new String(buffer); 
<br>
 } 
<br>
} 
<br>
[/code] 
<br>
需另外加入下列jar檔到CLASSPATH: 
<br>
lucene-3.0.3\contrib\highlighter\lucene-highlighter-3.0.3.jar 
<br>
lucene-3.0.3\contrib\memory\lucene-memory-3.0.3.jar]]></description>
				<guid isPermaLink="true">https://forum.andowson.com/posts/preList/467/966.page</guid>
				<link>https://forum.andowson.com/posts/preList/467/966.page</link>
				<pubDate><![CDATA[Thu, 20 Jan 2011 19:21:53]]> GMT</pubDate>
				<author><![CDATA[ andowson]]></author>
			</item>
	</channel>
</rss>