Skip to content

Commit bc3c2ce

Browse files
author
zhaoyuanli6
committed
批处理
1 parent b957f7b commit bc3c2ce

File tree

8 files changed

+584
-29
lines changed

8 files changed

+584
-29
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
</repositories>
3838

3939
<dependencies>
40+
41+
<dependency>
42+
<groupId>org.jsoup</groupId>
43+
<artifactId>jsoup</artifactId>
44+
<version>1.7.3</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.htmlparser</groupId>
49+
<artifactId>htmlparser</artifactId>
50+
<version>1.6</version>
51+
</dependency>
4052
<!-- 微软office处理 api -->
4153
<dependency>
4254
<groupId>org.apache.poi</groupId>
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.code.repository.study.http;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.client.methods.HttpGet;
9+
import org.apache.http.impl.client.HttpClients;
10+
import org.apache.http.util.EntityUtils;
11+
import org.jsoup.Jsoup;
12+
import org.jsoup.nodes.Document;
13+
import org.jsoup.nodes.Element;
14+
import org.jsoup.select.Elements;
15+
16+
import java.io.BufferedWriter;
17+
import java.io.File;
18+
import java.io.FileOutputStream;
19+
import java.io.OutputStreamWriter;
20+
import java.net.URI;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.concurrent.ExecutorService;
24+
import java.util.concurrent.Executors;
25+
26+
/**
27+
* @Author zhaoyuan.lizy on 2019/9/5
28+
**/
29+
30+
public class HscodeGatherer implements Runnable{
31+
32+
private int start=0;
33+
34+
private int end=0;
35+
36+
HscodeGatherer(int start,int end){
37+
this.start = start;
38+
this.end = end;
39+
}
40+
41+
42+
public static void main(String[] args){
43+
44+
ExecutorService executor = Executors.newCachedThreadPool();
45+
46+
47+
String url = "https://www.customs.gov.vn/SitePages/Tariff-Search.aspx?portlet=DetailsImportTax&language=en-US&code=";
48+
String hscode = "62043300";
49+
HscodeGatherer.generateCode(62,63);
50+
// 爬取数据
51+
// Map<String,String> result = HscodeGatherer.fetchInfo(url+hscode); // 爬取数据
52+
// result.put("hscode",hscode); // 填充hscode
53+
// System.out.println(JSON.toJSONString(result));
54+
// HscodeGatherer.writeToFile(result);// 写文件
55+
}
56+
57+
58+
@Override
59+
public void run() {
60+
this.generateCode(start,end);
61+
}
62+
63+
private static String generateCode(int start,int end){ // 开头两位 01-97,一共8位
64+
if(start<=0){
65+
start=1;
66+
}
67+
if(end>99){
68+
end=99;
69+
}
70+
int start1 = start;
71+
int start2 = 1;
72+
int start3 = 1;
73+
int start4 = 0;
74+
for(;start1<=end;start1++){
75+
for(;start2 <=99;start2++){
76+
for(;start3 <=99;start3++){
77+
for(;start4 <=99;start4++){
78+
String hscode = HscodeGatherer.numToStr(start1)+HscodeGatherer.numToStr(start2)+HscodeGatherer.numToStr(start3)+HscodeGatherer.numToStr(start4);
79+
System.out.println("=====hscode:"+hscode);
80+
String url = "https://www.customs.gov.vn/SitePages/Tariff-Search.aspx?portlet=DetailsImportTax&language=en-US&code=";
81+
Map<String,String> result = HscodeGatherer.fetchInfo(url+hscode); // 爬取数据
82+
if(result == null){
83+
System.out.println("=====no result:");
84+
continue;
85+
}
86+
result.put("hscode",hscode); // 填充hscode
87+
System.out.println(JSON.toJSONString(result));
88+
HscodeGatherer.writeToFile(result);// 写文件
89+
}
90+
}
91+
}
92+
}
93+
return null;
94+
}
95+
96+
// 写文件
97+
private static void writeToFile(Map<String,String> result){
98+
String fileName = "D:\\hscodeTH.txt";
99+
BufferedWriter out = null;
100+
try {
101+
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(new File(fileName),true),"UTF-8");
102+
out = new BufferedWriter(ow);
103+
out.newLine();
104+
out.write(result.get("hscode")+";"+result.get("Favour")+";"+result.get("ASEAN - China  (ACFTA)"));
105+
out.flush();
106+
out.close();
107+
} catch (Exception e) {
108+
}finally{
109+
}
110+
}
111+
112+
private static String numToStr(int num){
113+
if(num<9){
114+
return "0"+num;
115+
}else{
116+
return String.valueOf(num);
117+
}
118+
}
119+
120+
private static Map<String,String> fetchInfo(String url){
121+
122+
String html = HscodeGatherer.doGet(url);
123+
if(StringUtils.isBlank(html)){
124+
return null;
125+
}
126+
if(html.contains("Not found")){ // Not found
127+
System.out.println("===Not found:");
128+
return null;
129+
}
130+
Document doc = Jsoup.parse(html);// 结构化解析
131+
Elements tables = doc.select("table");
132+
for(Element eTable : tables){
133+
if (eTable.text().contains("Tax rate")) {// 锁定税率列表,提取各种税率
134+
Map<String,String> result = new HashMap<>();
135+
Elements trs = eTable.select("tr");
136+
for(Element tr:trs){
137+
Elements tds = tr.select("td");
138+
if(tds.size()<2){
139+
continue;
140+
}
141+
if(StringUtils.isNotBlank(tds.get(1).text())){ // 税率列不为空,记录
142+
result.put(tds.get(0).text(),tds.get(1).text());
143+
}
144+
}
145+
return result;
146+
}else{
147+
continue;
148+
}
149+
}
150+
return null;
151+
}
152+
153+
private static String doGet(String url){
154+
HttpClient client = HttpClients.createDefault();
155+
HttpGet request = new HttpGet();
156+
try {
157+
// 初始化post请求头
158+
request.addHeader("Content-Type", "text/html; charset=utf-8");
159+
request.setURI(new URI(url));
160+
HttpResponse response = client.execute(request);
161+
// 解析结果
162+
int code = response.getStatusLine().getStatusCode();
163+
if (code == 200) { // 请求成功
164+
HttpEntity entity = response.getEntity();
165+
return EntityUtils.toString(entity, "utf-8");
166+
}else {
167+
System.out.println("===http status:"+code);
168+
}
169+
}catch(Exception e){
170+
}
171+
return null;
172+
}
173+
174+
175+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.code.repository.study.http;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import org.apache.http.HttpEntity;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.NameValuePair;
7+
import org.apache.http.client.HttpClient;
8+
import org.apache.http.client.entity.UrlEncodedFormEntity;
9+
import org.apache.http.client.methods.HttpPost;
10+
import org.apache.http.entity.StringEntity;
11+
import org.apache.http.impl.client.HttpClients;
12+
import org.apache.http.message.BasicNameValuePair;
13+
import org.apache.http.util.EntityUtils;
14+
15+
import java.io.UnsupportedEncodingException;
16+
import java.net.URI;
17+
import java.security.NoSuchAlgorithmException;
18+
import java.util.*;
19+
20+
/**
21+
* @Author zhaoyuan.lizy on 2019/7/25
22+
**/
23+
24+
public class HttpClientDingTalkTest {
25+
26+
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
27+
28+
Map<String, String> params = new HashMap<String, String>();
29+
params.put("bizType", "MSG_DIRECT_EXPORT_CALLBACK_BIZ_TYPE");
30+
params.put("formatType", "1");
31+
params.put("bizKey", "LP00096553139264");
32+
33+
// content内容
34+
String msg = "{\n" +
35+
" \"msgtype\": \"text\", \n" +
36+
" \"text\": {\n" +
37+
" \"content\": \"\"\n" +
38+
" }, \n" +
39+
" \"msgId\": \"123456\", \n" +
40+
" \"createAt\": 1487561654123,\n" +
41+
" \"conversationType\": \"2\", \n" +
42+
" \"conversationId\": \"12345\", \n" +
43+
" \"conversationTitle\": \"钉钉群\", \n" +
44+
" \"senderId\": \"dingtalk1235\", \n" +
45+
" \"senderNick\": \"星星\",\n" +
46+
" \"chatbotUserId\":\"XXX\",\n" +
47+
" \"atUsers\":[\n" +
48+
" {\n" +
49+
" \"dingtalkId\":\"testdingid234\" \n" +
50+
" },\n" +
51+
" {\n" +
52+
" \"dingtalkId\":\"testdingid444\" \n" +
53+
" }\n" +
54+
" ]\n" +
55+
"}";
56+
57+
JSONObject jsonMsg = JSONObject.parseObject(msg);
58+
59+
String url = "https://pre-partner.customs.cainiao.com/robot/cbe";
60+
// String url = "https://partner.customs.cainiao.com/robot/cbe";
61+
// String url = "https://pre-partner.customs.cainiao.com/checkpreload.htm";
62+
63+
64+
String token = "sadfwer22ssfgCUSTOMS_13468211___sdf22dg";
65+
System.out.println("服务器的响应是:"+HttpClientDingTalkTest.doPost(url, token,jsonMsg));
66+
}
67+
68+
/**
69+
* post请求
70+
*/
71+
private static String doPost(String url, String token , JSONObject params) {
72+
// 定义HttpClient
73+
HttpClient client = HttpClients.createDefault();
74+
// 实例化HTTP方法
75+
HttpPost request = new HttpPost();
76+
try {
77+
// 初始化post请求头
78+
request.addHeader("Content-Type","application/json; charset=utf-8");
79+
request.addHeader("token",token);
80+
request.setURI(new URI(url));
81+
// 设置参数
82+
StringEntity stringEntity = new StringEntity(params.toString(), "UTF-8");
83+
stringEntity.setContentEncoding("UTF-8");
84+
request.setEntity(stringEntity);
85+
// 发起调用
86+
HttpResponse response = client.execute(request);
87+
// 解析结果
88+
int code = response.getStatusLine().getStatusCode();
89+
if (code == 200) { // 请求成功
90+
HttpEntity entity=response.getEntity();
91+
return EntityUtils.toString(entity, "utf-8");
92+
} else {
93+
System.out.println("状态码:" + code);
94+
return null;
95+
}
96+
} catch (Exception e) {
97+
e.printStackTrace();
98+
return null;
99+
}finally{
100+
// 释放连接
101+
request.releaseConnection();
102+
}
103+
}
104+
}

