如何在JSP中显示数据库中的图像?

时间:2015-08-19 18:19:43

标签: java spring jsp spring-mvc spring-data-jpa

我在从数据库显示图像时遇到一些问题。

Article.class

@Entity
@Table(name = "cmr_article", indexes = { @Index(columnList = "name",    unique = true) })
public class Article {

public static final int NAME_MAX = 50;
public static final int DESCRIPTION_MAX = 100;
public static final int FULL_MAX = 500;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(nullable = false, length = NAME_MAX)
private String name;

@Column(nullable = false, length = DESCRIPTION_MAX)
private String description;

@Lob
@Column(nullable = false, length = FULL_MAX)
private String full;

@Column(nullable = false)
private String date;

@Column(nullable = false)
private byte[] data;

@ManyToOne
private User user;

文章保存:

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void articleSave2(long userId, ArticleForm articleForm) {
    User user = userRepository.findOne(userId); 
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    Date date = new Date();
    Article article = new Article();
    article.setName(articleForm.getName());
    article.setDescription(articleForm.getDescription());
    article.setDate(dateFormat.format(date));
    article.setFull(articleForm.getFull());
    article.setData(articleForm.getData()); 
    article.setUser(user);
    articleRepository.save(article);
}

jps文件:

<img src='<c:out value="${article.data}"></c:out>' />

问题是JSP文件中的图像没有显示,它显示代码:[B@6acdfc86。在MYSQL中,我看到我保存图像。如何更改代码以在我的jsp中查看图像?

1 个答案:

答案 0 :(得分:0)

使用原始字节数组和JSP EL无法实现您的目标。您需要将图像嵌入base 64 data as described here,或者您需要将图像保存到某个位置的磁盘,并提供一个URL以在src属性中访问它。