0

这里使用 MIME 类型作为 Multipart/form-data 和参数作为 spb 的 POST 请求我正在传递 1 个具有完整 JSON 正文的文本文件。

text file : upload.txt
{
  "id":"Rx2160C-019A",
  "iAgree":false,
  "price":"31.25",
  "maxDispenceDays":null,
  "RxId":"DRAFT9800E",
 ....... 
}

每次我需要根据最后一个 HTTP 请求更新文本文件中的idand时。RxId

我正在尝试使用BeanShell PostProcessor元素,但我没有得到适当的代码和解决方案。

f = new FileOutputStream("/Users/bhkuma/Documents/Bharath/Jmeter/Loa‌​dTesting/goRxDigitiz‌​e.txt", true); 
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(name + "," + email); 
f.close();

我怎样才能做到这一点?

这是我正在尝试的更新代码..

import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;

import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;


//ObjectMapper mapper = new ObjectMapper();

//JSONObject root = mapper.readValue(new File("/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt"), JSONObject.class);
FileReader fr = new FileReader("/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt");
        BufferedReader br = new BufferedReader(fr);

            String sCurrentLine;

            String output="";
            while ((sCurrentLine = br.readLine()) != null) {
                output += sCurrentLine;
                //System.out.println(sCurrentLine);
            }
log.info("bharath");
log.info(output);

JSONObject obj = new JSONObject(output);

JSONObject value = new JSONObject();
value.put("id","Rx2160C-100A");
log.info(value);
obj.put(value);
log.info(output);

System.out.println("Successfully updated json object to file...!!");

而这里的 JSON 对象将无法工作......

4

2 回答 2

0

这是我想出的最终代码..它工作正常

import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import org.json.JSONObject;


import com.fasterxml.jackson.databind.ObjectMapper;

String path = "/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/goRxDigitize1.txt";


FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);

            String sCurrentLine;

            String output="";
            while ((sCurrentLine = br.readLine()) != null) {
                output += sCurrentLine;
                //System.out.println(sCurrentLine);
            }

            log.info("bharath");
log.info(output);

JSONObject obj = new JSONObject(output);
obj.put("id",vars.get("orderId"));
obj.put("RxId", vars.get("RxId"));
//log.info(obj);
//obj.id="test1";

File file = new File(path);

            // If file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            // Write in file
            bw.write(obj.toString());

            // Close connection
            bw.close();

//obj.id = "test";
/*JSONObject value = new JSONObject();

log.info(value);
obj.put(value);
log.info(output);*/

//file.write(obj.toString());
System.out.println("Successfully updated json object to file...!!");
于 2017-09-05T15:02:15.130 回答
0

我听说Groovy 是新的黑色,所以我建议从 Beanshell 切换到 JSR223 PostProcessor。要替换的相关 Groovy 代码id类似于:

def upload = new File('/Users/bhkuma/Documents/Bharath/Jmeter/LoadTesting/upload.txt')
def json = new  groovy.json.JsonSlurper().parse(upload)
def builder = new groovy.json.JsonBuilder(json)

builder.content.id = 'Rx2160C-100A'

def writer = upload.newWriter()
writer << builder.toPrettyString()
writer.close()

参考:

于 2017-09-05T14:03:38.267 回答