有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从JMeter采样器读取二进制数据

尝试从JMeter采样器读取二进制数据响应,并在Beanshll后处理器中对其进行反序列化

流程是,控制器首先向Web服务发送请求,Web服务返回二进制数据。 当我在JMeter中查看时,我看到一些垃圾字符

我需要将其转换为BeanShell脚本中的实际对象(我的自定义对象)

这就是我试过的。但是没有运气。也许我在尝试一些愚蠢的事情。 附言:有IO功能的新手

 import java.io.ObjectOutputStream;
 import com.package.MyClass;

 MyClass myObj = new MyClass();
 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));

 myObj = ois.readObject();

它不起作用。它没有显示任何东西。 有什么帮助吗


共 (1) 个答案

  1. # 1 楼答案

    你确定readObject方法对二进制数据有效吗?例如,如果不发生StreamCorruptedException,后处理器就会停止工作

    根据《如何使用BeanShell指南》

    As Beanshell is executed within Rhino engine there is no other option to see what’s wrong apart from inspecting jmeter.log file for something like: Error invoking bsh method and using System.out.println(“something”) or log.info(“something”) to get to the bottom of where the script fails.

    如果无法验证后处理器是否成功,则必须查看日志和/或在末尾添加一些调试输出语句

    另一个有用的技巧是用try/catch包围代码

    有关如何使用,请参见下面的示例。在“一切正常”的情况下,后处理器将Successfully read data into myObj打印到标准输出中。在相反的情况下,你会看到一个相关的错误

        import java.io.ByteArrayInputStream;
        import java.io.ObjectInputStream;
        try {
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
            Object myObj = ois.readObject();
            ois.close();
            System.out.println("Successfully read data into myObj"); 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    

    希望这有帮助