[英]How to send a gzip file as byte array in HTTP request in JMeter
I have a post request where I have to send a gzip file as byte array.我有一个 post 请求,我必须将 gzip 文件作为字节数组发送。 I have tried the following:
我尝试了以下方法:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
FileInputStream in = new FileInputStream("C:\Users\New\Desktop\Load_Testing");
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[] postData = bos.toByteArray();
bos.close();
vars.put("postData", new String(postData));
But the file is not sent.但是文件没有发送。
But for all the cases I am getting the error :但对于所有情况,我都收到错误消息:
:{"Result":"Error uploading data stream
The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
at System.IO.Compression.Inflater.Decode()
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Compression.GZipStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.Stream.ReadByte()
at ServiceData.CompressionHelper.UnGZip(Byte[] input)
First of all, I don't like your file path C:\\Users\\New\\Desktop\\Load_Testing
.首先,我不喜欢你的文件路径
C:\\Users\\New\\Desktop\\Load_Testing
。 In Java and Beanshell you need to escape back slashes like C:\\\\Users\\\\New\\\\Desktop\\\\Load_Testing
在 Java 和 Beanshell 中,您需要转义反斜杠,如
C:\\\\Users\\\\New\\\\Desktop\\\\Load_Testing
Second: make sure that you're sending a correct gzip
file.第二:确保您发送的是正确的
gzip
文件。 If Your C:\\Users\\New\\Desktop\\Load_Testing
file is not in gzip format, you'll need to convert compress it in your Beanshell Pre-Processor as follows:如果你的
C:\\Users\\New\\Desktop\\Load_Testing
文件不是 gzip 格式,你需要在你的 Beanshell 预处理器中转换压缩它,如下所示:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPOutputStream;
byte[] buffer = new byte[1024];
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream("C:/Users/New/Desktop/Load_Testing.gz"));
FileInputStream in = new FileInputStream("C:/Users/New/Desktop/Load_Testing");
int len;
while ((len = in.read(buffer)) > 0) {
gzos.write(buffer, 0, len);
}
in.close();
gzos.finish();
gzos.close();
And finally, make sure that you're sending correct Accept-Encoding header.最后,确保您发送的是正确的 Accept-Encoding 标头。 To do so add a HTTP Header Manager and include
gzip
value in Accept-Encoding
stanza.为此,请添加HTTP 标头管理器并在
Accept-Encoding
节中包含gzip
值。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.