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

}

——业务逻辑层——

//抓取数据(可直接用)

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;

}

}

希望可以帮到友友们,如果哪里不合适的也可以提出来,我也学习一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值