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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of The method toByteArray(InputStream) is undefined for the type IOUtils without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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;
We are here to answer your question about The method toByteArray(InputStream) is undefined for the type IOUtils - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji