CpsNewCreate.java 4.78 KB
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;
    }
}