src/main/java/com/code/repository/study/http/HttpClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class HttpClientTest {
3737
// params.put("content", json.toJSONString());
3838
// params.put("dataDigest", HttpClientTest.doSign(json.toJSONString(), "UTF-8", "123"));
3939
//
40-
// String url = "https://linkdaily.tbsandbox.com/gateway/custom/customsCommonPlgin?msg_type=GLOBAL_CUSTOMS_DECLARE_CALLBACK&from_code=hzdirect_001";
40+
// String url = "https://pre-partner.customs.cainiao.com/robot/cbe";
4141
// System.out.println("服务器的响应是:"+HttpClientTest.doPost(url, params));
4242
// }
4343

src/main/java/com/code/repository/study/proxy/cglib/CglibProxy.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,28 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
5353
}
5454

5555
public static void main(String[] args) {
56-
CglibProxy cglibProxy = new CglibProxy();
57-
NoFaceAction NoFaceActionProxy = (NoFaceAction) cglibProxy.getInstance(new NoFaceAction());
58-
59-
NoFaceActionProxy.doAction();
60-
61-
NoFaceActionProxy.getNumber();
56+
// CglibProxy cglibProxy = new CglibProxy();
57+
// NoFaceAction NoFaceActionProxy = (NoFaceAction) cglibProxy.getInstance(new NoFaceAction());
58+
//
59+
// NoFaceActionProxy.doAction();
60+
//
61+
// NoFaceActionProxy.getNumber();
62+
63+
CglibProxy cglibProxy = new CglibProxy(); // 创建代理实现类
64+
65+
NoFaceAction action = new NoFaceAction(); // 创建被代理业务类
66+
67+
Enhancer enhancer = new Enhancer(); // 创建加强器,用来生成代理类
68+
69+
enhancer.setSuperclass(action.getClass());// 设置代理类的父类:CGlib动态代理是通过继承业务类,生成的动态代理类是业务类的子类,通过重写业务方法进行代理;
70+
71+
enhancer.setCallback(cglibProxy);// 回调:对于代理类上所有方法的调用,都会调用CallBack,而Callback则需要实现intercept()方法进行拦
72+
73+
NoFaceAction actionProxy = (NoFaceAction)enhancer.create();// 创建代理类对象
74+
75+
// 使用代理类调用
76+
actionProxy.doAction();
77+
6278
}
6379

6480
}

0 commit comments

Comments
 (0)