我正在使用 spring boot。我有一个使用字节数组返回文件的方法。当我试图返回 byteArray 时,我得到了这个错误。我的代码在下面给出-
@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);
}
最佳答案
很有可能,您从 tomcat 导入了错误的 IOUtils (import org.apache.tomcat.util.http.fileupload.IOUtils;
)
添加 Apache Commons IO 依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
并使用下面的导入
import org.apache.commons.io.IOUtils;
关于java - IOUtils 类型未定义方法 toByteArray(InputStream),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549197/