Need to send Byte Array through Webservice in jmeter

你说的曾经没有我的故事 提交于 2019-12-25 02:26:57

问题


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));

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

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


回答1:


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: ent-to-end code

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)));


来源:https://stackoverflow.com/questions/24142587/need-to-send-byte-array-through-webservice-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!