本文介绍了需要通过jmeter中的Web服务发送字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该将图像转换为数组字节,并将其与目标位置一起发送到 Webservice 中。
我的代码是用beanshell编写的

I am supposed to convert an image into array bytes and send it along with the destination location in a Webservice.I have this code written in beanshell

File file = new File("\\PICS\\k6.jpg"); 
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int i=0;
for (int i; (i = in.read(buffer)) != -1; ) 
{
bos.write(buffer, 0, i);
}
in.close();
 byte[] imageData = bos.toByteArray();
 bos.close();
 vars.put("imageData", new String(imageData));

我遇到此错误-
发现无效的XML字符(Unicode:0x0)

I am facing this error-An invalid XML character (Unicode: 0x0) was found in the element content of the document.

变量 imageData似乎是ASCII格式,但是如何与请求一起发送。我需要数组对象

The variable "imageData" seems to be in ASCII but how to send it along with the request.I need the array object

推荐答案

尝试在脚本顶部添加以下行:

Try adding the following line to the top of your script:

import org.apache.commons.lang.StringEscapeUtils;

并将最后一行更改如下:

and change the last line to look as follows:

vars.put("imageData", StringEscapeUtils.escapeXml(new String(imageData)));

UPD:端到端代码

import org.apache.commons.lang.StringEscapeUtils;

File file = new File("/home/glinius/test.html");
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];    
for (int i; (i = in.read(buffer)) != -1; ) {
    bos.write(buffer, 0, i);
}
in.close();
byte[] imageData = bos.toByteArray();
bos.close();
vars.put("imageData", StringEscapeUtils.escapeXml(new String(imageData)));

这篇关于需要通过jmeter中的Web服务发送字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 06:38