我正在尝试使用HTTP POST采样器上传文件,同时压缩文件内容。为此,我在Header Manager中添加了Content-Encoding:gzip,并在采样器的预处理器中添加了以下代码
import org.apache.commons.io.IOUtils;
import java.util.zip.GZIPOutputStream;
String bodyString = sampler.getArguments().getArgument(0).getValue();
byte [] requestBody = bodyString.getBytes();
ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(requestBody);
gzip.close();
sampler.getArguments().getArgument(0).setValue(out.toString(0));
这无法正常工作,并且引发了异常
2019-08-07 14:08:21,970 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor
javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' : Typed variable declaration : at Line: 5 : in file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' : .getValue ( )
Target exception: java.lang.NullPointerException: Attempt to invoke method getValue on null value
in inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; S . . . '' at line number 5
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:87) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_73]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:225) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
现在要调试,在
之类的getValue之前添加log.info(“ sampler -------:” + sampler)log.info("sampler-------:"+sampler);
String bodyString = sampler.getArguments().getArgument(0).getValue();
但是在日志采样器中,返回的是空的
Query Data:
那么我应该如何访问HTTP请求中的文件内容,并同时gzip并发送?
答案 0 :(得分:0)
要使用gzip压缩文件及其内容,请使用以下代码并将其放在JSR223 Sampler中(应该在调用POST API之前),并且假设有效载荷数据已复制到文件中。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
String source_filepath = "C:/Sample/Input.txt";
String destinaton_zip_filepath = "C:/Sample/Input.gzip";
byte[] buffer = new byte[1024];
try {
FileOutputStream fileOutputStream =new FileOutputStream(destinaton_zip_filepath);
GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);
FileInputStream fileInput = new FileInputStream(source_filepath);
int bytes_read;
while ((bytes_read = fileInput.read(buffer)) > 0) {
gzipOuputStream.write(buffer, 0, bytes_read);
}
fileInput.close();
gzipOuputStream.finish();
gzipOuputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
vars.put("ZipFileLocation",destinaton_zip_filepath);
现在可以通过${ZipFileLocation}
变量使用gzip文件。
由如何调用API决定,以下是解决方案:
注意:在适用的情况下,需要使用正确的MIME类型和标头
答案 1 :(得分:0)
如果您正在执行file upload with JMeter,要通过“文件上传”部分传递文件-您将无法使用getArguments()函数来获取任何内容,您需要使用getHTTPFiles()建议使用以下算法:
获取所需的文件路径,例如:
def plainFile = sampler.getHTTPFiles()[0].path
用压缩的文件路径替换原始文件路径
sampler.getHTTPFiles()[0].setPath("/path/to/compressed/file.gz")