The method toByteArray(InputStream) is undefined for the type IOUtils

I am working on spring boot.I have a method to return a file using byte array.while i am trying to return byteArray I got this error.my code is given below-

@GetMapping(
      value = "/get-file",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
    )
    public @ResponseBody byte[] getFile() throws IOException {

        InputStream in = getClass()
          .getResourceAsStream("/com/baeldung/produceimage/data.txt");
        return IOUtils.toByteArray(in);
    }

Answer

Very likely, you have imported the wrong IOUtils from tomcat (import org.apache.tomcat.util.http.fileupload.IOUtils;)

Add Apache Commons IO dependency

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

and use the following import

import org.apache.commons.io.IOUtils;

Leave a Reply

Your email address will not be published. Required fields are marked *