目录

1、生成随机字符串

2、生成随机中文字符

3、下载文件并存储到本地

4、生成身份证号码

5、上传脚本

 

 

import java.util.Random; 
String random_len(int string_length) {
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz123456789"; 
    randomstring =""; 
    for (int i=0; i<string_length; i++) { 
        Random randomGenerator = new Random(); 
        int randomInt = randomGenerator.nextInt(chars.length()); 
        randomstring += chars.substring(randomInt,randomInt+1); 
    } 
    return randomstring;
}
log.info("8位长度:"+random_len(8));
log.info("16位长度:"+random_len(16));
vars.put("random_8", random_len(8));
vars.put("random_16", random_len(16));

  

2、生成随机中文字符

import java.util.Random;
public class Random_str {
    public static String RandomJianHan(int len) {
        String ret = "";
        for (int i = 0; i < len; i++) {
            String str = null;
            int hightPos, lowPos; // 定义高低位
            Random random = new Random();
            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                str = new String(b, "GBK"); // 转成中文
            } catch(UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret += str;
        }
 
        return ret;
    }
}
 
Random_str ran = new Random_str();
String content = ran.RandomJianHan(30); //此处生成的是长度为4的字符串
vars.put("Content",content);
 
String content1 = "接口自动化发布帖子,这是随机内容:"+ran.RandomJianHan(4);//此处生成的是长度为4的字符串
vars.put("content_post",content1);
String content2 = "接口自动化评论帖子,这是随机评论内容:"+ran.RandomJianHan(4);
vars.put("content_comment",content2);
String content3 = "接口自动化回复评论,这是随机回复内容:"+ran.RandomJianHan(4);
vars.put("content_reply",content3);

  

3、下载文件并存储到本地

import java.io.*;
 
//获取上个请求的返回数据
byte[] result = prev.getResponseData();
//要下载到什么地方
String file_name = "/Users/zhenning/Desktop/test/${counter}.zip";
File file = new File(file_name);
FileOutputStream out = new FileOutputStream(file);
out.write(result);
out.close();

  

4、生成身份证号码

import java.util.*;
        StringBuilder generater = new StringBuilder();
        
        Map areaCode = new HashMap();
 
  
        areaCode.put("北京市", 110000);
        areaCode.put("市辖区", 110100);
        areaCode.put("东城区", 110101);
        areaCode.put("西城区", 110102);
        areaCode.put("崇文区", 110103);
        areaCode.put("宣武区", 110104);
        areaCode.put("朝阳区", 110105);
        areaCode.put("丰台区", 110106);
        areaCode.put("石景山区", 110107);
        areaCode.put("清河门区", 210905);
        areaCode.put("细河区", 210911);
        areaCode.put("彰武县", 210922);
        areaCode.put("辽阳市", 211000);
        areaCode.put("市辖区", 211001);
        areaCode.put("白塔区", 211002);
        areaCode.put("文圣区", 211003);
        areaCode.put("宏伟区", 211004);
        areaCode.put("新北区", 320411);
        areaCode.put("武进区", 320412);
        areaCode.put("溧阳市", 320481);
        areaCode.put("金坛市", 320482);
        areaCode.put("苏州市", 320500);
        areaCode.put("市辖区", 320501);
        areaCode.put("鹤壁市", 410600);
        areaCode.put("市辖区", 410601);
        areaCode.put("鹤山区", 410602);
        areaCode.put("山城区", 410603);
        areaCode.put("淇滨区", 410611);
        areaCode.put("浚 县", 410621);
        areaCode.put("淇 县", 410622);
        areaCode.put("新乡市", 410700);
        areaCode.put("青白江区", 510113);
        areaCode.put("新都区", 510114);
        areaCode.put("温江区", 510115);
        areaCode.put("金堂县", 510121);
        areaCode.put("双流县", 510122);
        areaCode.put("郫 县", 510124);
        areaCode.put("大邑县", 510129);
        areaCode.put("蒲江县", 510131);
        areaCode.put("新津县", 510132);
        areaCode.put("乾 县", 610424);
        areaCode.put("礼泉县", 610425);
        areaCode.put("永寿县", 610426);
        areaCode.put("彬 县", 610427);
        areaCode.put("长武县", 610428);
        areaCode.put("旬邑县", 610429);
        areaCode.put("淳化县", 610430);
        areaCode.put("武功县", 610431);
        areaCode.put("兴平市", 610481);
        areaCode.put("渭南市", 610500);
        
        //地区号
        String  randomAreaCode="";
        int index = (int) (Math.random() * areaCode.size());
         Collection values = areaCode.values();
         Iterator it = values.iterator();
        int i = 0;
        int code = 0;
        while (i < index && it.hasNext()) {
            i++;
            randomAreaCode = it.next().toString();
        }
        generater.append(randomAreaCode);
        //生日
        String randomBirthday="";
        Calendar birthday = Calendar.getInstance();
        birthday.set(Calendar.YEAR, (int) (Math.random() * 60) + 1950);
        birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
        birthday.set(Calendar.DATE, (int) (Math.random() * 31));
        StringBuilder builder = new StringBuilder();
        builder.append(birthday.get(Calendar.YEAR));
        long month = birthday.get(Calendar.MONTH) + 1;
        if (month < 10) {
            builder.append("0");
        }
        builder.append(month);
        long date = birthday.get(Calendar.DATE);
        if (date < 10) {
            builder.append("0");
        }
        builder.append(date);
        randomBirthday= builder.toString();
         generater.append(randomBirthday);
         
      //随机码
      String randomCode="";
      int code = (int) (Math.random() * 1000);
        if (code < 10) {
            randomCode= "00" + code;
        } else if (code < 100) {
            randomCode= "0" + code;
        } else {
            randomCode= "" + code;
        }
     
        generater.append(randomCode);
     //验证码
        char[]  chars= generater.toString().toCharArray();
        int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        int[] n = new int[17];
        int result = 0;
        for (int i = 0; i < n.length; i++) {
            n[i] = Integer.parseInt(chars[i] + "");
        }
        for (int i = 0; i < n.length; i++) {
            result += c[i] * n[i]
        }
        char validateCode = r[result % 11];
        generater.append(validateCode);
 
    
        vars.put("idNumber",generater.toString()) ;
        SampleResult.setResponseData(generater.toString());

  

  

5、上传脚本

import java.io.*;
byte[] result = prev.getResponseData();  //这个是获取到请求返回的数据,prev是获取上个请求的返回
String file_name = "xxx.zip"; //代表存放文件的位置和文件名
File file = new File(file_name);
FileOutputStream out = new FileOutputStream(file);
out.write(result);
out.close(); 

  

相关文章: