英文:
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 = "/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);
}
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 <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
EDIT: The solution:
@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);
}
答案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 = "/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);
}
}
答案2
得分: 0
getClass().getResource("/resources/color.jpg"))
尝试使用"color.jpg"而不是"/resources" (`getClass().getResource("color.jpg"))`)。因为您已经获取了资源。还要确保您已经设置了资源文件夹。
英文:
getClass().getResource("/resources/color.jpg"))
Try only "color.jpg" without "/resources" (getClass().getResource("color.jpg"))
). 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("color.jpg").getFile()); //Remove "resources"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论