Search code examples
file-iojmeterbeanshell

java.io.FileNotFoundException with Jmeter - Using beanshell pre processor


I'm having issue with Jmeter using BeanShell PreProcessor to encode input file then include the encoded file in "Send file with request".

Jmeter Setup


  • HTTP Request (Send file with the request - ${file1})
    • BeanShell PreProcessor

BeanShell PreProcessor

import org.apache.commons.io.FileUtils;
import org.apache.commons.codec.binary.Base64;
String file1 = FileUtils.readFileToString(new File("D:/File/test.txt"),"UTF-8");
vars.put("file1",new String(Base64.encodeBase64(file.getBytes("UTF-8"))));

Error Message

java.io.FileNotFoundException: ${file1} (The system cannot find the file specified)

Solution

  • If you're trying to do the following:

    1. Read file into a string
    2. Encode content to Base64
    3. Save encoded content to another file
    4. Save new file path into a JMeter Variable

    your Beanshell code should look like:

    String file1 = FileUtils.readFileToString(new File("D:/File/test.txt"), "UTF-8");
    FileUtils.write(new File("D:/File/testbase64.txt"),new String(Base64.encodeBase64(file1.getBytes("UTF-8"))));
    vars.put("file1","D:/File/testbase64.txt");
    

    Your code snippet was

    1. Trying to put encoded file content into file JMeter variable
    2. Having a typo in the last line file.getBytes() should be changed to file1.getBytes()

    See How to use BeanShell: JMeter's favorite built-in component guide for more information on bsh scripting in Apache JMeter.