Need to send Byte Array through Webservice in jmet

2019-08-31 03:26发布

问题:

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