- 浏览: 109638 次
- 性别:
- 来自: 杭州
文章分类
最新评论
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
DBUtil.java
public static Map<String, WordBean> getWordDicMap() { // int page, int rows, String sidx, // String sord, String _search, String searchField, String searchOper, // String searchString List<WordBean> wordDicList = (List<WordBean>) (DBUtil .getWordDicList(0, Integer.MAX_VALUE, "", "", "false", "", "", "")[0]); Map<String, WordBean> wordDicMap = new HashMap<String, WordBean>(); for (int i = 0; i < wordDicList.size(); i++) { WordBean oneWordBean = wordDicList.get(i); System.out.println("oneWordBean:" + oneWordBean); if(StringUtils.isNotEmpty(oneWordBean.getContent())) { wordDicMap.put(oneWordBean.getContent().toLowerCase(), oneWordBean); } } return wordDicMap; }
package sy; import java.util.List; import org.jsoup.*; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.select.Elements; public class HttpDemo1 { public static void main(String[] args) { HttpDemo1 demo1 = new HttpDemo1(); // System.out.println("Word:A, Resutl:" + demo1.getDictWord("A")); // System.out.println("Word:elaborate, Resutl:" + demo1.getDictWord("elaborate")); // System.out.println("Word:Test, Resutl:" + demo1.getDictWord("Test")); // System.out.println("Word:City, Resutl:" + demo1.getDictWord("City")); // System.out.println("Word:Come, Resutl:" + demo1.getDictWord("Come")); // System.out.println("Word:in, Resutl:" + demo1.getDictWord("in")); // System.out.println("Word:right, Resutl:" + demo1.getDictWord("right")); // System.out.println("Word:tel, Resutl:" + demo1.getDictWord("tel")); String returnBody = HttpUtil.Get("http://www.h2database.com/html/main.html"); returnBody = Jsoup.parse(returnBody).text(); System.out.println("returnBody:" + returnBody); } public String getDictWord(String word) { String returnBody = HttpUtil.Get("http://dict.cn/" + word); String returnStr = ""; Document doc = Jsoup.parse(returnBody); Elements els = doc.getElementsByClass("dict-basic-ul"); for(Element el : els) { List<Node> nds = el.childNodes(); for(Node nd : nds) { String ndStr = nd.toString().trim(); if(ndStr.length()>0 && ndStr.length()<200) { returnStr += ndStr + "\r"; } } } returnStr = returnStr.replaceAll("<[^>]+>", "").replaceAll("<","<").replaceAll(">",">").replaceAll("&","&"); if(returnStr.trim().length()==0) { return "Not found"; } else return returnStr; } }
package sy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HostParams; import org.apache.http.client.params.ClientPNames; public class HttpUtil { public static String Get(String url) { HttpClient httpClient = new HttpClient(); HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setProxy("proxy.statestr.com", 80); HostParams hostparams = new HostParams(); hostparams.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY); hostConfiguration.setParams(hostparams); httpClient.setHostConfiguration(hostConfiguration); String response = null; HttpMethod head = new GetMethod(url); //head.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK"); int statusCode = 0; // execute the method and handle any error responses. try { statusCode = httpClient.executeMethod(head); InputStream inputStm = head.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStm,"utf-8")); String tempbf; StringBuffer html = new StringBuffer(100); while ((tempbf = br.readLine()) != null) { html.append(tempbf +"\n"); } response = html.toString(); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { head.releaseConnection(); } // Retrieve all the headers. return response; } }
-------------------------
import java.io.File; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class ParserUtil { @Before public void init() { System.setProperty("webdriver.firefox.bin", "C:/soft/Mozilla Firefox/firefox.exe"); MyFilter.isLocal = true; } long time1 = 0; WebDriver driver = null; boolean isDone = false; @Test public void translateTODO() throws Exception { do { try { time1 = System.currentTimeMillis(); driver = new FirefoxDriver(); // WebDriver driver = new HtmlUnitDriver(); // ((HtmlUnitDriver)driver).setProxy("proxy.statestr.com", 80); List<WordBean> infoBeanList = (List<WordBean>) (DBUtil .getWordDicList(0, Integer.MAX_VALUE, "", "", "true", "comment", "eq", "TODO")[0]); if(infoBeanList.isEmpty()) { isDone = true; } for (int i = 0; i < infoBeanList.size(); i++) { time1 = System.currentTimeMillis(); WordBean wordBean = infoBeanList.get(i); String word = wordBean.getContent(); System.out.println("===word:" + word + " currDate:" + new Date()); driver.get("http://dict.cn/" + word); String pageSource = driver.getPageSource(); // System.out.println(pageSource); String comment = ""; if (pageSource.contains("您要查找的是不是")) { comment = "UNKNOWN"; wordBean.setRank(-1); } else { WebElement div = driver.findElement(By .xpath("//div[@class='basic clearfix']")); comment = div.getText(); } System.out.println("comment:" + comment); wordBean.setComment(comment); DBUtil.updateInfo(wordBean); } isDone = true; } catch (Exception e) { e.printStackTrace(); } } while (!isDone); new Thread() { public void run() { while(!isDone) { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } if (System.currentTimeMillis() - time1 >= 90 * 1000) { System.out.println("###longer than 90s"); time1 = System.currentTimeMillis(); driver.quit(); time1 = System.currentTimeMillis(); } } if(driver != null) { driver.quit(); } } }.start(); // String word = "cat"; } @Test public void parseSentences() throws Exception { String content = FileUtils.readFileToString(new File( "lib/CamelInAction.txt")); content = content.replaceAll("-(\r)?\n", ""); content = content.replaceAll("(\r)?\n", " "); // System.out.println("content:" + content); String[] arr = content.split("\\.|\\?|!"); for (int i = 0; i < arr.length; i++) { String oneSen = StringUtils.trim(arr[i]); System.out.println("line" + i + ":" + oneSen); if (StringUtils.isBlank(oneSen) || StringUtils.split(oneSen, "\\s+").length == 1) { continue; } SentenBean sentenBean = new SentenBean(0, oneSen.toLowerCase(), 1); DBUtil.saveInfo(sentenBean); } } @Test public void parseWords() throws Exception { File file = new File("lib/CamelInAction.txt"); Map<String, WordBean> wordDicMap = DBUtil.getWordDicMap(); String content = FileUtils.readFileToString(new File( "lib/CamelInAction.txt")); content = content.replaceAll("-(\r)?\n", ""); // hap-pen --> happen FileUtils.writeStringToFile(file, content); List<String> lineList = FileUtils.readLines(file); int size = lineList.size(); for (int i = 0; i < size; i++) { System.out.println("line:" + (i + 1)); String oneLine = lineList.get(i); String[] oneLineArr = StringUtils.split(oneLine, ' '); // System.out.println(Arrays.asList(oneLineArr)); for (int j = 0; j < oneLineArr.length; j++) { String oneWord = StringUtils.trim(oneLineArr[j]); oneWord = oneWord.replace(",", "").replace(":", "") .replace(".", "").replace("(", "").replace(")", ""); if (StringUtils.isEmpty(oneWord) || oneWord.length() == 1 || oneWord.startsWith("www") || !oneWord.matches("^[a-zA-Z\\-]*")) { System.out.println("##ignore:" + oneWord); continue; } System.out.println("get:" + oneWord); String lowerCaseWord = oneWord.toLowerCase(); if (wordDicMap.containsKey(lowerCaseWord)) { WordBean oneWordBean = wordDicMap.get(lowerCaseWord); oneWordBean.setHit(oneWordBean.getHit() + 1); DBUtil.updateInfo(oneWordBean); } else { WordBean oneWordBean = new WordBean(0, oneWord, "TODO", 5, 1); wordDicMap.put(oneWord.toLowerCase(), oneWordBean); DBUtil.saveInfo(oneWordBean); } } } } }
import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.SingleConnectionDataSource; public class DBUtil { static Connection conn = null; private static JdbcTemplate getJDBCTemplate() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception e) { e.printStackTrace(); } try { String hostname = null; String port = null; String user = null; String password = null; String dbName = null; if (MyFilter.isLocal) { hostname = "localhost"; port = "3306"; user = "root"; password = "root"; dbName = "english"; } else { hostname = System.getenv("MOPAAS_MYSQL10846_HOST"); port = System.getenv("MOPAAS_MYSQL10846_PORT"); user = System.getenv("MOPAAS_MYSQL10846_USER"); password = System.getenv("MOPAAS_MYSQL10846_PASSWORD"); dbName = System.getenv("MOPAAS_MYSQL10846_NAME"); } String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + user + "&password=" + password + "&characterEncoding=UTF8"; if (conn == null) { conn = DriverManager.getConnection(jdbcUrl); } System.out.println("jdbcUrl:" + jdbcUrl); } catch (SQLException e) { e.printStackTrace(); } System.out.println("conn:" + conn); JdbcTemplate jdbcTempalte = new JdbcTemplate( new SingleConnectionDataSource(conn, false)); return jdbcTempalte; } // public static String queryInfo(String queryStr) { // StringBuilder sb = new StringBuilder(); // JdbcTemplate jdbcTempalte = getJDBCTemplate(); // System.out.println("jdbcTempalte:" + jdbcTempalte + " queryStr:" // + queryStr); // String queryStr2 = "%" + queryStr + "%"; // List infoBeanList = jdbcTempalte // .query("select * from info where (title like ? or content like ? or keywords like ? ) and status = " // + MyConstant.APPROVED, // new Object[] { queryStr2, queryStr2, queryStr2 }, // new ItemMapper()); // System.out.println(infoBeanList); // if (!(infoBeanList.isEmpty())) { // sb.append("搜索结果为:\n"); // } // int infoBeanListSize = infoBeanList.size() >= 5 ? 5 : // infoBeanList.size(); // for (int i = 0; i < infoBeanListSize; ++i) { // WordBean oneInfoBean = (WordBean) infoBeanList.get(i); // sb.append((i + 1) + ".标题:" + oneInfoBean.getTitle()); // if (StringUtils.isNotEmpty(oneInfoBean.getContent())) { // sb.append("\n内容:" + oneInfoBean.getContent()); // } // if (StringUtils.isNotEmpty(oneInfoBean.getContactor())) { // sb.append("\n联系人:" + oneInfoBean.getContactor()); // } // if (StringUtils.isNotEmpty(oneInfoBean.getTel())) { // sb.append("\n联系电话:" + oneInfoBean.getTel()); // } // // sb.append("\n\n"); // } // if (sb.length() == 0) { // sb.append("抱歉,没有找到您想要的信息"); // } // return sb.toString(); // } // // public static String getTopStr() { // StringBuilder sb = new StringBuilder(); // JdbcTemplate jdbcTempalte = getJDBCTemplate(); // System.out.println("jdbcTempalte:" + jdbcTempalte); // List infoBeanList = jdbcTempalte // .query("select * from (select * from info where status = " + // MyConstant.APPROVED + " order by weight desc) t limit 0, 10", // new ItemMapper()); // System.out.println(infoBeanList); // for (int i = 0; i < infoBeanList.size(); ++i) { // WordBean oneInfoBean = (WordBean) infoBeanList.get(i); // sb.append((i + 1) + ".标题:" + oneInfoBean.getTitle()); // if (StringUtils.isNotEmpty(oneInfoBean.getContent())) { // sb.append("\n内容:" + oneInfoBean.getContent()); // } // if (StringUtils.isNotEmpty(oneInfoBean.getContactor())) { // sb.append("\n联系人:" + oneInfoBean.getContactor()); // } // if (StringUtils.isNotEmpty(oneInfoBean.getTel())) { // sb.append("\n联系电话:" + oneInfoBean.getTel()); // } // // sb.append("\n\n"); // } // return sb.toString(); // } // // public static List<WordBean> getInfoList(String category) { // JdbcTemplate jdbcTempalte = getJDBCTemplate(); // System.out.println("jdbcTempalte:" + jdbcTempalte); // List infoBeanList = jdbcTempalte // .query("select * from (select * from info where status = " + // MyConstant.APPROVED + " and comment2 = '" + category + // "' order by lastUpdateDate desc) t ", // new ItemMapper()); // // if("CQ".equals(category)) { // Collections.sort(infoBeanList); // } // // System.out.println(infoBeanList); // return infoBeanList; // } // // public static List<WordBean> getInfoList(String type, String status) { // JdbcTemplate jdbcTempalte = getJDBCTemplate(); // System.out.println("jdbcTempalte:" + jdbcTempalte); // String sql = "select * from info where 1 = 1 "; // if(!StringUtils.isEmpty(type)) { // sql += " and comment2 = '" + type + "'"; // } else { // sql += " and comment2 in ('CQ', 'resource') "; // } // // if(!StringUtils.isEmpty(status)) { // sql += " and status = " + status; // } // // sql += " order by lastUpdateDate desc"; // List infoBeanList = jdbcTempalte // .query(sql , // new ItemMapper()); // // System.out.println(infoBeanList); // return infoBeanList; // } // // public static List<WordBean> getClosedCQList() { // String category = "CQ"; // JdbcTemplate jdbcTempalte = getJDBCTemplate(); // System.out.println("jdbcTempalte:" + jdbcTempalte); // List infoBeanList = jdbcTempalte // .query("select * from (select * from info where status = " + // MyConstant.CLOSED + " and comment2 = '" + category + // "' order by weight desc) t ", // new ItemMapper()); // System.out.println(infoBeanList); // return infoBeanList; // } // public static Object[] getWordDicList(int page, int rows, String sidx, String sord, String _search, String searchField, String searchOper, String searchString) { if (StringUtils.isEmpty(sidx)) { sidx = "id"; } if (StringUtils.isEmpty(sord)) { sord = "asc"; } JdbcTemplate jdbcTempalte = getJDBCTemplate(); String whereCaluse = " where 1 = 1 "; if ("true".equals(_search)) { whereCaluse += " and " + transOper(searchField, searchOper, searchString); } String sql = "select * from words " + whereCaluse + " order by " + sidx + " " + sord + " limit " + page + ", " + rows; String countSql = "select count(*) from words" + whereCaluse; System.out.println("###sql:" + sql); List<WordBean> infoBeanList = jdbcTempalte.query(sql, new ItemMapper()); int totalCount = jdbcTempalte.queryForObject(countSql, Integer.class); return new Object[] { infoBeanList, totalCount }; } private static String transOper(String searchField, String searchOper, String searchString) { String result = ""; if ("eq".equals(searchOper)) { result = searchField + " = '" + searchString + "'"; } else if ("ne".equals(searchOper)) { result = searchField + " != '" + searchString + "'"; } else if ("cn".equals(searchOper)) { result = searchField + " like '%" + searchString + "%'"; } else if ("nc".equals(searchOper)) { result = searchField + " not like '%" + searchString + "%'"; } else if ("bw".equals(searchOper)) { result = searchField + " like '" + searchString + "%'"; } else if ("ew".equals(searchOper)) { result = searchField + " like '%" + searchString + "'"; } else if ("bn".equals(searchOper)) { result = searchField + " not like '" + searchString + "%'"; } else if ("en".equals(searchOper)) { result = searchField + " not like '%" + searchString + "'"; }else if ("gt".equals(searchOper)) { result = searchField + " > " + searchString; } return result; } public static Map<String, WordBean> getWordDicMap() { // FIXME // List<WordBean> wordDicList = (List<WordBean>) (getWordDicList(0, 0, // null, null, null, null, null, null)[0]); // Map<String, WordBean> wordDicMap = new HashMap<String, WordBean>(); // for (int i = 0; i < wordDicList.size(); i++) { // WordBean oneWordBean = wordDicList.get(i); // wordDicMap.put(oneWordBean.getContent().toLowerCase(), oneWordBean); // } return null; } public static WordBean getWordBean(int id) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); String sql = "select * from words where id = " + id; WordBean wordBean = jdbcTempalte.queryForObject(sql, new ItemMapper()); return wordBean; } public static void delInfo(String ids) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); jdbcTempalte.execute("delete from words where id in (" + ids + ")"); } public static boolean saveInfo(SentenBean sentenBean) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); int count = ((Integer) jdbcTempalte.queryForObject( "select count(*) from sentences", Integer.class)).intValue(); int maxId = 0; if (count == 0) { maxId = 0; } else { maxId = ((Integer) jdbcTempalte.queryForObject( "select max(id) maxId from sentences", Integer.class)) .intValue(); } int updateCount = jdbcTempalte.update( "insert into sentences (id, content, rank) values(? , ? , ?)", new Object[] { Integer.valueOf(maxId + 1), sentenBean.getContent(), sentenBean.getRank() }); System.out.println("save sentence Info updateCount:" + updateCount); return (updateCount != 0); } public static boolean saveInfo(WordBean wordBean) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); System.out.println("jdbcTempalte:" + jdbcTempalte); int count = ((Integer) jdbcTempalte.queryForObject( "select count(*) from words", Integer.class)).intValue(); int maxId = 0; if (count == 0) { maxId = 0; } else { maxId = ((Integer) jdbcTempalte.queryForObject( "select max(id) maxId from words", Integer.class)) .intValue(); } int updateCount = jdbcTempalte .update("insert into words (id, content, comment, rank, hit, createDate, lastUpt) values(? , ? , ? , ?, ?, ?, ?)", new Object[] { Integer.valueOf(maxId + 1), wordBean.getContent(), wordBean.getComment(), wordBean.getRank(), wordBean.getHit(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") .format(new Date()), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") .format(new Date()) }); System.out.println("save word Info updateCount:" + updateCount); return (updateCount != 0); } public static boolean updateInfo(WordBean wordBean) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); System.out.println("jdbcTempalte:" + jdbcTempalte); int updateCount = jdbcTempalte .update("update words set content = ?, comment = ?, hit = ?, rank = ?, lastUpt = ? where id = ?", new Object[] { wordBean.getContent(), wordBean.getComment(), wordBean.getHit(), wordBean.getRank(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") .format(new Date()), wordBean.getId() }); System.out.println("updateInfo updateCount:" + updateCount); return (updateCount != 0); } public static void main(String[] args) { // String str = "abssdf中"; // System.out.println(str.matches("^[a-zA-Z]*")); String str = "1111? aaaaaa aaaaa,bbbb bbb bbbbbb.cccccccccc!ddddd ddd."; String[] arr = str.split("\\.|\\?|!"); for (int i = 0; i < arr.length; i++) { System.out.println(i + ":" + arr[i]); } } public static void main0(String[] args) throws Exception { MyFilter.isLocal = true; WordBean wordBean = new WordBean(); List<String> wordList = FileUtils.readLines(new File("words.txt")); int size = wordList.size(); // int size = 3; boolean isWordLine = true; for (int i = 0; i < size; i++) { String oneLine = StringUtils.trim(wordList.get(i)); System.out.println("###oneLine:" + oneLine + " isWordLine:" + isWordLine); if (StringUtils.isEmpty(oneLine)) { continue; } if (oneLine.length() == 1) { int firstChar = oneLine.charAt(0); // System.out.println(firstChar); if (firstChar >= 65 && firstChar <= 90) { continue; } } if (isWordLine) { if (!oneLine.matches("^[a-zA-Z]*")) { continue; } } if (isWordLine) { wordBean = new WordBean(); wordBean.setContent(oneLine); } else { wordBean.setComment(oneLine); DBUtil.saveInfo(wordBean); } isWordLine = !isWordLine; } } public static List<SentenBean> getSenList(String word) { JdbcTemplate jdbcTempalte = getJDBCTemplate(); String sql = "select * from sentences where content like '" + word.toLowerCase() + " %' or content like '% " + word.toLowerCase() + "' or content like '% " + word.toLowerCase() + " %' order by rank desc limit 0, 20"; System.out.println("###sql:" + sql); List<SentenBean> infoBeanList = jdbcTempalte.query(sql, new SentenItemMapper()); if(infoBeanList.isEmpty()) { sql = "select * from sentences where content like '%" + word.toLowerCase() + "%' order by rank desc limit 0, 20"; infoBeanList = jdbcTempalte.query(sql, new SentenItemMapper()); } return infoBeanList; } }
WordBean:
public String getComment() { if(comment != null && comment.length() >= 150) { comment = comment.substring(0, 145) + "..."; } return comment; }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.TimeZone; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import player.AudioBean; import player.LrcBean; import com.google.gson.Gson; /** * Servlet implementation class PlayerServlet */ @WebServlet(urlPatterns = "/play.do", asyncSupported = true) public class PlayerServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String SPLIT_TOKEN = "|||"; /** * @see HttpServlet#HttpServlet() */ public PlayerServlet() { super(); } public String getLrcNameFromSrc(String srcName) { if (StringUtils.isEmpty(srcName)) { return ""; } if (!srcName.contains(".")) { srcName += ".mp3"; } return srcName.substring(srcName.lastIndexOf("/") + 1, srcName.lastIndexOf(".")) + ".txt"; } public int getSecondsInt(String timeStr) { if (timeStr.length() == 5) { timeStr = "00:" + timeStr; } DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = null; try { date = dateFormat.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } long seconds = date.getTime() / 1000L; return (int) seconds; } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); String remoteAddr = request.getRemoteAddr(); System.out.println("###doPost### date:" + new Date() + " remoteAddr:" + remoteAddr); String action = request.getParameter("action"); String name = request.getParameter("name"); String startStr = request.getParameter("start"); String endStr = request.getParameter("end"); String textStr = request.getParameter("text"); String lastUptStr = request.getParameter("lastUpt"); if (StringUtils.isEmpty(textStr)) { textStr = " "; } textStr = StringUtils.replace(textStr, "\r", " "); textStr = StringUtils.replace(textStr, "\n", " "); name = getLrcNameFromSrc(name); System.out.println("action:" + action + " startStr:" + startStr + " endStr:" + endStr + " textStr:" + textStr); System.out.println("name:" + name); File lrcDir = new File("lrcDir"); if (!lrcDir.exists()) { lrcDir.mkdirs(); } File lrcFile = new File(lrcDir, name); File externalAudioList = new File(lrcDir, "externalAudio.txt"); System.out.println("##externalAudioList##:" + externalAudioList.getAbsolutePath()); // external files if (!externalAudioList.exists()) { externalAudioList.createNewFile(); } if ("add".equals(action)) { String msg = "0"; int start = getSecondsInt(startStr); int end = getSecondsInt(endStr); List<String> lineList = new ArrayList<String>(); if (lrcFile.exists()) { lineList = FileUtils.readLines(lrcFile, "UTF-8"); } System.out.println("###lrcDir:" + lrcDir.getAbsolutePath()); StringBuilder sb = new StringBuilder(); String lastestUptStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date()); String lineInfo = start + SPLIT_TOKEN + end + SPLIT_TOKEN + textStr + SPLIT_TOKEN + lastestUptStr + SPLIT_TOKEN + remoteAddr; boolean matched = false; for (int i = 0; i < lineList.size(); i++) { String oneLine = lineList.get(i); if (StringUtils.trim(oneLine).isEmpty()) { continue; } String[] fieldsArr = StringUtils.split(oneLine, SPLIT_TOKEN); // update data if (fieldsArr[0].equals(start + "")) { if(!oneLine.contains(SPLIT_TOKEN + lastUptStr)) { msg = "-1"; } oneLine = lineInfo; matched = true; } // insert data if (!matched && Integer.parseInt(fieldsArr[0]) > start) { matched = true; sb.append(lineInfo + "\r\n"); } sb.append(oneLine + "\r\n"); } if (lineList.size() == 0 || !matched) { sb.append(lineInfo + "\r\n"); } if("0".equals(msg)) { FileUtils.writeStringToFile(lrcFile, sb.toString(), "UTF-8"); FileUtils.writeStringToFile(new File(lrcDir, "historyLog_" + name), lineInfo + "\r\n", "UTF-8", true); } response.setContentType("application/json"); Gson gson = new Gson(); response.getWriter().print(gson.toJson(new String[]{msg, lastestUptStr})); response.getWriter().close(); } else if ("getLrcList".equals(action)) { List<String> lineList = new ArrayList<String>(); List<LrcBean> lrcBeanList = new ArrayList<LrcBean>(); if (lrcFile.exists() && lrcFile.isFile()) { lineList = FileUtils.readLines(lrcFile, "UTF-8"); } for (int i = 0; i < lineList.size(); i++) { String oneLine = lineList.get(i); if (StringUtils.trim(oneLine).isEmpty()) { continue; } String[] fieldsArr = StringUtils.split(oneLine, SPLIT_TOKEN); LrcBean lrcBean = new LrcBean(fieldsArr[0], fieldsArr[1], fieldsArr.length <= 2 ? "" : (StringUtils.isBlank(fieldsArr[2]) ? "" : fieldsArr[2]), fieldsArr[3]); lrcBeanList.add(lrcBean); } response.setContentType("application/json"); Gson gson = new Gson(); response.getWriter().print(gson.toJson(lrcBeanList)); response.getWriter().close(); } else if ("downloadLrc".equals(action)) { if (!lrcFile.exists()) { FileUtils.writeStringToFile(lrcFile, "", "UTF-8"); } response.setHeader("Content-disposition", "attachment; filename=" + lrcFile.getName()); BufferedInputStream buffInput = new BufferedInputStream( new FileInputStream(lrcFile.getAbsolutePath())); BufferedOutputStream buffout = new BufferedOutputStream( response.getOutputStream()); int length = -1; byte[] buff = new byte[1024]; while ((length = buffInput.read(buff)) != -1) { buffout.write(buff, 0, length); } buffout.flush(); buffInput.close(); buffout.close(); } else if ("getAudioList".equals(action)) { List<AudioBean> audioBeanList = new ArrayList<AudioBean>(); String rootPath = getServletConfig().getServletContext() .getRealPath("/"); // internal files File audioDir = new File(rootPath + "/audio"); for (int i = 0; i < audioDir.list().length; i++) { audioBeanList .add(new AudioBean(audioDir.list()[i], null, false)); } List<String> audioLines = FileUtils.readLines(externalAudioList, "UTF-8"); for (int i = 0; i < audioLines.size(); i++) { String oneAudioInfo = audioLines.get(i); if (StringUtils.isEmpty(oneAudioInfo)) { continue; } String[] oneAudioInfoArr = StringUtils.split(oneAudioInfo, SPLIT_TOKEN); audioBeanList.add(new AudioBean(oneAudioInfoArr[0], oneAudioInfoArr[1], true)); } response.setContentType("application/json"); Gson gson = new Gson(); response.getWriter().print(gson.toJson(audioBeanList)); response.getWriter().close(); } else if ("fetchRawMsg".equals(action)) { String fileContent = "N/A"; if (lrcFile.exists()) { fileContent = FileUtils.readFileToString(lrcFile, "UTF-8"); } response.getWriter().print(fileContent); response.getWriter().close(); } else if ("addExtFile".equals(action)) { String filename = request.getParameter("filename"); String fileurl = request.getParameter("fileurl"); FileUtils.writeStringToFile(externalAudioList, "\r\n" + filename + SPLIT_TOKEN + fileurl, "UTF-8", true); response.getWriter().print(1); response.getWriter().close(); } else if ("getWordDicList".equals(action)) { int currPage = Integer.parseInt(request.getParameter("page")); int rows = Integer.parseInt(request.getParameter("rows")); String _search = request.getParameter("_search"); String searchField = request.getParameter("searchField"); String searchOper = request.getParameter("searchOper"); String searchString = request.getParameter("searchString"); String sidx = request.getParameter("sidx"); String sord = request.getParameter("sord"); Object[] objArr = DBUtil.getWordDicList((currPage - 1) * rows, rows, sidx, sord, _search, searchField, searchOper, searchString); List<WordBean> wordBeanList = (List<WordBean>) objArr[0]; int totalCount = (Integer) objArr[1]; response.setContentType("application/json"); // Gson gson = new Gson(); // response.getWriter().print(gson.toJson(wordBeanList)); int totalNumberOfRecords = totalCount; int totalNumberOfPages = (totalCount % rows == 0 ? (totalCount / rows) : (totalCount / rows + 1)); JqGridData<WordBean> gridData = new JqGridData<WordBean>( totalNumberOfPages, currPage, totalNumberOfRecords, wordBeanList); System.out.println("Grid Data: " + gridData.getJsonString()); response.getWriter().print(gridData.getJsonString()); response.getWriter().close(); }else if ("getSenList".equals(action)) { String word = request.getParameter("word"); List<SentenBean> sentenBean = DBUtil.getSenList(word); int totalCount = sentenBean.size(); int rows = totalCount; int currPage = 1; response.setContentType("application/json"); // Gson gson = new Gson(); // response.getWriter().print(gson.toJson(wordBeanList)); int totalNumberOfRecords = totalCount; int totalNumberOfPages = 1; JqGridData<SentenBean> gridData = new JqGridData<SentenBean>( totalNumberOfPages, currPage, totalNumberOfRecords, sentenBean); System.out.println("Sentences Grid Data: " + gridData.getJsonString()); response.getWriter().print(gridData.getJsonString()); response.getWriter().close(); } else if ("addOrUpdateWord".equals(action)) { String id = request.getParameter("id"); String comment = request.getParameter("comment"); String content = request.getParameter("content"); String hit = request.getParameter("hit"); String oper = request.getParameter("oper");//edit String rank = request.getParameter("rank"); if("edit".equals(oper)) { WordBean wordBean = DBUtil.getWordBean(Integer.parseInt(id)); wordBean.setComment(comment); wordBean.setContent(content); wordBean.setHit(Integer.parseInt(hit)); wordBean.setRank(Integer.parseInt(rank)); DBUtil.updateInfo(wordBean); } else if("add".equals(oper)) { WordBean wordBean = new WordBean(); wordBean.setComment(comment); wordBean.setContent(content); wordBean.setHit(Integer.parseInt(hit)); wordBean.setRank(Integer.parseInt(rank)); DBUtil.saveInfo(wordBean); } else if("del".equals(oper)) { DBUtil.delInfo(id); } response.getWriter().print(""); response.getWriter().close(); }else if ("deleteWord".equals(action)) { } /* * else if ("saveRawMsg".equals(action)) { String fileContent = * request.getParameter("content"); FileUtils.writeStringToFile(lrcFile, * fileContent); * * response.getWriter().print("save successfully"); * response.getWriter().close(); } */ } }
import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; class SentenItemMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { SentenBean sentenBean = new SentenBean(rs.getInt("id"), rs.getString("content"), rs.getInt("rank")); return sentenBean; } }
word.js
function addDetail(oneWordBean) { var wordInfo = "<div>" + " id:" + oneWordBean.id + " content:" + oneWordBean.content + " comment:" + oneWordBean.comment + " hit:" + oneWordBean.hit + " rank:" + oneWordBean.rank + "</div>"; $("#wordsDiv").append(wordInfo); } var lastsel2; $(document) .ready( function() { jQuery("#grid-table") .jqGrid( { url : "play.do?action=getWordDicList", serializeEditData : function(data) { return $.param($.extend({}, data, { id : 0 })); }, datatype : "json", height : 470, colNames : [ 'id', 'content', 'comment', 'hit', 'rank', 'act', 'createDate', 'lastUpt' ], colModel : [ { name : 'id', index : 'id', width : 50, sorttype : "int", editable : false }, { name : 'content', index : 'content', width : 150, editable : true, editoptions : { size : "20", maxlength : "30" } }, { name : 'comment', index : 'comment', width : 150, editable : true, editoptions : { size : "20", maxlength : "30" } ,edittype:"textarea", editoptions:{rows:"2",cols:"10"} }, { name : 'hit', index : 'hit', width : 90, sorttype : "int", editable : true }, { name : 'rank', index : 'rank', width : 100, sorttype : "int", editable : true, edittype : "select", editoptions : { value : "1:Easy;2:Master;3:Familiar;4:NotFamiliar;5:Hard;-1:SeldomUsed;-2:Ignore;0:NotSet;-10:Broadcasted" } }, { name : 'act', index : 'act', width : 150, sortable : false }, { name : 'createDate', index : 'createDate', width : 150, sortable : true }, { name : 'lastUpt', index : 'lastUpt', width : 150, sortable : true }], onSelectRow : function(id) { if (id && id !== lastsel2) { jQuery('#grid-table').jqGrid( 'restoreRow', lastsel2); jQuery('#grid-table').jqGrid( 'editRow', id, true); lastsel2 = id; } }, viewrecords : true, rowNum : 20, // rowList:[10,20,30], pager : "#grid-pager", altRows : true, // toppager: true, multiselect : true, // multikey: "ctrlKey", multiboxonly : true, gridComplete : function() { var ids = jQuery("#grid-table") .jqGrid('getDataIDs'); // alert("ids:" + ids); for (var i = 0; i < ids.length; i++) { var cl = ids[i]; be = "<input style='height:22px;width:20px;' type='button' value='E' onclick=\"jQuery('#grid-table').editRow('" + cl + "');\" />"; se = "<input style='height:22px;width:20px;' type='button' value='S' onclick=\"jQuery('#grid-table').saveRow('" + cl + "');\" />"; ce = "<input style='height:22px;width:20px;' type='button' value='C' onclick=\"jQuery('#grid-table').restoreRow('" + cl + "');\" />"; jQuery("#grid-table").jqGrid( 'setRowData', ids[i], { act : ce + " " + be + " " + se }); } }, editurl : "play.do?action=addOrUpdateWord", caption : "Words List", subGrid: true, subGridRowExpanded: function(subgrid_id, row_id) { // we pass two parameters // subgrid_id is a id of the div tag created whitin a table data // the id of this elemenet is a combination of the "sg_" + id of the row // the row_id is the id of the row // If we wan to pass additinal parameters to the url we can use // a method getRowData(row_id) - which returns associative array in type name-value // here we can easy construct the flowing var pattern = /(id=\".+_content\")/ig; var rowData = jQuery("#grid-table").getRowData(row_id); var rowDataContent = rowData.content; if (rowDataContent.toLowerCase().indexOf('<input') >= 0) { rowDataContent = $('#' + row_id + '_content').val(); } // alert("rowData:" + rowData); var subgrid_table_id, pager_id; subgrid_table_id = subgrid_id+"_t"; pager_id = "p_"+subgrid_table_id; $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>"); jQuery("#"+subgrid_table_id).jqGrid({ url:"play.do?action=getSenList&word=" + rowDataContent, datatype: "json", colNames: ['ID','Content','Rank'], colModel: [ {name:"id",index:"id",width:80,key:true}, {name:"content",index:"content",width:750}, {name:"rank",index:"rank",width:70} ], rowNum:20, pager: pager_id, sortname: 'num', sortorder: "asc", height: '100%' }); jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false}) }, subGridRowColapsed: function(subgrid_id, row_id) { // this function is called before removing the data //var subgrid_table_id; //subgrid_table_id = subgrid_id+"_t"; //jQuery("#"+subgrid_table_id).remove(); } } // ,autowidth: true, /** * , grouping:true, groupingView : { * groupField : ['name'], groupDataSorted : * true, plusicon : 'fa fa-chevron-down * bigger-110', minusicon : 'fa * fa-chevron-up bigger-110' }, caption: * "Grouping" */ }); jQuery("#grid-table").jqGrid('navGrid', "#grid-pager", { edit : true, add : true, del : true }, { closeAfterEdit : true }, { closeAfterAdd : true }); });
- english_words.zip (243.9 KB)
- 下载次数: 1
发表评论
-
steve0603
2015-06-04 18:21 3410|||8|||SS will be late, traf ... -
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
2015-03-18 20:45 1358出现一个如题的错误,后来才知道原来是mysql密码错误造成的 ... -
TeamCity Plugin 安装 -- Static UI
2015-03-17 18:08 9841. 简介 With the help of th ... -
CAS 单点登录(Single Sign On)
2015-03-16 22:30 6091. 证书配置 a) 创建证书 ke ... -
spring-boot-sample-data-jpa
2015-02-25 07:54 440spring-boot-sample-data-jpa -
FileCopierWithCamel
2015-01-23 22:21 530package camelinaction; ... -
bob
2015-01-22 22:09 8aaaaaaaaaaaaaaaaaaaaaaaaaaa -
MyShell
2014-12-09 21:37 614import com.jcraft.jsch.*; ... -
info
2014-12-07 21:44 501ant lib http://stackoverflow.co ... -
Maven3应用入门session
2014-12-03 21:02 579Maven3应用入门session -
spring boot xsl
2014-12-01 21:59 442spring boot xsl -
How to fix GC overhead limit exceeded in Eclipse
2014-11-26 09:07 630https://docs.oseems.com/general ... -
truncate java string
2014-10-14 08:07 628/** Truncate a String to t ... -
javamelody
2014-09-28 07:56 346https://code.google.com/p/javam ... -
myresume
2014-08-17 22:17 0*2011年6月---至今 道富 作为Itels/Was ... -
interview
2014-08-11 07:46 30http://howtodoinjava.com/2013/0 ... -
resume
2014-07-23 22:05 0resume -
ttt
2014-07-02 14:17 0hhhh江河湖海红红火火宝贝宝贝宝贝好 -
SPA WEB
2014-04-26 22:53 418spa web -
SPA
2014-04-26 22:43 572antlr4 Cymbol.g4 javac Cymbo ...
相关推荐
player.on('stateChange', (event) => { console.log(`视频状态变更: ${event.data}`); }); // 控制播放 player.play(); player.pause(); player.stop(); // 销毁播放器 player.destroy(); ``` **五、总结** ...
- `TimeChange`:播放位置改变时触发,可用于实现进度条同步。 4. **方法**: - `Play()`:开始播放媒体。 - `Stop()`:停止播放。 - `Pause()`:暂停播放。 - `SeekToBegin()`:跳转到媒体的开头。 - `...
百度apollo lane_change_decider 详细注释代码
You can sort the tracks by the Title or Artist, as well as change the Equalizer. Does not yet support filtering by artist or album, stay tuned! It contains a widget with customizable text color, ...
### Windows Media Player 循环播放实现方法 在本篇文章中,我们将探讨如何使用 C# WinForms 实现 Windows Media Player 的循环播放功能。该知识点适用于希望为自己的应用程序添加媒体播放功能的开发人员,尤其是...
Java的`ActionListener`和`ChangeListener`接口常用于实现这一功能。 6. **线程管理**:音乐播放通常需要在后台线程进行,以免阻塞用户界面。Java的`Thread`类和`Runnable`接口可用于实现多线程,确保播放和UI交互...
MP3Player播放器是一款基于Flex技术实现的音频播放软件,主要功能是播放MP3格式的音频文件。在本文中,我们将深入探讨Flex技术以及如何利用它来开发一个高效的MP3播放器。 首先,Flex是一种开源的、基于XML的标记...
此扩展程序更改了Chrome中的默认媒体播放器。 新播放器提供了更舒适的控制面板以供使用。 此扩展程序更改了Chrome中的默认纯媒体播放器。 新播放器提供了更舒适的控制面板以供使用。 语言:English
, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "rate" && player.rate == 0 { // 播放暂停,展示广告 showAd() } } ``` 5. **广告展示**:广告的实现通常...
在探讨"C#_Windows_Media_Player控件的所有属性和方法"这一主题时,我们深入剖析了Windows Media Player(WMP)控件的核心功能与特性,该控件是Microsoft为开发人员提供的一种强大工具,用于在.NET框架下创建多媒体...
标题中的“macos13 ventura unlocker for vmware workstation/player 17”指的是一个工具,这个工具的主要目的是为了让VMware Workstation或Player 17能够支持安装和运行 macOS 13 Ventura操作系统。在虚拟化环境中...
最后,"CHANGELOG.txt"是一个关键文件,记录了Flourish Player自发布以来的所有版本更新和变更,这对于开发者跟踪项目的演进和解决潜在问题非常有用。 从技术角度来看,Flourish Player依赖于Flash技术,这是一种...
使用`<input type="range">`创建音量和进度滑块,通过`.change()`方法监听滑动事件并更新媒体状态。 5. **实现媒体操作**:通过JavaScript的Audio或Video对象的API,我们可以控制媒体的播放、暂停、音量、缓冲等...
Source code of registry manager.... 4)Change the Title of Windows Media Player. 5)Remove Recent Documents from the Start Menu. 6)Hide the Taskbar Clock. 7)Remove Username from the Start Menu.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary,id> *)change context:(void *)context { if ([keyPath isEqualToString:@"status"]) { AVPlayerStatus status = ...
2. **MIDI处理**:理解MIDI协议,包括MIDI消息类型(如Note On/Off,Controller Change等),以及如何将这些消息转换为声音。 3. **音频处理**:可能需要使用Android的AudioTrack或AudioRecord类来处理音频流,或者...
tb-video-player 版本:1.5 教程: demo: changelog V1.5 修复一个页面上有多个播放器同时存在引起的bug; V1.4 增加两个方法:play和pause,支持js处理播放器的播放和暂停; 修复一个拼写错误,万万不该,destroy...
document.getElementById('player-select').addEventListener('change', function() { let selectedPlayer = this.value; // 获取用户选择的球员 favorite_player = selectedPlayer; // 更新favorite_player }); `...
moviePlayer.addObserver(self, forKeyPath: "moviePlayer.status", options: .new, context: nil) ``` 在控制器中实现`observeValue(forKeyPath:of:change:context:)`方法,处理播放状态变化: ```swift override...