我是Spring boot的新手。我正在设计一个使用Spring boot的系统,它是一个Restful API。我想要显示用户上传的图像。我将文件存储在资源中的子文件夹"fotos/x“中。我本能地试图通过输入以下命令来访问它
http://localhost:8080/fotos/76/miniaturas/casa-venda-2-dormitorios-franca-sao-paulo_1.jpg拿着Tomcat可以为我提供图像,但它没有。
所以我继续尝试构建一个端点,我遇到了一个代码片段
@GetMapping(value = "/image")
public @ResponseBody byte[] getImage() throws IOException {
InputStream in = null;
try{
in = getClass()
.getResourceAsStream("/fotos/76/miniaturas/casa-venda-2-dormitorios-franca-sao-paulo_1.jpg");
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return IOUtils.toByteArray(in); //toByteArray doesn't exist.
}
}发布于 2019-11-27 23:39:42
我找到了一些对我有帮助的代码。
@GetMapping(value = "/imagens/{dir}/{img}", produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody byte[] getImage(@PathVariable String dir, @PathVariable String img) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get("/var/www/back_imoveis/back_imoveis/imoveis/src/main/resources/public/fotos/" + dir + "/miniaturas/" + img));
return bytes;
}https://stackoverflow.com/questions/59070468
复制相似问题