├── .gitignore ├── jars └── json-20080701.jar ├── jmx ├── OrderJRequest.jmx └── order_users.csv ├── pom.xml └── src └── main ├── java └── com │ └── test │ ├── common │ ├── HttpClient.java │ └── SSLClient.java │ ├── stress │ └── JHttpRequestSample.java │ └── utils │ ├── JSONParser.java │ └── Tools.java └── resources └── log4j2.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | .DS_Store 4 | target/ 5 | *.swp 6 | *.swo 7 | *.txt12 8 | *.log 9 | logs/ 10 | /target 11 | .project 12 | .vagrant 13 | .classpath 14 | .settings 15 | *.key 16 | ~*.doc 17 | *.pdb 18 | dependency-reduced-pom.xml 19 | ~$*.pptx 20 | myTestSpring/notify-subscription.log.* 21 | *.log.* 22 | *.txt 23 | automation/PreClassMenu_auto_*.jmx 24 | automation/test_*.jtl 25 | -------------------------------------------------------------------------------- /jars/json-20080701.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/princeqjzh/iJava_Request/25d8c82948266372c1947927de472e9ea3cf269d/jars/json-20080701.jar -------------------------------------------------------------------------------- /jmx/OrderJRequest.jmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | false 7 | true 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | continue 17 | 18 | false 19 | 1 20 | 21 | 20 22 | 1 23 | false 24 | 120 25 | 26 | true 27 | 28 | 29 | 30 | false 31 | 32 | saveConfig 33 | 34 | 35 | true 36 | true 37 | true 38 | 39 | true 40 | true 41 | true 42 | true 43 | false 44 | true 45 | true 46 | false 47 | false 48 | false 49 | true 50 | false 51 | false 52 | false 53 | true 54 | 0 55 | true 56 | true 57 | true 58 | true 59 | true 60 | true 61 | 62 | 63 | 64 | 65 | 66 | 67 | false 68 | 69 | saveConfig 70 | 71 | 72 | true 73 | true 74 | true 75 | 76 | true 77 | true 78 | true 79 | true 80 | false 81 | true 82 | true 83 | false 84 | false 85 | false 86 | true 87 | false 88 | false 89 | false 90 | true 91 | 0 92 | true 93 | true 94 | true 95 | true 96 | true 97 | true 98 | 99 | 100 | 101 | 102 | 103 | 104 | , 105 | UTF-8 106 | ./order_users.csv 107 | true 108 | false 109 | true 110 | shareMode.all 111 | false 112 | no,username,password 113 | 114 | 115 | 116 | 117 | 118 | port 119 | 9091 120 | = 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | hostname 130 | localhost 131 | = 132 | 133 | 134 | 135 | com.test.stress.JHttpRequestSample 136 | 137 | 138 | 139 | 140 | 141 | 142 | influxdbMetricsSender 143 | org.apache.jmeter.visualizers.backend.influxdb.HttpMetricsSender 144 | = 145 | 146 | 147 | influxdbUrl 148 | http://localhost:8086/write?db=jmeter 149 | = 150 | 151 | 152 | application 153 | Java_Request 154 | = 155 | 156 | 157 | measurement 158 | jmeter 159 | = 160 | 161 | 162 | summaryOnly 163 | true 164 | = 165 | 166 | 167 | samplersRegex 168 | .* 169 | = 170 | 171 | 172 | percentiles 173 | 90;95;99 174 | = 175 | 176 | 177 | testTitle 178 | JavaRequest Sample 179 | = 180 | 181 | 182 | eventTags 183 | 184 | = 185 | 186 | 187 | 188 | org.apache.jmeter.visualizers.backend.influxdb.InfluxdbBackendListenerClient 189 | 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /jmx/order_users.csv: -------------------------------------------------------------------------------- 1 | No.,username,password 2 | 1,user01,pwd 3 | 2,user02,pwd 4 | 3,user03,pwd 5 | 4,user04,pwd 6 | 5,user05,pwd 7 | 6,user06,pwd 8 | 7,user07,pwd 9 | 8,user08,pwd 10 | 9,user09,pwd 11 | 10,user10,pwd -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.test.demo 5 | jmeter_java 6 | jar 7 | 1.0 8 | jmeter_java 9 | http://maven.apache.org 10 | 11 | UTF-8 12 | 4.0.0.RELEASE 13 | 4.3.0.Final 14 | 3.2.1 15 | 16 | 17 | 18 | 19 | org.apache.jmeter 20 | ApacheJMeter_java 21 | 5.1.1 22 | 23 | 24 | 25 | org.apache.jmeter 26 | ApacheJMeter_core 27 | 5.1.1 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-antrun-plugin 32 | 1.8 33 | 34 | 35 | org.json 36 | json 37 | 20080701 38 | 39 | 40 | 41 | 42 | jmeter_java 43 | 44 | 45 | org.apache.maven.plugins 46 | maven-compiler-plugin 47 | 3.3 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-jar-plugin 52 | 53 | 54 | 55 | maven-antrun-plugin 56 | 57 | 58 | copy 59 | package 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | run 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /src/main/java/com/test/common/HttpClient.java: -------------------------------------------------------------------------------- 1 | package com.test.common; 2 | 3 | import org.apache.http.HttpEntity; 4 | import org.apache.http.HttpResponse; 5 | import org.apache.http.client.methods.CloseableHttpResponse; 6 | import org.apache.http.client.methods.HttpDelete; 7 | import org.apache.http.client.methods.HttpGet; 8 | import org.apache.http.client.methods.HttpPost; 9 | import org.apache.http.conn.ConnectTimeoutException; 10 | import org.apache.http.entity.ContentType; 11 | import org.apache.http.entity.StringEntity; 12 | import org.apache.http.impl.client.CloseableHttpClient; 13 | import org.apache.http.impl.client.HttpClients; 14 | import org.apache.http.params.CoreConnectionPNames; 15 | import org.apache.http.util.EntityUtils; 16 | 17 | import java.net.SocketTimeoutException; 18 | 19 | public class HttpClient { 20 | private static int waitTime = 30; //30 秒超时 21 | 22 | public static String sendGet(String url, String access_token) { 23 | 24 | String charset = "utf-8"; 25 | org.apache.http.client.HttpClient httpClient = null; 26 | HttpGet httpGet = null; 27 | String result = null; 28 | 29 | try { 30 | httpClient = new SSLClient(); //创建客户端client实例 31 | httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, waitTime * 1000); 32 | httpGet = new HttpGet(url); 33 | httpGet.setHeader("accesstoken", access_token); //配置Header 34 | 35 | HttpResponse response = httpClient.execute(httpGet); //执行get请求 36 | if (response != null) { 37 | HttpEntity resEntity = response.getEntity(); //获取响应实体 38 | if (resEntity != null) { 39 | result = EntityUtils.toString(resEntity, charset); //解析响应实体内容 40 | } 41 | } 42 | } catch (SocketTimeoutException ste) { 43 | result = "response timeout!"; 44 | 45 | } catch (ConnectTimeoutException cte) { 46 | result = "response timeout!"; 47 | 48 | } catch (Exception e) { 49 | e.printStackTrace(); 50 | result = "Server internal error!"; 51 | } 52 | 53 | return result; 54 | } 55 | 56 | public static String sendPost(String url, String data, String access_token) { 57 | String response = null; 58 | String contentType = "application/json"; 59 | try { 60 | CloseableHttpClient httpclient = null; 61 | CloseableHttpResponse httpresponse = null; 62 | try { 63 | httpclient = HttpClients.createDefault(); //创建HttpClient对象 64 | HttpPost httppost = new HttpPost(url); //创建post请求对象 65 | 66 | //配置post请求 67 | StringEntity stringentity = new StringEntity(data, 68 | ContentType.create(contentType, "UTF-8")); 69 | httppost.setEntity(stringentity); 70 | httppost.setHeader("accesstoken", access_token); 71 | 72 | //执行post请求 73 | httpresponse = httpclient.execute(httppost); 74 | 75 | //获取post请求响应实体 76 | response = EntityUtils 77 | .toString(httpresponse.getEntity()); 78 | } finally { 79 | if (httpclient != null) { 80 | httpclient.close(); 81 | } 82 | if (httpresponse != null) { 83 | httpresponse.close(); 84 | } 85 | } 86 | } catch (Exception e) { 87 | e.printStackTrace(); 88 | } 89 | return response; 90 | } 91 | 92 | public static String sendDelete(String url, String access_token) { 93 | String response = null; 94 | 95 | try { 96 | CloseableHttpClient httpclient = null; 97 | CloseableHttpResponse httpresponse = null; 98 | try { 99 | httpclient = HttpClients.createDefault(); //创建Client对象 100 | HttpDelete httpdelete = new HttpDelete(url); //创建Delete请求 101 | httpdelete.setHeader("accesstoken", access_token);//配置delete请求 102 | httpresponse = httpclient.execute(httpdelete); //执行delete请求 103 | response = EntityUtils 104 | .toString(httpresponse.getEntity()); //获取响应返回实体 105 | } finally { 106 | if (httpclient != null) { 107 | httpclient.close(); 108 | } 109 | if (httpresponse != null) { 110 | httpresponse.close(); 111 | } 112 | } 113 | } catch (Exception e) { 114 | e.printStackTrace(); 115 | } 116 | return response; 117 | } 118 | 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/com/test/common/SSLClient.java: -------------------------------------------------------------------------------- 1 | package com.test.common; 2 | 3 | import org.apache.http.conn.ClientConnectionManager; 4 | import org.apache.http.conn.scheme.Scheme; 5 | import org.apache.http.conn.scheme.SchemeRegistry; 6 | import org.apache.http.conn.ssl.SSLSocketFactory; 7 | import org.apache.http.impl.client.DefaultHttpClient; 8 | 9 | import javax.net.ssl.SSLContext; 10 | import javax.net.ssl.TrustManager; 11 | import javax.net.ssl.X509TrustManager; 12 | import java.security.cert.CertificateException; 13 | import java.security.cert.X509Certificate; 14 | 15 | public class SSLClient extends DefaultHttpClient { 16 | public SSLClient() throws Exception { 17 | super(); 18 | SSLContext ctx = SSLContext.getInstance("TLS"); 19 | X509TrustManager tm = new X509TrustManager() { 20 | 21 | public void checkClientTrusted(X509Certificate[] chain, 22 | String authType) throws CertificateException { 23 | } 24 | 25 | 26 | public void checkServerTrusted(X509Certificate[] chain, 27 | String authType) throws CertificateException { 28 | } 29 | 30 | 31 | public X509Certificate[] getAcceptedIssuers() { 32 | return null; 33 | } 34 | }; 35 | ctx.init(null, new TrustManager[]{tm}, null); 36 | SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 37 | ClientConnectionManager ccm = this.getConnectionManager(); 38 | SchemeRegistry sr = ccm.getSchemeRegistry(); 39 | sr.register(new Scheme("https", 443, ssf)); 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/com/test/stress/JHttpRequestSample.java: -------------------------------------------------------------------------------- 1 | package com.test.stress; 2 | 3 | import com.test.common.HttpClient; 4 | import com.test.utils.JSONParser; 5 | import org.apache.jmeter.config.Arguments; 6 | import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 7 | import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 8 | import org.apache.jmeter.samplers.SampleResult; 9 | import org.apache.jmeter.threads.JMeterVariables; 10 | 11 | public class JHttpRequestSample extends AbstractJavaSamplerClient { 12 | /** 13 | * Get default parameter from the java request sampler. 14 | * @return arguments 15 | */ 16 | @Override 17 | public Arguments getDefaultParameters() { 18 | Arguments params = new Arguments(); 19 | params.addArgument("hostname","default_host"); 20 | return params; 21 | } 22 | 23 | /** 24 | * Pre-test method. (Optional) 25 | * 26 | * @param context 27 | */ 28 | public void setupTest(JavaSamplerContext context) { 29 | System.out.println("==== Restful API test started! ===="); 30 | } 31 | 32 | /** 33 | * Post-test method. (Optional) 34 | * 35 | * @param context 36 | */ 37 | public void teardownTest(JavaSamplerContext context) { 38 | System.out.println("==== Restful API test stopped. ===="); 39 | } 40 | 41 | /** 42 | * Test running method. (Required) 43 | * 44 | * @param arg0 45 | * @return 46 | */ 47 | public SampleResult runTest(JavaSamplerContext arg0) { 48 | //从JMeter的全局变量中获取参数值 49 | JMeterVariables jMeterVariables = arg0.getJMeterVariables(); 50 | String port = jMeterVariables.get("port"); 51 | String username = jMeterVariables.get("username"); 52 | String password = jMeterVariables.get("password"); 53 | 54 | //从Java Request变量列表中获取参数值 55 | String hostname = arg0.getParameter("hostname"); 56 | 57 | System.out.println("== JMeter Variables:"); 58 | System.out.println("hostname = " + hostname); 59 | System.out.println("port = " + port); 60 | System.out.println("username = " + username); 61 | System.out.println("password = " + password); 62 | 63 | System.out.println("== Test Start with user " + username); 64 | SampleResult sampleResult = new SampleResult(); 65 | sampleResult.sampleStart(); 66 | //运行接口测试组合代码,接口1 -> 接口4 67 | boolean testResult = menuRestfulAPITest(hostname, port, username, password); 68 | if (testResult) { 69 | sampleResult.setSuccessful(true); //设定成功条件下的Java Request 结果为成功 70 | String succMsg = "OrderService restfulAPI test success."; 71 | sampleResult.setResponseData(succMsg.getBytes()); 72 | System.out.println(succMsg); 73 | 74 | } else { 75 | sampleResult.setSuccessful(false); //设定成功条件下的Java Request 结果为失败 76 | String failMsg = "OrderService restfulAPI test failed."; 77 | sampleResult.setResponseData(failMsg.getBytes()); 78 | System.out.println(failMsg); 79 | } 80 | sampleResult.sampleEnd(); 81 | return sampleResult; 82 | } 83 | 84 | public static void main(String[] args) { 85 | //实验代码 86 | String hostname = "localhost"; 87 | String port = "9091"; 88 | String username = "user01"; 89 | String password = "pwd"; 90 | boolean result = JHttpRequestSample.menuRestfulAPITest(hostname, port, username, password); 91 | System.out.println("最终结果:" + result); 92 | } 93 | 94 | /** 95 | * 测试请求实际运行 96 | * @param hostname: Host or IP 97 | * @param port: 端口 98 | * @param username: 用户名 99 | * @param password: 密码 100 | * @return 101 | */ 102 | public static boolean menuRestfulAPITest(String hostname, String port, String username, String password) { 103 | String protocal = "http"; 104 | String access_token = ""; 105 | 106 | //接口1 登录操作 107 | String path1 = "/api/v1/user/login"; 108 | String url1 = protocal + "://" + hostname + ":" + port + path1; 109 | String reqData1 = "{\n" + 110 | "\t\"authRequest\": {\n" + 111 | "\t \"userName\": \"" + username + "\",\n" + 112 | "\t \"password\": \"" + password + "\"\n" + 113 | "\t}\n" + 114 | "}"; 115 | String respData1 = HttpClient.sendPost(url1, reqData1, access_token); 116 | access_token = JSONParser.getJsonValue(respData1, "access_token"); 117 | String retcode1 = JSONParser.getJsonValue(respData1, "code"); 118 | if (!"200".equalsIgnoreCase(retcode1)) { //校验接口1的返回code是否等于200 119 | System.out.println("1. 登录接口请求失败!" + " return code = " + retcode1); 120 | return false; 121 | } 122 | System.out.println("1. 登录接口请求成功"); 123 | 124 | //接口2 浏览菜单 125 | String path2 = "/api/v1/menu/list"; 126 | String url2 = protocal + "://" + hostname + ":" + port + path2; 127 | String respData2 = HttpClient.sendGet(url2, access_token); 128 | String retcode2 = JSONParser.getJsonValue(respData2, "code"); 129 | if (!"200".equalsIgnoreCase(retcode2)) { //校验接口2的返回code是否等于200 130 | System.out.println("2. 浏览菜单接口请求失败" + " return code = " + retcode2); 131 | return false; 132 | } 133 | System.out.println("2. 浏览菜单接口请求成功"); 134 | 135 | //接口3 下订单 136 | String path3 = "/api/v1/menu/confirm"; 137 | String url3 = protocal + "://" + hostname + ":" + port + path3; 138 | String reqData3 = "{\n" + 139 | " \"order_list\": [\n" + 140 | " {\n" + 141 | " \"menu_nunber\" : \"01\",\n" + 142 | " \"number\" : 1\n" + 143 | " },\n" + 144 | " {\n" + 145 | " \"menu_nunber\" : \"03\",\n" + 146 | " \"number\" : 2\n" + 147 | " }\n" + 148 | " ]\n" + 149 | "}"; 150 | String respData3 = HttpClient.sendPost(url3, reqData3, access_token); 151 | String retcode3 = JSONParser.getJsonValue(respData3, "code"); 152 | if (!"200".equalsIgnoreCase(retcode3)) { //校验接口3的返回code是否等于200 153 | System.out.println("3. 下订单接口请求失败" + " return code = " + retcode3); 154 | return false; 155 | } 156 | System.out.println("3. 下订单接口请求成功"); 157 | 158 | //接口4 退出 159 | String path4 = "/api/v1/user/logout"; 160 | String url4 = protocal + "://" + hostname + ":" + port + path4; 161 | String respData4 = HttpClient.sendDelete(url4, access_token); 162 | String retcode4 = JSONParser.getJsonValue(respData4, "code"); 163 | 164 | if (!"200".equalsIgnoreCase(retcode4)) { //校验接口4的返回code是否等于200 165 | System.out.println("4. 退出接口请求成功" + " return code = " + retcode4); 166 | return false; 167 | } 168 | System.out.println("4. 退出接口请求成功"); 169 | 170 | return true; 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /src/main/java/com/test/utils/JSONParser.java: -------------------------------------------------------------------------------- 1 | package com.test.utils; 2 | 3 | import org.json.JSONObject; 4 | 5 | 6 | public class JSONParser { 7 | /** 8 | * Parser the json内容 9 | * 10 | * @param JsonString String 11 | * @param JsonId String 12 | * @return JsonValue String 13 | */ 14 | public static String getJsonValue(String JsonString, String JsonId) { 15 | String JsonValue = ""; 16 | if (JsonString == null || JsonString.trim().length() < 1) { 17 | return null; 18 | } 19 | try { 20 | JSONObject obj1 = new JSONObject(JsonString); 21 | JsonValue = obj1.getString(JsonId); 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | return JsonValue; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/test/utils/Tools.java: -------------------------------------------------------------------------------- 1 | package com.test.utils; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | import java.util.Random; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | public class Tools { 10 | public static void main(String[] args) { 11 | System.out.println(Tools.genXCorID()); 12 | System.out.println(Tools.getDateString(new Long(System.currentTimeMillis()).toString())); 13 | 14 | } 15 | 16 | public static String genXCorID() { 17 | return generateHexFour() + generateHexFour() + "-" + generateHexFour() + "-" + generateHexFour() + "-" 18 | + generateHexFour() + "-" + generateHexFour() + generateHexFour() + generateHexFour(); 19 | } 20 | 21 | public static String generateHexFour() { 22 | String s = Double.toHexString(Math.floor((1 + Math.random()) * 0x10000)).replace("0x1.", "").replace("p16", ""); 23 | return s; 24 | } 25 | 26 | /** 27 | * Millisecond string to date 28 | * 29 | * @param millisecond: Millisecond string of the time 30 | * @return date: Date string of the time. 31 | */ 32 | public static String getDateString(String millisecond) { 33 | String milSecond = parseNumber(millisecond); 34 | return long2date(milSecond); 35 | } 36 | 37 | public static String parseNumber(String origStr) { 38 | Pattern pattern = Pattern.compile("[^0-9]"); 39 | Matcher matcher = pattern.matcher(origStr); 40 | String number = matcher.replaceAll(""); 41 | return number; 42 | } 43 | 44 | public static String long2date(String milisecond) { 45 | long timestamp = new Long(milisecond).longValue(); 46 | Date date = new Date(timestamp); 47 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 48 | return simpleDateFormat.format(date); 49 | } 50 | 51 | public static String getCurrentTime() { 52 | Date date = new Date(); 53 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); 54 | return simpleDateFormat.format(date); 55 | } 56 | 57 | public static String getRandomIp() { 58 | 59 | // ip范围 60 | int[][] range = {{607649792, 608174079}, // 36.56.0.0-36.63.255.255 61 | {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255 62 | {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255 63 | {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255 64 | {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255 65 | {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255 66 | {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255 67 | {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255 68 | {-770113536, -768606209}, // 210.25.0.0-210.47.255.255 69 | {-569376768, -564133889}, // 222.16.0.0-222.95.255.255 70 | }; 71 | 72 | Random rdint = new Random(); 73 | int index = rdint.nextInt(10); 74 | String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0])); 75 | return ip; 76 | } 77 | 78 | /* 79 | * 将十进制转换成IP地址 80 | */ 81 | public static String num2ip(int ip) { 82 | int[] b = new int[4]; 83 | String x = ""; 84 | b[0] = (int) ((ip >> 24) & 0xff); 85 | b[1] = (int) ((ip >> 16) & 0xff); 86 | b[2] = (int) ((ip >> 8) & 0xff); 87 | b[3] = (int) (ip & 0xff); 88 | x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]); 89 | 90 | return x; 91 | } 92 | 93 | /** 94 | * 生成随机身份证号 95 | * 96 | * @return cardSN 97 | */ 98 | public static String createIdcard() { 99 | String cardSN = ""; 100 | int checkSum = 0; 101 | String[] provinceCode = { 102 | "11", "12", "13", "14", "15", 103 | "21", "22", "23", 104 | "31", "32", "33", "34", "35", "36", "37", 105 | "41", "42", "43", "44", "45", "46", 106 | "50", "51", "52", "53", "54", 107 | "61", "62", "63", "64", "65", 108 | "81", "82" 109 | }; 110 | String[] cityCode = { 111 | "01", "02", "03", "04", "05", "06", "07", "08", "09", 112 | "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 113 | "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 114 | "30", "31", "32", "33", "34", "35", "36", "37", "38", "39" 115 | }; 116 | String[] villageCode = { 117 | "01", "02", "03", "04", "05", "06", "07", "08", "09", 118 | "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 119 | "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 120 | "80", "81", "82", "83", "84", "85", "86", "87", "88", "89" 121 | }; 122 | String[] birthYear = { 123 | "194", "195", "196", "197", "198" 124 | }; 125 | String[] birthMonth = { 126 | "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" 127 | }; 128 | String[] birthDay = { 129 | "01", "02", "03", "04", "05", "06", "07", "08", "09", 130 | "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 131 | "20", "21", "22", "23", "24", "25", "26", "27", "28" 132 | }; 133 | String[] oneCode = { 134 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" 135 | }; 136 | int[] fx = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; 137 | String[] checkCode = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; 138 | cardSN += getRandomFromArray(provinceCode);//create province 139 | cardSN += getRandomFromArray(cityCode);//create city 140 | cardSN += getRandomFromArray(villageCode);//create village 141 | cardSN += getRandomFromArray(birthYear); 142 | cardSN += getRandomFromArray(oneCode); 143 | cardSN += getRandomFromArray(birthMonth); 144 | cardSN += getRandomFromArray(birthDay); 145 | cardSN += getRandomFromArray(oneCode); 146 | cardSN += getRandomFromArray(oneCode); 147 | cardSN += getRandomFromArray(oneCode); 148 | for (int i = 0; i < 17; i++) { 149 | checkSum += Integer.parseInt(String.valueOf(cardSN.charAt(i))) * fx[i]; 150 | } 151 | cardSN += checkCode[checkSum % 11]; 152 | return cardSN; 153 | } 154 | 155 | /** 156 | * 从数组中得到随机字符串 157 | * 158 | * @param strArray String[] 159 | * @return String 160 | */ 161 | private static String getRandomFromArray(String[] strArray) { 162 | Random random = new Random(); 163 | return strArray[random.nextInt(strArray.length)]; 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | logs 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | --------------------------------------------------------------------------------