CpsNewCreate.java
4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.yoho.unions.shoudong;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* 批量创建cps有赚用户
* Created by mingdan.ge on 2019/10/21.
*/
public class CpsNewCreate {
private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
private static final String APPLICATION_JSON = "application/json;charset=utf-8";
public static void main(String[] ags) {
String readFile="E:\\union\\newcps\\20191119newcps.txt";
String url="http://union.yoho.cn/union/UnionShareRest/userApply";
// String url="http://java-yoho-union.test3.ingress.dev.yohocorp.com/union/UnionShareRest/userApply";
// String url="http://localhost/union/UnionShareRest/userApply";
int max=600000;
long start = System.currentTimeMillis();
CpsNewCreate.readFileByLinesToHttp(readFile,url,max);
long end = System.currentTimeMillis();
System.out.println("total time is "+(end-start)/1000);
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static int readFileByLinesToHttp(String readFile,String url,int max) {
BufferedReader reader = null;
int line = 1;
try {
// System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(readFile));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null&&line<max) {
if (tempString.trim().length() > 0) {
JSONObject u = new JSONObject();
String[] strings = tempString.trim().split(",");
u.put("uid", strings[0]);
// u.put("name", "有赚达人");
// u.put("mobile", strings[0]);
String result = httpPostWithJSON(url, u);
JSONObject r = JSONObject.parseObject(result);
if (r.getInteger("code") !=800010011) {
try {
System.out.println("sleep..");
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("line " + line+",result=" +result );
}
line++;
}
reader.close();
} catch (IOException e) {
// 显示行号
System.out.println("line " + line + ": " +e.getMessage() );
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return line;
}
public static String httpPostWithJSON(String url, JSONObject params) {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(url);
try {
if (params != null) {
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
StringEntity se = new StringEntity(params.toJSONString(),"UTF-8");
se.setContentType(CONTENT_TYPE_TEXT_JSON);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httpPost.setEntity(se);
}
HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 判断响应实体是否为空
if (entity != null) {
return EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}