player0109change



<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
});
});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值