struts2 action 返回图片流

时间:2022-01-24 12:51:58

数据库为mssql zp字段为image类型

java代码

 OutputStream out = null;
try
{
String contenttype = "image/jpeg";
ActionContext context = ActionContext.getContext();
// HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE); response.setContentType(contenttype);
out = response.getOutputStream();
Statement stmt=con.createStatement();
String sql="select top 1 zp from table_1 ";
ResultSet rs=stmt.executeQuery(sql);
//byte []by =rs.getBytes(0);
while(rs.next()){
out.write(rs.getBytes("ZP"));
}
}
catch(SQLException ee){ }
finally {
out.flush();
out.close();
}

struts.xml

<action name="bb" class="test.tt" method="getimage2">
</action>

index.jsp

 <img name="inputName"  id="inputName" src="bb.action"/>