gpt4 book ai didi

spring - 我如何使用spring mvc在html页面上显示blob图像标签

转载 作者:行者123 更新时间:2023-12-04 04:54:29 24 4
gpt4 key购买 nike

我创建了一个 Spring Web 应用程序,用于将图像存储为 blob 并以 <img> 显示标签。如何在页面上显示它?它没有在我的页面上使用 thymeleaf 模板引擎显示 image.img。

我试过这段代码:

<img th:attr="src=@{${Advertising.image}} , title=#{background}, alt=#{background}"
width="20" height="20"/>
@RequestMapping("/ViewAd.html")
public String viewAdvertise(ModelMap map) throws IOException {
map.addAttribute("Advertising", advertisingDAO.findById(2));
return "/admin/EditAdvertisement";
}

最佳答案

作为 Nizet 答案的扩展,这里有一些代码可以举例说明:

@EnableAutoConfiguration
@ComponentScan
@Controller
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}

@RequestMapping(value = "/home")
public ModelAndView home() throws IOException {
ModelAndView view = new ModelAndView("index");
view.addObject("image_id", 1);
return view;
}

@RequestMapping(value = "/image/{image_id}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable("image_id") Long imageId) throws IOException {

byte[] imageContent = //get image from DAO based on id
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(imageContent, headers, HttpStatus.OK);
}
}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
You can find <b>your inlined image</b> just below this text.
</p>
<p>
<img th:src="@{/image/${image_id}}" />
</p>
<p>
Regards, <br />
  <em>The Thymeleaf Team</em>
</p>
</body>
</html>

关于spring - 我如何使用spring mvc在html页面上显示blob图像标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com