[英]How do I Return SUM from JPA Query Using Hibernate and Spring-boot?
[英]How do I return an image using Spring boot
我是 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.
}
}
我找到了一些對我有幫助的代碼。
@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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.