Struts2+Hibernate reads the Blob with the image stored in the database and displays the image to the front page

First, the JSP page part

Obtaining pictures in the database needs to be implemented through actions, as follows

<img alt="头像" src="<s:url action='ShowUserProfilePicture'/>"/>

It can be seen that we can display a picture by returning a URL through an action.
If parameters are required, we set an s:param tag body after the action to achieve this:

<img id="authorImg" src="<s:url action='Action'><s:param name='...' value='...'></s:param></s:url>" />

Second, the Hibernate part

Here for the convenience of description, we create a User class

package com.aplusBBS.nor;

import java.sql.Blob;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="user_inf")
public class User
{
    @Id @Column(name="user_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

    @Column(name="register_time")
    private Timestamp registerTime;

    //此处通过一个byte[]来存储图片,数据库也应当有一个Blob类型的列
    @Column(name="profile_picture")
    @Lob @Basic(fetch=FetchType.LAZY)
    private byte[] picture;

    public User() {}

    //省略一堆getter、setter方法
}

3. Action part

Next we create a picture display action of the above JSP page: ShowUserProfilePicture

public class ShowUserProfilePicture extends ActionSupport
{
    private static final long serialVersionUID = 1L;

    public String execute()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();

        HttpSession session = request.getSession();

        User user = (User)session.getAttribute("user");

        //设置编码方式,用来解析图片(关键点一)
        response.setContentType("multipart/form-data");

        ServletOutputStream out = null;
        try 
        {
            //获取页面输出流(关键点二)
            out = response.getOutputStream();
            //获取User类中存储有图片二进制数据的数组(关键点三)
            byte[] img = user.getPicture();
            if(img == null)
            {
                return null;
            }
            else
                //将图片载入输出流(关键点四)
                out.write(user.getPicture());
            out.flush();
            out.close();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        //必须返回null(关键点五)
        return null;
    }
}

Four, struts.xml part

The configuration is as follows:

<action name="ShowAuthorImage" class="com.aplusBBS.action.bbs.ShowAuthorImage"></action>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325980333&siteId=291194637