从资源文件夹读取图像

huangapple go评论74阅读模式
英文:

Read image from resources folder

问题

我想从资源文件夹读取图像,并将其作为响应主体发送。我尝试了这个:

@RequestMapping(value = "/image/{imageid}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

    File file = new File(String.valueOf(getClass().getResource("/resources/color.jpg")));
    byte[] fileContent = Files.readAllBytes(file.toPath());

    InputStream in = new ByteArrayInputStream(fileContent);
    return IOUtils.toByteArray(in);
}

但是我得到了:抛出异常 java.nio.file.NoSuchFileException: null at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)

color.jpg 文件位于那里,但由于某种原因找不到。你知道我如何解决这个问题吗?

目录:C:\Users........war_file\src\main\resources

10/04/2020  20:58    <DIR>          .
10/04/2020  20:58    <DIR>          ..
10/04/2020  20:05               816 application-dev.yml
10/04/2020  19:26               816 application-local.yml
10/04/2020  20:05               813 application.yml
10/04/2020  20:58           187,405 color.jpg
11/03/2020  01:43               795 logback-spring.xml
               5 File(s)        190,645 bytes
               2 Dir(s)  48,421,285,888 bytes free

编辑:解决方案:

@RequestMapping(value = "/image/{imageid}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream("color.jpg");
    byte[] bytes = IOUtils.toByteArray(is);
    InputStream in = new ByteArrayInputStream(bytes);
    return IOUtils.toByteArray(in);
}
英文:

I want to read image from resources folder and send it as a response body. I tried this:

@RequestMapping(value = &quot;/image/{imageid}&quot;,method= RequestMethod.GET,produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

        File file = new File(String.valueOf(getClass().getResource(&quot;/resources/color.jpg&quot;)));
        byte[] fileContent = Files.readAllBytes(file.toPath());

        InputStream in = new ByteArrayInputStream(fileContent);
        return IOUtils.toByteArray(in);
    }

But I get: threw exception
java.nio.file.NoSuchFileException: null
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)

The file color.jpg is located there but for some reason is not found. Do you know how I can solve this issue?

Directory of C:\Users\........war_file\src\main\resources

10/04/2020  20:58    &lt;DIR&gt;          .
10/04/2020  20:58    &lt;DIR&gt;          ..
10/04/2020  20:05               816 application-dev.yml
10/04/2020  19:26               816 application-local.yml
10/04/2020  20:05               813 application.yml
10/04/2020  20:58           187,405 color.jpg
11/03/2020  01:43               795 logback-spring.xml
               5 File(s)        190,645 bytes
               2 Dir(s)  48,421,285,888 bytes free

EDIT: The solution:

@RequestMapping(value = &quot;/image/{imageid}&quot;,method= RequestMethod.GET,produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream(&quot;color.jpg&quot;);
        byte[] bytes = IOUtils.toByteArray(is);
        InputStream in = new ByteArrayInputStream(bytes);
        return IOUtils.toByteArray(in);
    }

答案1

得分: 1

这适用于Spring Boot 2.2.6.RELEASE。

@Controller
public class Test {

    @Autowired
    private ResourceLoader resourceLoader;

    @RequestMapping(value = "/image/{imageid}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody
    byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

        Resource resource = resourceLoader.getResource("classpath:color.jpg");
        File file = resource.getFile();
        byte[] fileContent = Files.readAllBytes(file.toPath());

        InputStream in = new ByteArrayInputStream(fileContent);
        return IOUtils.toByteArray(in);
    }
}
英文:

This works on Spring Boot 2.2.6.RELEASE.

@Controller
public class Test {

    @Autowired
    private ResourceLoader resourceLoader;

    @RequestMapping(value = &quot;/image/{imageid}&quot;, method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody
    byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {

        Resource resource = resourceLoader.getResource(&quot;classpath:color.jpg&quot;);
        File file = resource.getFile();
        byte[] fileContent = Files.readAllBytes(file.toPath());

        InputStream in = new ByteArrayInputStream(fileContent);
        return IOUtils.toByteArray(in);
    }
}

答案2

得分: 0

getClass().getResource("/resources/color.jpg"))

尝试使用&quot;color.jpg&quot;而不是&quot;/resources&quot; (`getClass().getResource("color.jpg"))`)。因为您已经获取了资源。还要确保您已经设置了资源文件夹。
英文:
getClass().getResource(&quot;/resources/color.jpg&quot;))

Try only "color.jpg" without "/resources" (getClass().getResource(&quot;color.jpg&quot;))). Because you already get resource. Also be sure you setted your resources folder.

答案3

得分: -1

假设这是一个Spring Boot项目,您可以按照以下方式访问资源文件:

File file = new File(getClass().getResource("color.jpg").getFile()); //移除 "resources"
英文:

Assuming this is a Spring boot project, you can access any files from resources as below

File file = new File(getClass().getResource(&quot;color.jpg&quot;).getFile()); //Remove &quot;resources&quot; 

huangapple
  • 本文由 发表于 2020年4月11日 02:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61146092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定