APP接口
——controller层——
public class WenAnBianJiController {
@Resource
DicAnalysisService service;
/**
* 添加
*
* @param pkphone
* @param address
* @return
*/
@RequestMapping("add")
@ResponseBody
public ResultData saveDicAnaLySis(String pkphone, String address) {
return service.addDicAnalysis(pkphone, address);
}
——业务逻辑层——
//抓取数据(可直接用)
List list;
public static List getURLCollection(String address) {
List list = new LinkedList();
try {
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.connect();
InputStream in = conn.getInputStream();
InputStreamReader input = new InputStreamReader(in, "utf-8");
BufferedReader buf = new BufferedReader(input);
String nextLine = buf.readLine();
while (nextLine != null) {
list.add(nextLine);
nextLine = buf.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//可以将抓取的数据放到数据库中
/**
* 插入数据
*
* @param address
* @return
*/
public ResultData addDicAnalysis(String pkphone, String address) {
// public static List getURLCollection(String address) {
ResultData result = new ResultData();
if (address == null || address == "" || pkphone == null
|| pkphone == "") {
result.setMsg("参数有空");
result.setStatus(1);
return result;
}
// ResultData listss = addDicAnalysis(address, pkphone);
// "http://news.china.com/news100/11038989/20170508/30497947.html";
List list4 = getURLCollection(address);
String buf = "";
String bufs = "";
for (String str : list4) {
buf += str + "\n";
bufs += str;
}
DicAnalysisDTO lists = new DicAnalysisDTO();
lists.setPkphone(pkphone);
lists.setUrl(address);
lists.setContent(bufs);
dao.addDicAnalysis(lists);
// DicAnalysisDTO a = new DicAnalysisDTO();
// a.setContent(bufs);
// a.setPkphone(pkphone);
// a.setUrl(address);
// dao.addDicAnalysis();
result.setMsg("success");
result.setStatus(0);
return result;
}
}
希望可以帮到友友们,如果哪里不合适的也可以提出来,我也学习一下!