【问题标题】:How can I send file using parameter instead set it in FilePath in JMeter HTTP Request Sampler?如何使用参数发送文件,而不是在 JMeter HTTP Request Sampler 的 FilePath 中设置它?
【发布时间】:2016-04-11 21:53:40
【问题描述】:

当我使用 bean shell 脚本将文件预加载到一个变量然后 在HTTP请求采样器的参数中设置它来上传文件,从wireshark数据包中我发现内容长度不正确。 内容类型也设置为导致问题的文本/纯文本。 怎么改?

下面是一些显示jemter设置和wireshrak snoops的图片。

The wireshark snoop when the content of the file set as the value of the parameter of the Http request sampler The jmeter test element setup showing that the the content of the file is set in the http request parameter

【问题讨论】:

    标签: jmeter http-post jmeter-plugins


    【解决方案1】:

    你使用了错误的配置:

    1. 勾选Use multipart/form-data for POST
    2. Parameters 区域中删除您的文件
    3. 将其添加到Send Files With the Request 区域,例如:

      • 文件路径:${__property(FileData,,)}
      • 参数名称:File
      • MIME 类型:text/plain

    有关在 JMeter 脚本中模拟文件上传和下载事件的更多信息,请参阅 Performance testing: Upload and Download Scenarios with Apache JMeter 指南。

    【讨论】:

    • 我需要将文件内容预加载到字符串的原因是我不想在每个上传请求中从磁盘加载文件,因为加载 2M 文件需要时间。所以我在我的设置线程组中的 bean shell 脚本中加载文件并尝试在发送参数中使用它,因为我认为“发送文件”部分只允许我提供文件路径。关于如何实现文件预加载的任何想法?
    • 我的 bean shell 脚本如下所示: import java.io.ByteArrayOutputStream;导入java.io.File;导入 java.io.IOException; String filePath = vars.get("uploadFilePath");打印(“文件路径:” + 文件路径); FileInputStream in = new FileInputStream(filePath); ByteArrayOutputStream bos = new ByteArrayOutputStream();字节[]缓冲区=新字节[1024]; for (int i; (i = in.read(buffer)) != -1; ) { bos.write(buffer, 0, i); } in.close(); byte[] fileData = bos.toByteArray(); print("文件长度:" + fileData.length); bos.close(); props.put("fileData", new String(fileData));
    • 点击此链接:stackoverflow.com/questions/23101904/…,是否可以使用 http 原始请求实现上传预加载文件?
    • 我没有问“你为什么需要这个”,我指出你的 HTTP 请求采样器配置错误
    • 也许我在上面的链接中误解了一些东西,它说“如果你向你的 HTTP 请求添加一个 Beanshell 预处理器,它执行文件上传,例如: FileInputStream in = new FileInputStream("/home/glinius /401.png"); 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", new String(imageData)); 你'将能够在您的 POST 请求中添加 ${imageData} 参数。
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2016-12-24
    相关资源
    最近更新 更多