package com.nxt.datacenter.utils;
/**
* 文件描述
*
* @author: maomi
* @date: 2019/5/31
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 用于模拟HTTP请求中GET/POST方式
* @author landa
*
*/
public class HttpUtils {
/**
* 发送GET请求
*
* @param url 目的地址
* @param parameters 请求参数,Map类型。
* @return 远程响应结果
*/
public static String get(String url, Map parameters) {
String result = "";
// 读取响应输入流
BufferedReader in = null;
// // 存储参数
StringBuffer sb = new StringBuffer();
// // 编码之后的参数
String params = "";
try {
// 编码请求参数
if (parameters.size() == 1) {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name),
"UTF-8"));
}
params = sb.toString();
} else {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name),
"UTF-8")).append("&");
}
String temp_params = sb.toString();
params = temp_params.substring(0, temp_params.length() - 1);
}
String full_url = url + "?" + params;
System.out.println(full_url);
// 创建URL对象
java.net.URL connURL = new java.net.URL(full_url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 建立实际的连接
httpConn.connect();
// 响应头部获取
Map> headers = httpConn.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.out.println(key + "\t:\t" + headers.get(key));
}
// 定义BufferedReader输入流来读取URL的响应,并设置编码方式
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), "UTF-8"));
String line;
// 读取返回的内容
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* 发送POST请求
*
* @param url 目的地址
* @param parameters 请求参数,Map类型。
* @return 远程响应结果
*/
public static String post(String url, Map parameters) {
String result = "";// 返回的结果
BufferedReader in = null;// 读取响应输入流
PrintWriter out = null;
StringBuffer sb = new StringBuffer();// 处理请求参数
String params = "";// 编码之后的参数
try {
// 编码请求参数
if (parameters.size() == 1) {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name),
"UTF-8"));
}
params = sb.toString();
} else {
for (String name : parameters.keySet()) {
sb.append(name).append("=").append(
java.net.URLEncoder.encode(parameters.get(name),
"UTF-8")).append("&");
}
String temp_params = sb.toString();
params = temp_params.substring(0, temp_params.length() - 1);
}
// 创建URL对象
java.net.URL connURL = new java.net.URL(url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 设置POST方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
// 获取HttpURLConnection对象对应的输出流
out = new PrintWriter(httpConn.getOutputStream());
// 发送请求参数
out.write(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应,设置编码方式
in = new BufferedReader(new InputStreamReader(httpConn
.getInputStream(), "UTF-8"));
String line;
// 读取返回的内容
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* 主函数,测试请求
*
* @param args
*/
public static void main(String[] args) {
Map parameters = new HashMap();
parameters.put("AREA1", "sarin");
parameters.put("STARTTIME", "2019-05-31");
String result = post("http://jxtz.apn110.com/tuzaiChart/meatCert/meatCertData", parameters);
System.out.println(result);
}
}
package com.nxt.datacenter.task;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nxt.datacenter.config.FuwuConfig;
import com.nxt.datacenter.entity.FuwuClientEntity;
import com.nxt.datacenter.service.IFuwuClientService;
import com.nxt.datacenter.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* Cron表达式参数分别表示:
*
* 秒(0~59) 例如0/5表示每5秒
* 分(0~59)
* 时(0~23)
* 日(0~31)的某天,需计算
* 月(0~11)
* 周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
* @author liuliang
* @date 2019-05-30
*/
@Slf4j
@Component
@Configuration
@EnableScheduling //zhuanjiafuwuUrl
public class FuwuClientTask {
@Autowired
private FuwuConfig zhuanjiafuwuUrl;
@Autowired
private IFuwuClientService fuwuClientService;
//@Scheduled(cron = "0 * * * * ?") //方便调试
//@Scheduled(cron = "0/10 * * * * ? ") 测试10秒定时任务
@Scheduled(cron = "0 * * * * ?")
public void perDay() {
log.info("每天晚上1点任务-----------(12316专家服务)开始");
//12316专家服务:通过url拿数据
String expent = this.fuwuGetUrldata();
//12316专家服务:数据存入表中
this.fuwuSetTabledata(expent);
}
//12316专家服务:每天定时更新一次
public String fuwuGetUrldata(){
//拿到的数据
String ResultString="";
try {
String uboxSize = "";
//每次请求2页数据
for(Integer s = 1; s<3; s++){
Map prop = new HashMap();
prop.put("pageNum",String.valueOf(s));
String material = HttpUtils.get(zhuanjiafuwuUrl.getZhuanjiafuwuUrl(),prop);
if(material==null && material.length()==0 ){
return null;
}
//System.out.println("打印数据:" + material);
//将字符串转JSON
JSONObject jsStr = JSONObject.parseObject(material);
//定位到data内容
uboxSize = jsStr.getString("data");
uboxSize = uboxSize.substring(1,uboxSize.length()-1);
uboxSize += ",";
ResultString+=uboxSize;
}
ResultString ="["+ ResultString.substring(0,ResultString.length()-1)+"]";
//System.out.println("打印数据:" + ResultString);
} catch (Exception e) {
log.error("12316专家服务, 取数据错误:", e);
}
return ResultString;
}
public void fuwuSetTabledata(String newData){
try{
if(newData != null && newData.length() != 0) {
JSONArray json = JSONArray.parseArray(newData);
//清空老数据
fuwuClientService.removeTableIsOld();
for (Object obj : json) {
FuwuClientEntity fuwuClientEntity = new FuwuClientEntity();
JSONObject jo = (JSONObject) obj;
fuwuClientEntity.setId(Long.valueOf(jo.getString("id")));
fuwuClientEntity.setHinfo(jo.getString("hinfo"));
fuwuClientEntity.setTname(jo.getString("tname"));
fuwuClientEntity.setBiaoqian(jo.getString("biaoqian"));
fuwuClientEntity.setTbiaoqian(jo.getString("tbiaoqian"));
fuwuClientEntity.setHtime(jo.getString("htime"));
fuwuClientEntity.setHtype(jo.getString("htype"));
if (fuwuClientEntity.getHinfo() != null && fuwuClientEntity.getHinfo().length() > 0) {
Boolean bol = fuwuClientService.save(fuwuClientEntity);
if (bol) {
log.info("成功存储12316数据:" + fuwuClientEntity);
} else {
log.info("存储失败12316数据:" + fuwuClientEntity);
}
}
}
}
}catch (Exception e) {
log.error("12316专家服务, 存数据错误:", e);
}
}
}
application.yml
datacenter:
# 服务配置
fuwu:
# 12316专家咨询服务, 每天定时更新一次 - url
zhuanjiafuwu_url: http://rmt.118.aoyacms.com/plugin/aoya/notebook/wentilistapi.php
package com.nxt.datacenter.config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author: liuliang
* @date: 2019/5/31
*/
@Slf4j
@Component
@Data
public class FuwuConfig {
@Value("${datacenter.fuwu.zhuanjiafuwu_url}")
private String zhuanjiafuwuUrl;
}