[英]Java Spring Boot: project saves images in the wrong path and not display images when opening with IDE Intellji
[英]Spring doesnt display images when there is a path variable on URI
我的 controller 中有一個方法可以指向帶有評論的頁面。 所有評論都與特定消息相關。 我使用 pathvariable 發送 messageID,我稍后使用它從我的數據庫中獲取評論。
@GetMapping("/showComments/{messageId}")
public String displayComments(@PathVariable Long messageId, @AuthenticationPrincipal User user, Model model) {
List<Comment> comments = commentService.showComments(messageId);
Message messageGotById = messageService.getMessageById(messageId);
model.addAttribute("comments", comments);
model.addAttribute("messageGot",messageGotById);
model.addAttribute("userId",user.getId());
return "listComments";
}
然后我有一個從數據庫中獲取圖像的方法
@GetMapping("/getImageComment/{id}")
public void showCommentImage(@PathVariable Long id, HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
Message messageImage = messageService.getMessageById(id);
ImageInfo imageInfo = messageImage.getUsr().getImageInfo();
byte[] imageData = imageInfo.getImageData();
String encode = Base64.getEncoder().encodeToString(imageData);
InputStream is = new ByteArrayInputStream(imageData);
IOUtils.copy(is, response.getOutputStream());
}
listComments.html
<img th:src="@{'getImageComment/' + ${userId}}" height="55"
width="55">
將我用於 go 的鏈接添加到 listComments.html
<a th:href="@{/showComments/{id}(id=${message.id})}">comment</a>
這里的問題是,當 displayComments 方法中的 URI 有一個變量時,圖像不會顯示。 但是沒有 pathvariable 一切正常。我不明白是什么問題。
遵循dsp_user的建議並使用服務器相關的 URL 。 改變了
<img th:src="@{'getImageComment/' + ${userId}}" height="55"
width="55">
到
<img class="rounded" th:src="@{'~/getImageComment/' + ${userId}}" height="55"
width="55">
它終於對我有用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.