我真的不理解字节数组...可能是因为我在处理图像时,第一次如此需要你的帮助从数据库中检索字节组在servlet和JSP中
我的base64字符串R0lGODlhDwAPAKECAAAAzMzM///// wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw ==
对此我解码并将其存储在数据库中的MySQL然后我使用上我的servlet侧下面的代码
if (request.getParameter("imgID") != null)
{
iNumPhoto = Integer.parseInt(request.getParameter("imgID")) ;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost:9191/mbcss","root","admin");
stmt= conn.createStatement();
//conn.setAutoCommit (false);
// get the image from the database
byte[] imgData = GetPhoto.getPhoto(conn, iNumPhoto);
System.out.println("imgData="+imgData);
// display the image
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这里是getPhoto方法
public static byte[] getPhoto (Connection conn, int iNumPhoto)
throws Exception, SQLException
{
String req = "" ;
Blob img ;
byte[] imgData = null ;
Statement stmt = conn.createStatement();
// Query
req = "Select image From visit";
ResultSet rset = stmt.executeQuery (req);
while (rset.next())
{
img = rset.getBlob(1);
imgData = img.getBytes(1,(int)img.length());
}
rset.close();
stmt.close();
return imgData ;
}
访问表中只有1个的纪录,但每当我执行我的JSP文件,该文件调用servlet类imgData我已经印我的控制台上的每个刷新打印不同的价值和形象是也没有显示在JSP上
- 每次显示不同的值是编码错误或字节数组的特性。
- 为什么不显示图像帮助我
解码图像可能是图像未正确显示在JSP中的原因。尝试将图像数据直接存储到数据库而不进行解码。 – 2012-02-04 05:39:58
@Ravindra Gullapalli你的意思是说,我应该将图像存储为base64字符串只...这对我来说效率太低.. – 2012-02-04 08:21:34