Home > Enterprise >  Issues Faced while saving one pdf file content into another pdf file using JMeter
Issues Faced while saving one pdf file content into another pdf file using JMeter

Time:07-20

I am using Beanshell sampler to save one pdf file content into another pdf file.

In Beanshell sampler I have put this following code:

FileInputStream in = new FileInputStream("C:\\Users\\Dey\\Downloads\\sample.pdf");
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[] extractdata = bos.toByteArray();
bos.close();
vars.put("extractdata", new String(extarctdata));

using beanshell post processor I saved this variable ${extractdata} in another pdf file . file is generated but when open the file it's empty means there is no content showing.

So , can someone please tell me how to resolve this issue ?? is there anything wrong in above code snippet ?? please guide me.

CodePudding user response:

  1. You made a typo

    byte[] extractdata = bos.toByteArray();
    

    and

    vars.put("extractdata", new String(extarctdata));
    

    so your test element is silently failing, check jmeter.log file it should contain some errors.

  2. It's not possible to state what else is wrong because we don't see your Beanshell Post-Processor code, most probably there is an issue with encoding when converting the byte array to string and vice versa.

    So I would suggest skipping this step and using vars.putObject() function instead like:

    vars.put("extractdata", extractdata);
    

    and then

    byte [] extractdata = vars.getObject("extractdata");
    
  3. If you just need to copy the file you can use the following snippet:

    import java.nio.file.Files
    import java.nio.file.Path
    import java.nio.file.Paths
    
    Path source = Paths.get("C:\\Users\\Dey\\Downloads\\sample.pdf");
    Path target = Paths.get("/location/for/the/new/file.pdf")
    
    Files.copy(source, target);
    
  4. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy for scripting so you should switch to Groovy, in this case you will be able to do something like:

    new File('/location/for/the/new/file.pdf').bytes = new File('C:\\Users\\Dey\\Downloads\\sample.pdf').bytes
    

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

  • Related