簡體   English   中英

對於IOUtils類型,未定義方法toByteArray(InputStream)

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

我正在春季啟動。我有一種使用字節數組返回文件的方法。而我試圖返回字節數組時出現此錯誤。我的代碼如下-

@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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM