首先声明一下:小弟不是为了别的,只是想让大家多提点意见,比如代码方面啊,等等!好了 不说那么多了!
注意:由于小弟的美工不太好,小弟的这个博客系统模仿了 网上某位大侠博客系统的页面美工!
声明:由于接下来小弟忙于别的事情,所以没有对项目代码进行优化,所以有很多重复性代码!所以我认为代码确实存在很多缺点!
下面图片是主页的截图
下面图片是日志模块的截图
下图是留言模块的截图( 留言模块利用到struts2+json+jquery实现ajax异步技术实现)
下图是图片显示的截图模块( 主页是利用thickbox实现浏览相片功能,和利用纯js实现防百度的相册和ajax评论)
防百度显示相册功能:
下图是音乐播放模块( 这个模块主要防网易的音乐播放模块)
音乐播放是调用网易的音乐播放器
下图是利用lucene搜索日志高亮显示模块( 主要是利用lucene技术进行实现的高亮显示和搜索)
文章显示模块:( 相关文章提示主要是利用lucene对题目进行分词,在进行对索引进行查询)
程序下载模块:( 这个模块主要是上传项目供用户下载)
后台模块
登陆界面:
日志添加:(主要利用fckeditor编辑器实现)
就贴这么多了!
代码总体结构如下.
贴上部分代码:(lucene索引的建立和查询)
package cn.ljzblog.ljz.util; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import jeasy.analysis.MMAnalyzer; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.highlight.Fragmenter; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import test.LuceneJEAnalyzerText; import cn.ljzblog.ljz.model.Article; import cn.ljzblog.ljz.model.LogQueryTemp; import cn.ljzblog.ljz.model.PicQueryTemp; /** * @theme 主要是封装lucene索引和查询索引的方法 * @author ljz * @since 2010/10/10 */ public class LuceneQuery { private static String indexPath;// 索引生成的目录 private static IndexWriter indexWriter;// private IndexSearcher searcher; private Directory dir; private String prefixHTML = ""; private String suffixHTML = ""; private Date date; private Date date2; public LuceneQuery(String indexPath) { date = new Date(); this.indexPath = indexPath; } /** * 函数功能:建立文章的索引 * @param list */ public void createIndex(Listlist) { createIndexWriter();// 生成indexWriter对象 for (Article article : list) { Document document = new Document();// 生成Document对象 Field field = new Field("articleId", String.valueOf(article.getArticleId()), Field.Store.YES, Field.Index.NO); Field field2 = new Field("articleName", article.getArticleName(),Field.Store.YES, Field.Index.ANALYZED); Field field3 = new Field("articleContent", article.getArticleContent(), Field.Store.YES, Field.Index.NO); document.add(field); document.add(field2); document.add(field3); try { indexWriter.addDocument(document); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } closeIndexWriter(indexWriter); } /** * 函数功能:建立对文章的高亮显示查询 * */ public void createHightLightIndex(List list) { createIndexWriter();// 生成indexWriter对象 System.out.println("createHightLightIndex list length" + list.size()); for (LogQueryTemp logQuery : list) { Document document = new Document();// 生成Document对象 Field field = new Field("articleId", String.valueOf(logQuery.getArticleId()), Field.Store.YES, Field.Index.NO); Field field2 = new Field("articleName", logQuery.getArticleName(),Field.Store.YES, Field.Index.ANALYZED); Field field3 = new Field("articleKindName", logQuery.getArticleKindName(), Field.Store.YES, Field.Index.NO); Field field4 = new Field("writeTime", logQuery.getWriteTime(),Field.Store.YES, Field.Index.NO); Field field5 = new Field("articleContent", logQuery.getArticleContent(), Field.Store.YES, Field.Index.ANALYZED); document.add(field); document.add(field2); document.add(field3); document.add(field4); document.add(field5); try { indexWriter.addDocument(document); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } closeIndexWriter(indexWriter); } /** * 函数功能:建立查询图片的索引(主要是对图片的描述建立索引) * */ public void createPicIndex(List list) { createIndexWriter();// 生成indexWriter对象 for (PicQueryTemp picQuery : list) { Document document = new Document();// 生成Document对象 Field field = new Field("picId", String.valueOf(picQuery.getPicId()), Field.Store.YES,Field.Index.NO); Field field2 = new Field("picGroupId", String.valueOf(picQuery.getPicGroupId()), Field.Store.YES, Field.Index.NO); Field field3 = new Field("picName", picQuery.getPicName(),Field.Store.YES, Field.Index.NO); Field field4 = new Field("pictureDetail", picQuery.getPictureDetail(), Field.Store.YES, Field.Index.ANALYZED); document.add(field); document.add(field2); document.add(field3); document.add(field4); try { indexWriter.addDocument(document); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } closeIndexWriter(indexWriter); } /** * 函数功能 :关闭indexWriter * * @param indexWriter */ public void closeIndexWriter(IndexWriter indexWriter) { try { indexWriter.optimize(); indexWriter.close(); date2 = new Date(); System.out.println("建立索引总共用了:" + (date2.getTime() - date.getTime()) + "毫秒"); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 函数功能:生成indexWriter对象 */ public void createIndexWriter() { try { boolean flag = true; // 如果已存在索引,则追加索引 if (IndexReader.indexExists(indexPath)) { flag = false; } indexWriter = new IndexWriter(indexPath, new StandardAnalyzer(), flag, IndexWriter.MaxFieldLength.LIMITED); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 函数功能:查询文章的索引 */ public List queryIndex(String indexPath, String findContent) { List list2 = new ArrayList (); System.out.println("查询内容为:" + findContent); try { dir = FSDirectory.getDirectory(indexPath); searcher = new IndexSearcher(dir); QueryParser parser = new QueryParser("articleName", new StandardAnalyzer()); try { Query query = parser.parse(findContent);// 根据查询内容进行查询 TopDocs topDocs = searcher.search(query, 5); ScoreDoc[] hits = topDocs.scoreDocs; for (int i = 0; i < hits.length; i++) { int DocId = hits[i].doc; Article article = new Article(); Document doc = searcher.doc(DocId); article.setArticleId(Integer.parseInt(doc.get("articleId"))); article.setArticleName(doc.get("articleName")); article.setArticleContent(doc.get("articleContent")); list2.add(article); } Date date3 = new Date(); System.out.println("查询总共花的时间为:"+ (date3.getTime() - date2.getTime()) + "毫秒"); } catch (ParseException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return list2; } /** * 函数功能:通过booleanQuery方法进行查询索引 */ public List queryIndexByBooleanQuery(String indexPath, String findContent) { List list2 = new ArrayList (); LuceneJEAnalyzerText jeAnalyzer = new LuceneJEAnalyzerText(indexPath); BooleanQuery booleanQuery = new BooleanQuery(); try { dir = FSDirectory.getDirectory(indexPath); searcher = new IndexSearcher(dir); String[] str = jeAnalyzer.createAnalyzer(findContent).split(","); for (int i = 0; i < str.length; i++) { booleanQuery.add( new TermQuery(new Term("articleName", str[i])), BooleanClause.Occur.SHOULD); } // QueryParser parser = new QueryParser("articleName", new // StandardAnalyzer()); // Query query = parser.parse(findContent);//根据查询内容进行查询 TopDocs topDocs = searcher.search(booleanQuery, 3); ScoreDoc[] hits = topDocs.scoreDocs; for (int i = 0; i < hits.length; i++) { int DocId = hits[i].doc; Article article = new Article(); Document doc = searcher.doc(DocId); article.setArticleId(Integer.parseInt(doc.get("articleId"))); article.setArticleName(doc.get("articleName")); // article.setArticleContent(doc.get("articleContent")); list2.add(article); // list2.add(doc.get("sname")); } if (hits.length > 0) { list2.remove(0); } Date date3 = new Date(); System.out.println("查询总共花的时间为:" + (date3.getTime() - date2.getTime()) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } return list2; } /** * 函数功能:高亮显示查询 * * @param fieldName * @param keyword * @throws CorruptIndexException * @throws IOException * @throws ParseException */ public List queryPicByLucene(String findContent) { List list2 = new ArrayList (); LuceneJEAnalyzerText jeAnalyzer = new LuceneJEAnalyzerText(indexPath); System.out.println("查询内容为:" + findContent); BooleanQuery booleanQuery = new BooleanQuery(); try { dir = FSDirectory.getDirectory(indexPath); searcher = new IndexSearcher(dir); String[] str = jeAnalyzer.createAnalyzer(findContent).split(","); for (int i = 0; i < str.length; i++) { booleanQuery.add(new TermQuery( new Term("pictureDetail", str[i])), BooleanClause.Occur.SHOULD); } TopDocs topDocs = searcher.search(booleanQuery, 12); ScoreDoc[] hits = topDocs.scoreDocs; for (int i = 0; i < hits.length; i++) { int DocId = hits[i].doc; PicQueryTemp picQuery = new PicQueryTemp(); Document doc = searcher.doc(DocId); picQuery.setPicGroupId(Integer.parseInt(doc.get("picGroupId"))); picQuery.setPicId(Integer.parseInt(doc.get("picId"))); picQuery.setPicName(doc.get("picName")); picQuery.setPictureDetail(doc.get("pictureDetail")); list2.add(picQuery); // list2.add(doc.get("sname")); } Date date3 = new Date(); System.out.println("查询总共花的时间为:" + (date3.getTime() - date2.getTime()) + "毫秒"); } catch (IOException e) { e.printStackTrace(); } return list2; } /**函数功能:主要对文章的索引进行查询,主要是查询文章的内容和标题 * @param keyword * @param startIndex * @param endIndex * @return * @throws CorruptIndexException * @throws IOException * @throws ParseException */ public List search(String keyword, int startIndex, int endIndex) throws CorruptIndexException, IOException, ParseException { List listlog = new ArrayList (); searcher = new IndexSearcher(indexPath); Analyzer analyzer = new MMAnalyzer(); // QueryParser queryParse = new QueryParser(fieldName, analyzer); // Query query = queryParse.parse(keyword); LuceneJEAnalyzerText jeAnalyzer = new LuceneJEAnalyzerText(indexPath); BooleanQuery booleanQuery = new BooleanQuery(); try { dir = FSDirectory.getDirectory(indexPath); searcher = new IndexSearcher(dir); String[] str = jeAnalyzer.createAnalyzer(keyword).split(","); for (int j = 0; j < str.length; j++) { booleanQuery.add(new TermQuery(new Term("articleContent",str[j])), BooleanClause.Occur.SHOULD); booleanQuery.add(new TermQuery(new Term("articleName", str[j])),BooleanClause.Occur.SHOULD); } Hits hits = searcher.search(booleanQuery); if (endIndex >= hits.length()) { endIndex = hits.length() - 1; } for (int i = startIndex; i <= endIndex; i++) { LogQueryTemp logQuery = new LogQueryTemp(); Document doc = hits.doc(i); String text = doc.get("articleContent"); String text2 = doc.get("articleName"); int htmlLength = prefixHTML.length() + suffixHTML.length(); // System.out.println("高亮HTML的总长度为" + htmlLength); SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter( prefixHTML, suffixHTML); Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(booleanQuery)); // 设置被高亮的文本返回的摘要的文本大小 Fragmenter fragmenter = new SimpleFragmenter(350);// 默认是50个字符,修改为350个字符 highlighter.setTextFragmenter(fragmenter); String highLightText = highlighter.getBestFragment(analyzer, "articleContent", text); String highLightText2 = highlighter.getBestFragment(analyzer, "articleName", text2); if (highLightText2 != null) { logQuery.setArticleName(highLightText2); } else { logQuery.setArticleName(doc.get("articleName")); } if (highLightText != null) { logQuery.setArticleContent(highLightText); } else { logQuery.setArticleContent(doc.get("articleContent")); } logQuery.setArticleId(doc.get("articleId")); logQuery.setIsMywrite(keyword); logQuery.setArticleKindName(doc.get("articleKindName")); logQuery.setWriteTime(doc.get("writeTime")); listlog.add(logQuery); } searcher.close(); } catch (IOException ex) { ex.printStackTrace(); } return listlog; } }
如果有需要的就直接部署就可以用了!
最后附上项目代码,因为所有的jar包都在里面,还用图片比较大 ,我没有好好的优化过 ,所以目前容量比较大!
有什么问题 可以在公众号给我留言 包括源码 欢迎关注 Java架构那些事
长按,识别二维码,加关注