【发布时间】:2012-02-03 20:59:02
【问题描述】:
我真的不了解字节数组...可能是因为我是第一次处理图像所以需要你的帮助
我有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 条记录,但是每当我执行调用 servlet 类 imgData 的 jsp 文件时,我在控制台上打印的每次刷新都会打印不同的值,并且图像也不会显示在 JSP 上
- 每次显示不同的值是编码错误或字节数组的特征。
- 为什么不显示图片帮帮我
【问题讨论】:
-
解码图像可能是图像无法正确显示在 JSP 中的原因。尝试将图像数据直接存储到数据库中而不进行解码。
-
@Ravindra Gullapalli 你的意思是说我应该只将图像存储为base64字符串......这对我来说效率太低了......
标签: java image jsp jakarta-ee