java 采样_java 采集数据
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);
}
——業(yè)務(wù)邏輯層——
//抓取數(shù)據(jù)(可直接用)
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;
}
//可以將抓取的數(shù)據(jù)放到數(shù)據(jù)庫中
/**
* 插入數(shù)據(jù)
*
* @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("參數(shù)有空");
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;
}
}
希望可以幫到友友們,如果哪里不合適的也可以提出來,我也學習一下!
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java 采样_java 采集数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java jedis sadd_Java
- 下一篇: java分割面板_Split 面板分割