且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Struts 2从MySQL在JSP页面中查看图像

更新时间:2023-12-02 13:16:28

重点是将url写入<img>标记.网址应映射将数据流式传输到响应的操作.

The point is to write url to the <img> tag. The url should map an action that streaming data to the response.

<img src="<s:url action='visualizzaimg'/>">  

该动作需要返回一个stream结果,为此,它应该为InputStream提供一个吸气剂.输入流的默认输入名称为inputStream,因此我们将使用它.

The action needs to return a stream result, for this purpose it should provide a getter for InputStream. The default input name for the input stream is inputStream, so we will use it.

private InputStream inputStream;

public ImputStream getInputStream(){
  return inputStream;
}

public String visualizzaimg() throws SQLException, IOException {
    Connessione();    // DB connection method
    PreparedStatement pstmt = con.prepareStatement("SELECT Immagine FROM Utenti WHERE Username = ?");
    pstmt.setString(1,username);
    ResultSet rs = pstmt.executeQuery();
    if(rs.next()){
      inputStream = rs.getBinaryStream(1);
    }
    return "success";
}

现在要配置结果

<result name="success" type="stream">
   <param name="contentType">image/jpeg</param>
</result>