在我的应用程序中,我有一个控制器,当表单传送图片时,它将图片存储在所需的目录中。图片的名称是userId。我正在使用Spring-boot和Thyemeleaf。 使用userId将图像存储在目录中的功能正常。 现在我有一个获取此图像并将其作为字节返回的函数,以便我从Thymeleaf访问它。
@RequestMapping(value = "/displayPicture/{userId}")
@ResponseBody
public byte[] getImage(@PathVariable(value = "userId") String userId) throws IOException {
File serverFile = new File("path");
return Files.readAllBytes(serverFile.toPath());
}
我如何做到这一点?
我之前从不同的控制器返回userId,如下所示:
modelAndView.addObject("userId", user.getUserId());
html表格:
<form method="POST" th:action="@{/form}"
enctype="multipart/form-data" role="form">
<input id="fileUpload" type="file" name="file" onchange="this.form.submit()"/>
<img th:href="/displayPicture/${userId}"/> // This is what I am asking how to do correctly
</form>
我希望在上传图片时以及返回modelAndView时,它会返回并显示上传的图片。 提前谢谢。
答案 0 :(得分:0)
上下文相关网址?
<img th:href="@{/displayPicture/{userId}(userId=${userId})}"/>