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);
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With