我正在尝试通过struts2 Java框架从数据库博客数据类型中检索JSP中的图像,但显示空白
我的JSP页面
<tbody>
<% int n=1; %>
<s:iterator value="dataList">
<tr>
<th><% out.print(n); %></th>
<th>
<s:property value="postTitle" />
</th>
<th>
<s:property value="postDescription"/>
</th>
<th>
<img width="100" height="auto" src="<s:url value="Post?url=" /><s:property value="postUrl" />" />
</th>
</tr>
<% n=n+1; %>
</s:iterator>
</tbody>
和Pujo Page
String postTitle;
String postUrl;
String postDescription;
File postImage;
// Getter Setter
Dao文件为:
public ResultSet getPost() {
try {
Connection conn = Connectionmanager.getconnection();
return conn.prepareStatement("SELECT posttitle,posturl,postbody,postmetadescription,postauthor,categery FROM blogpost ORDER BY id DESC").executeQuery();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public ResultSet getPostimage(String url) {
try {
Connection conn = Connectionmanager.getconnection();
PreparedStatement ps = conn.prepareStatement("SELECT postimage FROM blogpost WHERE posturl=?");
ps.setString(1,url);
return ps.executeQuery();
} catch (ClassnotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
动作文件代码
public String execute() throws Exception {
HttpServletResponse response = ServletactionContext.getResponse();
HttpServletRequest request = ServletactionContext.getRequest();
String url = request.getParameter("url");
try {
dataList = new ArrayList<>();
rs = new PostDao().getPost();
if (rs != null) {
while (rs.next()) {
dataBean = new PostInfo();
dataBean.setPostTitle(rs.getString("posttitle"));
dataBean.setPostUrl(rs.getString("posturl"));
dataBean.setPostBody(rs.getString("postbody"));
dataBean.setPostDescription(rs.getString("postmetadescription"));
dataBean.setPostAuthor(rs.getString("postauthor"));
dataBean.setPostCategery(rs.getString("categery"));
dataList.add(dataBean);
}
}
try {
rs = new PostDao().getPostimage(url);
if (rs.next()) {
Blob ph = rs.getBlob("postimage");
byte[] data = ph.getBytes(1,(int) ph.length());
response.setHeader("expires","0");
response.setContentType("image/jpg");
OutputStream out = response.getOutputStream();
out.write(data);
out.flush();
out.close();
}
} catch (IOException | SQLException e) {
e.printStackTrace();
e.getMessage();
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}