Jar包下载网
系统是采用cms 和 爬虫采集分离的,主要是为了节约带宽资源,爬虫不能影响cms访问的体验。
爬取地址列表写在文件中: crawlerFile=E:\\temp\\crawler.txt
package com.open1111.crawler; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import com.open1111.util.DbUtil; import com.open1111.util.PropertiesUtil; import com.open1111.util.UUIDUtil; /** * 爬虫起始类 爬取 http://central.maven.org/ * * @author user * */ public class StartCrawler { private static Logger logger = Logger.getLogger(StartCrawler.class); public static int total = 0; public static String[] excludeUrl = new String[] { ".pom", ".xml", ".md5", ".sha1", ".asc", ".gz", ".zip", "../" }; // 要过滤的url public static Queue<String> waitForCrawlerUrls = new LinkedList<String>(); // 等待爬取的url public static boolean exeFlag = true; // 执行解析url表示 默认true private static Connection con = null; //解析网页内容,把资源路径存入数据库(break 中断里面的for循环,把很多杂项排除,再判断是否jar文件,是路径的就添加进爬虫队列,continue 跳出本次外层for循环 ) private static void parseWebPage(String webPageContent, String realPath) { if (webPageContent.equals("")) { return; } Document doc = Jsoup.parse(webPageContent); Elements links = doc.select("a"); // 获取所有超链接元素 for (int i = 0; i < links.size(); i++) { Element link = links.get(i); String url = link.attr("href"); boolean f = true; for (int j = 0; j < excludeUrl.length; j++) { if (url.endsWith(excludeUrl[j])) { f = false; break; } } if (f) { if (url.endsWith(".jar")) { total++; logger.info("发现第" + total + "个目标:" + (realPath + url)); String sql = "select * from t_jar where name=?"; try { PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, url); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { logger.info("【" + url + "】 数据库已存在!"); continue; } } catch (Exception e) { logger.info("【" + url + "】 查询数据库有异常!", e); } sql = "insert into t_jar values(?,?,?,now(),?,0,0,0,0)"; try { PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, UUIDUtil.getUUID()); // uuid pstmt.setString(2, url); // 文件名称 pstmt.setString(3, (realPath + url)); // 完整路径 if (url.endsWith("javadoc.jar")) { pstmt.setString(4, "javadoc"); // javadoc文档 } else if (url.endsWith("sources.jar")) { pstmt.setString(4, "sources"); // java源码 } else { pstmt.setString(4, "jar"); // jar包 } if (pstmt.executeUpdate() == 1) { logger.info("【" + url + "】 成功插入数据库!"); } else { logger.info("【" + url + "】 插入数据库失败!"); } } catch (Exception e) { logger.info("【" + url + "】 插入数据库有异常!", e); } continue; } logger.info("爬虫url队列新增url:" + (realPath + url)); addUrl(realPath + url,"解析网页"); } } } //使用线程框架 public static void parseUrl() { ExecutorService executorService = Executors.newFixedThreadPool(10); while (exeFlag) { // System.out.println("waitForCrawlerUrls:"+waitForCrawlerUrls); if (waitForCrawlerUrls.size() > 0) { executorService.execute(new Runnable() { public void run() { String url = waitForCrawlerUrls.poll(); // 摘取第一个元素 if(url==null || "".equals(url)){ return; } logger.info("执行解析url:" + url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000) // 设置读取超时时间 .setConnectTimeout(5000) // 设置连接超时时间 .build(); CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httpclient实例 HttpGet httpget = new HttpGet(url); // 创建httpget实例 httpget.setConfig(requestConfig); CloseableHttpResponse response = null; try { response = httpclient.execute(httpget); } catch (ClientProtocolException e1) { logger.error("ClientProtocolException", e1); addUrl(url,"由于异常"); // 继续添加到集合 } catch (IOException e1) { logger.error("IOException", e1); addUrl(url,"由于异常"); // 继续添加到集合 } if (response != null) { HttpEntity entity = response.getEntity(); // 获取返回实体 // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { } // System.out.println("url:"+url); // System.out.println("contentType:"+entity.getContentType()); if (entity.getContentType().toString().equals("Content-Type: text/html")) { String webPageContent = null; try { webPageContent = EntityUtils.toString(entity, "utf-8"); // System.out.println("网页内容:"+webPageContent); // // 指定编码打印网页内容 parseWebPage(webPageContent, url); } catch (java.net.SocketTimeoutException e) { logger.error("读取超时", e); addUrl(url,"由于异常"); // 继续添加到集合 } catch (ParseException e) { logger.error("ParseException", e); addUrl(url,"由于异常"); // 继续添加到集合 } catch (IOException e) { logger.error("IOException", e); addUrl(url,"由于异常"); // 继续添加到集合 } try { response.close(); } catch (IOException e) { logger.error("IOException", e); } // 关闭流和释放系统资源 } } else { // 说明连接超时 logger.error("连接超时"); addUrl(url,"由于异常"); // 继续添加到集合 } } }); } else { if (((ThreadPoolExecutor) executorService).getActiveCount() == 0) { // 假如活动线程数是0 executorService.shutdown(); // 结束所有线程 exeFlag = false; logger.info("代号【10】爬虫任务已经完成!"); } } try { Thread.sleep(5000); // 休息5秒 } catch (InterruptedException e) { logger.error("线程报错", e); } } } /** * 初始化系统 */ public static void init() { DbUtil dbUtil = new DbUtil(); try { con = dbUtil.getCon(); } catch (Exception e) { logger.error("创建数据库连接失败", e); } logger.info("读取爬虫配置文件"); FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { String str = ""; fis = new FileInputStream(PropertiesUtil.getValue("crawlerFile")); // 从文件系统中的某个文件中获取字节 isr = new InputStreamReader(fis); br = new BufferedReader(isr); //读取文件中的URL,添加到队列 while ((str = br.readLine()) != null) { addUrl(str,"初始化"); } } catch (FileNotFoundException e) { logger.error("找不到指定文件", e); } catch (IOException e) { logger.error("读取文件失败", e); } finally { try { br.close(); isr.close(); fis.close(); } catch (IOException e) { logger.error("IO异常", e); } } logger.info("完成读取爬虫配置文件"); parseUrl(); } /** * 添加url到爬虫队列 假如队列里存在不添加 * @param url */ public static void addUrl(String url,String info){ if(url==null || "".equals(url)){ return; } if(!waitForCrawlerUrls.contains(url)){ waitForCrawlerUrls.add(url); } logger.info("【"+info+"】【"+url+"】"+"添加到爬虫队列"); } public static void main(String[] args) throws Exception { logger.info("开始执行任务"); init(); } }
全文索引也是采用离线方式手动生成, 估计站长是选访问量低的时间去搞。这个速度挺快的,不费事。
package com.open1111.index; import java.nio.file.Paths; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.apache.log4j.Logger; 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.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import com.open1111.util.DbUtil; import com.open1111.util.PropertiesUtil; public class JarsIndex { private static Logger logger = Logger.getLogger(JarsIndex.class); private static Connection con = null; public static void main(String[] args) throws Exception { long startTime=System.currentTimeMillis(); logger.error("创建索引开始"); Directory dir=FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexFile"))); Analyzer analyzer=new StandardAnalyzer(); IndexWriterConfig iwc=new IndexWriterConfig(analyzer); IndexWriter writer=new IndexWriter(dir, iwc); DbUtil dbUtil = new DbUtil(); try { con = dbUtil.getCon(); } catch (Exception e) { logger.error("创建数据库连接失败", e); } int total=0; String sql = "select count(*) as total from t_jar where indexState=0"; try { PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { total=rs.getInt("total"); } } catch (Exception e) { logger.info("查询数据库有异常!", e); } logger.info("总记录数:"+total); sql="select * from t_jar where indexState=0"; try { PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ String id=rs.getString("uuid"); String name=rs.getString("name"); Document doc=new Document(); doc.add(new StringField("id",id,Field.Store.YES)); doc.add(new TextField("name",name.replaceAll("-", " "),Field.Store.YES)); writer.addDocument(doc); // 更新数据库indexState状态字段 改成1 sql="update t_jar set indexState=1 where uuid='"+id+"'"; PreparedStatement pstmt2 = con.prepareStatement(sql); pstmt2.executeUpdate(); } } catch (Exception e) { logger.info("查询数据库有异常!", e); } try { dbUtil.closeCon(con); // 关闭数据库连接 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.close(); // 关闭写入流 long endTime=System.currentTimeMillis(); logger.error("创建索引完成"); logger.error("用时:"+(endTime-startTime)); } }
手动生成tag标签
package com.open1111.tag; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.apache.log4j.Logger; import com.open1111.util.DbUtil; public class TagMain { private static Logger logger = Logger.getLogger(TagMain.class); public static void main(String[] args) { long startTime = System.currentTimeMillis(); logger.error("生成tag开始"); Connection con = null; DbUtil dbUtil = new DbUtil(); try { con = dbUtil.getCon(); } catch (Exception e) { logger.error("创建数据库连接失败", e); } int total = 0; String sql = "select count(*) as total from t_jar where tagState=0"; try { PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { total = rs.getInt("total"); } } catch (Exception e) { logger.info("查询数据库有异常!", e); } logger.info("总记录数:" + total); sql = "select * from t_jar where tagState=0 limit 0,100000"; try { PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { String id=rs.getString("uuid"); String names[] = rs.getString("name").replaceAll(".jar", "").split("-"); // 获取jar包名称 // 获取简单名称 for (String n : names) { if ("javadoc".equals(n) || "sources".equals(n) || n.contains(".")) { // 过滤无效标签 continue; } // 去数据库t_tag表里去查询,看是否存在 假如不存在 就添加 sql = "select * from t_tag where name=?"; PreparedStatement pstmt2 = con.prepareStatement(sql); pstmt2.setString(1, n); ResultSet rs2 = pstmt2.executeQuery(); if (!rs2.next()) { // 假如不存在 我们插入下 sql = "insert into t_tag values(null,?)"; PreparedStatement pstmt3 = con.prepareStatement(sql); pstmt3.setString(1, n); pstmt3.executeUpdate(); logger.info("插入标签:" + n); } } // 更新数据库indexState状态字段 改成1 sql="update t_jar set tagState=1 where uuid='"+id+"'"; PreparedStatement pstmt4 = con.prepareStatement(sql); pstmt4.executeUpdate(); } } catch (Exception e) { logger.info("查询数据库有异常!", e); } try { dbUtil.closeCon(con); // 关闭数据库连接 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.error("生成tag结束"); long endTime = System.currentTimeMillis(); logger.error("用时:" + (endTime - startTime)); } }
CMS系统主要是前台页面,后台基本不会去手动修改索引或者tag,所以可以忽视。
主要3个页面,可以查看jar.open1111.com
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页_Jar包下载网</title> <META NAME="Author" CONTENT="Java1234_小锋老师"> <meta name="keywords" content="Jar包下载,JavaDoc下载,JavaSouce下载,Java文档下载,Java源码下载" /> <meta name="description" content="Jar包下载网是一家Jar包资源整合下载网站." /> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/jar.css"> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/jquery-1.11.2.min.js"></script> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/bootstrap.min.js"></script> <style type="text/css"> body { padding-bottom: 40px; } </style> <script type="text/javascript"> $(function(){ $("#search").click(function(){ // 给span注册点击事件 search(); }); $("#sInput").keydown(function(e) { // 给搜索文本框注册enter回车事件 if (e.keyCode == 13) { search(); } }); function search(){ var q=$("#sInput").val(); if(q==""){ $("#sInput").focus(); return; } window.location.href="${pageContext.request.contextPath}/jar/q.do?q="+q; } $.post("${pageContext.request.contextPath}/tag/randomList.do",{n:200},function(result){ $("#jars").children().remove(); $("#jars").append("<font color=red> 猜你喜欢:</font><hr style='margin-top: 2px; margin-bottom: 10px;'></hr>"); $.each(result.rows,function(i,val){ var di=$("<li><a href='${pageContext.request.contextPath}/jar/q.do?q="+val.name+"' target='_blank' title='"+val.name+".jar下载'>"+val.name+".jar</a></li>"); $("#jars").append(di); }); },"json"); }); </script> </head> <body> <jsp:include page="/foreground/common/top.jsp"/> <div class="container"> <div class="row"> <div class="col-md-12" align="center" style="padding-top: 50px;"> <a href="http://jar.open1111.com" target="_blank"><img alt="jar包下载网" src="${pageContext.request.contextPath}/static/images/logo.png"></a> </div> </div> <div class="row"> <div class="col-md-3"></div> <div class="col-md-6" align="center" style="padding-top: 20px;"> <div class="input-group"> <input type="text" class="form-control input-lg" id="sInput" placeholder="请输入英文jar包名称,多个关键字之间用空格隔开"><span class="input-group-addon btn btn-info" id="search">搜索一下</span> </div> </div> <div class="col-md-3"></div> </div> <div class="row"> <div class="col-md-2"></div> <div class="col-md-8"> <ul id="jars" class="indexJars"> </ul> </div> <div class="col-md-2"></div> </div> <jsp:include page="/foreground/common/foot.jsp"/> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${q}-查询结果页面_Jar包下载网</title> <META NAME="Author" CONTENT="Java1234_小锋老师"> <meta name="keywords" content="${q}-查询结果页面" /> <meta name="description" content="${q}-查询结果页面" /> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/jar.css"> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/jquery-1.11.2.min.js"></script> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/bootstrap.min.js"></script> <style type="text/css"> body { padding-bottom: 40px; } </style> <script type="text/javascript"> $(function(){ $("#search").click(function(){ // 给span注册点击事件 search(); }); $("#sInput").keydown(function(e) { // 给搜索文本框注册enter回车事件 if (e.keyCode == 13) { search(); } }); function search(){ var q=$("#sInput").val(); if(q==""){ $("#sInput").focus(); return; } window.location.href="${pageContext.request.contextPath}/jar/q.do?q="+q; } $.post("${pageContext.request.contextPath}/tag/randomList.do",{n:200},function(result){ $("#jars").children().remove(); $.each(result.rows,function(i,val){ var di=$("<li><a href='${pageContext.request.contextPath}/jar/q.do?q="+val.name+"' title='"+val.name+".jar下载'>"+val.name+".jar</a></li>"); $("#jars").append(di); }); },"json"); }); </script> </head> <body> <jsp:include page="/foreground/common/top.jsp"/> <div class="container"> <div class="row" style="padding-top: 30px;"> <div class="col-md-6" align="center"> <div class="input-group"> <input type="text" value="${q }" id="sInput" class="form-control" placeholder="请输入英文jar包名称,多个关键字之间用空格隔开"><span class="input-group-addon btn btn-info" id="search">搜索一下</span> </div> </div> <div class="col-md-6"> <div style="padding-top: 10px;"> <a href="${pageContext.request.contextPath}/jar/q.do?q=spring" title="spring.jar下载">spring</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=hibernate" title="hibernate.jar下载">hibernate</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=mybatis" title="mybatis.jar下载">mybatis</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=log4j" title="log4j.jar下载">log4j</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=struts" title="struts.jar下载">struts</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=hadoop" title="hadoop.jar下载">hadoop</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=spark" title="spark.jar下载">spark</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=solr" title="solr.jar下载">solr</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=poi" title="poi.jar下载">poi</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=itext" title="itext.jar下载">itext</a> </div> </div> </div> <div class="row" style="padding-top: 20px;"> <div class="col-md-8"> <div class="data_list"> <div class="data_list_title"> <img src="${pageContext.request.contextPath}/static/images/search_icon.png"/> 搜索 <font color="red">${q }</font> 的结果 (总共搜索到 ${resultTotal} 条记录) </div> <div class="datas search"> <ul> <c:choose> <c:when test="${jarList.size()==0 }"> <div align="center" style="padding-top: 20px">未查询到结果,请换个关键字试试看(注意:仅支持英文jar包名称搜索,多个关键字之间用空格隔开)</div> </c:when> <c:otherwise> <c:forEach var="jar" items="${jarList }"> <li style="margin-bottom: 20px"> <span class="title"><a href="${pageContext.request.contextPath}/jar/${jar.uuid}.html" target="_blank" title="${jar.name2 }下载">${jar.name }</a></span> </li> </c:forEach> </c:otherwise> </c:choose> </ul> </div> <nav> <ul class="pagination pagination-sm"> ${pageCode } </ul> </nav> </div> </div> <div class="col-md-4"> <div class="data_list"> <div class="data_list_title"> <img src="${pageContext.request.contextPath}/static/images/tag_icon.png"/> jar包标签 </div> <div> <ul id="jars" class="resultJars"> </ul> </div> </div> </div> </div> <jsp:include page="/foreground/common/foot.jsp"/> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${jar.name} jar包下载_Jar包下载网</title> <META NAME="Author" CONTENT="Java1234_小锋老师"> <meta name="keywords" content="${jar.name}" /> <meta name="description" content="${jar.name}" /> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/jar.css"> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/jquery-1.11.2.min.js"></script> <script src="${pageContext.request.contextPath}/static/bootstrap3/js/bootstrap.min.js"></script> <style type="text/css"> body { padding-bottom: 40px; } </style> <script type="text/javascript"> $(function(){ $("#search").click(function(){ // 给span注册点击事件 search(); }); $("#sInput").keydown(function(e) { // 给搜索文本框注册enter回车事件 if (e.keyCode == 13) { search(); } }); function search(){ var q=$("#sInput").val(); if(q==""){ $("#sInput").focus(); return; } window.location.href="${pageContext.request.contextPath}/jar/q.do?q="+q; } $.post("${pageContext.request.contextPath}/jar/relJarList.do",{name:'${jar.name}',type:'${jar.type}'},function(result){ $("#relJarTable").children().remove(); if(result.rows.length==0){ $("#relJarTable").append("未找到相关资源"); }else{ $.each(result.rows,function(i,val){ if(i<8){ var di=$("<li><a href='${pageContext.request.contextPath}/jar/"+val.uuid+".html' title='"+val.name2+"下载' target='_blank'>"+val.name2+"</a></li>"); $("#relJarTable").append(di); }else{ var di=$("<li><a href='${pageContext.request.contextPath}/jar/"+val.uuid+".html' title='"+val.name2+"下载' target='_blank'>"+val.name2+"</a></li>"); $("#relJarTable2").append(di); } }); } },"json"); $.post("${pageContext.request.contextPath}/tag/randomList.do",{n:200},function(result){ $("#jars").children().remove(); $.each(result.rows,function(i,val){ var di=$("<li><a href='${pageContext.request.contextPath}/jar/q.do?q="+val.name+"' title='"+val.name+".jar下载'>"+val.name+".jar</a></li>"); $("#jars").append(di); }); },"json"); }); function download(uuid){ window.open("${pageContext.request.contextPath}/jar/download/"+uuid+".html"); } </script> </head> <body> <jsp:include page="/foreground/common/top.jsp"/> <div class="container"> <div class="row" style="padding-top: 30px;"> <div class="col-md-6" align="center"> <div class="input-group"> <input type="text" value="${q }" id="sInput" class="form-control" placeholder="请输入英文jar包名称,多个关键字之间用空格隔开"><span class="input-group-addon btn btn-info" id="search">搜索一下</span> </div> </div> <div class="col-md-6"> <div style="padding-top: 10px;"> <a href="${pageContext.request.contextPath}/jar/q.do?q=spring" title="spring.jar下载">spring</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=hibernate" title="hibernate.jar下载">hibernate</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=mybatis" title="mybatis.jar下载">mybatis</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=log4j" title="log4j.jar下载">log4j</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=struts" title="struts.jar下载">struts</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=hadoop" title="hadoop.jar下载">hadoop</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=spark" title="spark.jar下载">spark</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=solr" title="solr.jar下载">solr</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=poi" title="poi.jar下载">poi</a> <a href="${pageContext.request.contextPath}/jar/q.do?q=itext" title="itext.jar下载">itext</a> </div> </div> </div> <div class="row" style="padding-top: 20px;"> <div class="col-md-8"> <div class="data_list"> <div class="data_list_title"> <img src="${pageContext.request.contextPath}/static/images/jar_show_icon.png"/> Jar包信息 </div> <div style="padding: 20px;"> <table> <tr> <td><strong><big>jar包名称:</big></strong></td> <td>${jar.name }</td> <td> </td> <td> </td> <td> </td> <td> </td> <td><strong><big>jar包类型:</big></strong></td> <td> <c:choose> <c:when test="${jar.type=='jar' }"> 二进制Jar包 </c:when> <c:when test="${jar.type=='javadoc' }"> JavaDoc文档 </c:when> <c:when test="${jar.type=='sources' }"> Java源码 </c:when> <c:otherwise> 未知 </c:otherwise> </c:choose> </td> </tr> <tr> <td colspan="6"> </td> </tr> <tr> <td><strong><big>更新时间:</big></strong></td> <td><fmt:formatDate value="${jar.updateDate}" type="date" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td> </td> <td> </td> <td> </td> <td> </td> <td><strong><big>整理人:</big></strong></td> <td>Java1234_小锋老师</td> </tr> <tr> <td colspan="6"> </td> </tr> <tr> <td><strong><big>访问次数:</big></strong></td> <td>${jar.click }</td> <td> </td> <td> </td> <td> </td> <td> </td> <td><strong><big>下载次数:</big></strong></td> <td>${jar.downHit }</td> </tr> <tr> <td colspan="6"> </td> </tr> <tr> <td colspan="8"> <button type="button" class="btn btn-danger" onclick="download('${jar.uuid}')">下载该jar包资源</button><br/><br/> (建议用迅雷下载,速度飞快;否则,龟速或者下载失败;建议用QQ浏览器) </td> </tr> </table> </div> </div> <div class="data_list"> <div class="data_list_title"> <img src="${pageContext.request.contextPath}/static/images/jar_about_icon.png"/> 相关jar包资源 </div> <div style="padding: 20px;height: 200px"> <ul id="relJarTable" class="relJarTable"> </ul> <ul id="relJarTable2" class="relJarTable"> </ul> </div> </div> <div> <!--PC版--> <div id="SOHUCS" sid="${jar.uuid }"></div> <script charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/changyan.js" ></script> <script type="text/javascript"> window.changyan.api.config({ appid: 'cysGfqVIm', conf: 'prod_b63f4160d3c1165b9a6d353811cc8e2d' }); </script> </div> </div> <div class="col-md-4"> <div class="data_list"> <div class="data_list_title"> <img src="${pageContext.request.contextPath}/static/images/tag_icon.png"/> jar包标签 </div> <div> <ul id="jars" class="resultJars"> </ul> </div> </div> </div> </div> <jsp:include page="/foreground/common/foot.jsp"/> </div> </body> </html>
随机列出200个tag标签,select * from t_tag order by RAND() limit #{n}
全文索引关键字出16个相关文件。
package jar.open1111.controller; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import jar.open1111.entity.Jar; import jar.open1111.lucene.JarsIndex; import jar.open1111.service.JarService; import jar.open1111.util.PageUtil; import jar.open1111.util.ResponseUtil; import jar.open1111.util.StringUtil; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Jar包Controller层 * @author user * */ @Controller @RequestMapping("/jar") public class JarController { @Resource private JarService jarService; private JarsIndex jarsIndex=new JarsIndex(); /** * 根据关键字查询相关Jar包信息 * @param q * @param page * @param request * @return * @throws Exception */ @RequestMapping("/q") public ModelAndView search(@RequestParam(value="q",required=false)String q,@RequestParam(value="page",required=false)String page,HttpServletRequest request)throws Exception{ if(StringUtil.isEmpty(page)){ page="1"; } ModelAndView mav=new ModelAndView(); List<Jar> jarList=jarsIndex.searchJar(q.trim(),200); // 查询200个,由于不是在数据库中查询,所以用下面的截取方式 Integer toIndex=jarList.size()>=Integer.parseInt(page)*20?Integer.parseInt(page)*20:jarList.size(); // 每页20条数据 mav.addObject("jarList",jarList.subList((Integer.parseInt(page)-1)*20, toIndex)); mav.addObject("pageCode",PageUtil.genPagination(request.getServletContext().getContextPath()+"/jar/q.do", jarList.size(), Integer.parseInt(page), 20, "q="+q)); mav.addObject("q",q); mav.addObject("resultTotal",jarList.size()); mav.setViewName("result"); return mav; } /** * 请求具体jar页面 指定jar访问次数加1 * @param uuid * @param request * @return * @throws Exception */ @RequestMapping("/{uuid}") public ModelAndView view(@PathVariable("uuid") String uuid,HttpServletRequest request)throws Exception{ ModelAndView mav=new ModelAndView(); Jar jar=jarService.findById(uuid); jar.setClick(jar.getClick()+1); //访问次数加1 jarService.update(jar); mav.addObject("jar", jar); mav.setViewName("view"); return mav; } /** * 根据当前jar包查询相关jar包 * @param name * @param response * @return * @throws Exception */ @RequestMapping("/relJarList") public String relJarList(Jar jar,HttpServletResponse response)throws Exception{ // 查找16个相关jar资源 List<Jar> relJarList=jarsIndex.searchJar(jar.getName().replaceAll("-", " "),16); JSONObject result=new JSONObject(); JSONArray jsonArray=JSONArray.fromObject(relJarList); result.put("rows", jsonArray); ResponseUtil.write(response, result); return null; } /** * 下载指定jar包 * @param uuid * @param request * @return * @throws Exception */ @RequestMapping("/download/{uuid}") public String download(@PathVariable("uuid") String uuid,HttpServletResponse response)throws Exception{ Jar jar=jarService.findById(uuid); jar.setDownHit(jar.getDownHit()+1); jarService.update(jar); // 下载次数加1 response.sendRedirect(jar.getPath()); // 重定向 return null; } }
/** * 标签Controller * @author user * */ @Controller @RequestMapping("/tag") public class TagController { @Resource private TagService tagService; /** * 随机查询N个标签 * @param n * @param response * @return * @throws Exception */ @RequestMapping("/randomList") public String randomList(Integer n,HttpServletResponse response)throws Exception{ List<Tag> tagList=tagService.randomList(n); JSONObject result=new JSONObject(); JSONArray jsonArray=JSONArray.fromObject(tagList); result.put("rows", jsonArray); ResponseUtil.write(response, result); return null; } } ======================================== /** * 查询Jar包信息 * @param q 查询关键字 * @param n 查询个数 * @return * @throws Exception */ public List<Jar> searchJar(String q,int n)throws Exception{ dir=FSDirectory.open(Paths.get(LUCENE_PATH)); IndexReader reader = DirectoryReader.open(dir); IndexSearcher is=new IndexSearcher(reader); Analyzer analyzer=new StandardAnalyzer(); QueryParser parser=new QueryParser("name",analyzer); Query query=parser.parse(q); TopDocs hits=is.search(query, n); // 查询n条 QueryScorer scorer=new QueryScorer(query); Fragmenter fragmenter = new SimpleSpanFragmenter(scorer); SimpleHTMLFormatter simpleHTMLFormatter=new SimpleHTMLFormatter("<b><font color='red'>","</font></b>"); Highlighter highlighter=new Highlighter(simpleHTMLFormatter, scorer); highlighter.setTextFragmenter(fragmenter); List<Jar> jarList=new LinkedList<Jar>(); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=is.doc(scoreDoc.doc); Jar jar=new Jar(); jar.setUuid(doc.get(("id"))); String name=doc.get("name"); jar.setName2(name); if(name!=null){ TokenStream tokenStream = analyzer.tokenStream("name", new StringReader(name)); String hName=highlighter.getBestFragment(tokenStream, name); if(StringUtil.isEmpty(hName)){ jar.setName(doc.get("name")); }else{ jar.setName(hName); } } jarList.add(jar); } return jarList; }

浙公网安备 33010602011771号