Java Can';t从postgres查看图像
我有一个实体类-产品Java Can';t从postgres查看图像,java,spring,image,Java,Spring,Image,我有一个实体类-产品 @Data @Entity @Table(name = "products") public class Products { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") @Size(min = 3, max = 40) priva
@Data
@Entity
@Table(name = "products")
public class Products {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
@Size(min = 3, max = 40)
private String name;
@Column(name = "price")
@Min(1)
@Max(10000)
private int price;
@Column(name = "serial")
@Min(1)
@Max(1000000)
private int serial;
@Column(name="picture")
private byte[] picture; //other code
我正在保存图像的路径:
@Bean
CommandLineRunner runner(ProductService productService, UserService userService) throws IOException {
byte[] array = Files.readAllBytes(Paths.get("src/main/resources/static/img/mask_black.png"));
return args -> {
productService.saveProduct(new Products(11L, "black mask", 100, 11111, array));
如表所示,图像为650x458,JPEG图像为25,45KB
这是我的html模板:
<tr th:each="products : ${products}">
<td th:text="${products.name}"></td>
<td th:text="${products.price}"></td>
<img src="${products.picture}" width="100" height="100"/> //some other code
//其他代码
但是当我打开一页时,我看不到我的图像,只有空的正方形。我做错了什么?
p、 我也有repository、service和controller类,但它和任何crud项目一样是标准的。试试看
<img src="data:image/jpg;base64, ${products.picture}">
HTML img src属性应仅包含URL(文本形式),而不是字节数组。它可以是指向单独资源的链接,也可以是下面发布的@ShaileshJain的嵌入式数据URL。如果使用数据URL,则需要对数据(图片数组)进行base64编码。