使用struts在jsp中显示字节数组中的图像
我可以以字节[]的形式获取图像,并使用form.jsp和FormFile将其存储在数据库中。 现在,我需要能够检索该字节[],并将其作为图像显示在JSP中。有没有办法创建资源并检索该图像使用struts在jsp中显示字节数组中的图像,jsp,servlets,struts,Jsp,Servlets,Struts,我可以以字节[]的形式获取图像,并使用form.jsp和FormFile将其存储在数据库中。 现在,我需要能够检索该字节[],并将其作为图像显示在JSP中。有没有办法创建资源并检索该图像 public ActionForward pageLoad(ActionMapping a,ActionForm b,HttpServletRequest c,HttpServletResponse d){ b.setImageData(loadImageData()); return a.fi
public ActionForward pageLoad(ActionMapping a,ActionForm b,HttpServletRequest c,HttpServletResponse d){
b.setImageData(loadImageData());
return a.findForward("toPage");
}
public ActionForward imageLoad(ActionMapping a,ActionForm b,HttpServletRequest c,HttpServletResponse d){
byte[] tempByte = b.getImageData();
d.setContentType("image/jpeg");
ServletOutputStream stream = d.getOutputStream();
/*
Code to write tempByte into stream here.
*/
return null;
}
将tempByte
写入流
,刷新并关闭它<代码>从操作返回空值
现在从
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/Test";
ResultSet rs = null;
PreparedStatement psmnt = null;
InputStream sImage;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "Admin", "Admin");
psmnt = connection.prepareStatement("SELECT image FROM save_image WHERE id = ?");
psmnt.setString(1, "11"); // here integer number '11' is image id from the table
rs = psmnt.executeQuery();
if(rs.next()) {
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
}
}
catch(Exception ex){
out.println("error :"+ex);
}
finally {
rs.close();
psmnt.close();
connection.close();
}
%>
连接=空;
字符串connectionURL=“jdbc:mysql://localhost:3306/Test";
结果集rs=null;
PreparedStatement psmnt=null;
输入流模拟;
试一试{
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connection=DriverManager.getConnection(connectionURL,“Admin”,“Admin”);
psmnt=connection.prepareStatement(“从保存图像中选择图像,其中id=?”;
setString(1,“11”);//这里整数“11”是表中的图像id
rs=psmnt.executeQuery();
如果(rs.next()){
字节[]字节数组=新字节[1048576];
int size=0;
sImage=rs.getBinaryStream(1);
response.reset();
response.setContentType(“图像/jpeg”);
而((size=sImage.read(bytearray))!=-1){
response.getOutputStream().write(bytearray,0,size);
}
}
}
捕获(例外情况除外){
out.println(“错误:+ex”);
}
最后{
rs.close();
psmnt.close();
connection.close();
}
%>
这样你就可以从数据库中检索图像了谢谢。我需要为此写单独的行动。或者实际上,我有一个动作,我把所有的数据和图像也一起带来。所以对于图片,我确实需要写单独的动作,请您现在在DispatchActionCheck中写下完整的例子。这就是代码的样子,一个缩写。