在Oracle数据库中的图像数据时,在JSP中显示图像

时间:2014-02-16 09:20:36

标签: jsp

我尝试从oracle数据库访问图像数据并在jsp上显示图像。写下面的代码。

<%
try
{
    ResultSet rs=stmt.executeQuery("select * from PICTABLE where ID=200");
    if(rs.next())
    {
        Blob b1=rs.getBlob("PIC");
        byte x[]=b1.getBytes(1, (int)b1.length());
        OutputStream o=response.getOutputStream();
        o.write(x);
        o.close();
    }
    System.out.println("Done");
}
catch(Exception ex)
{
    ex.printStackTrace();
}
%>

<% try { ResultSet rs=stmt.executeQuery("select * from PICTABLE where ID=200"); if(rs.next()) { Blob b1=rs.getBlob("PIC"); byte x[]=b1.getBytes(1, (int)b1.length()); OutputStream o=response.getOutputStream(); o.write(x); o.close(); } System.out.println("Done"); } catch(Exception ex) { ex.printStackTrace(); } %> 运行此代码并使用IE浏览器时,显示图像出错

[java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
当使用chrome浏览器时,图像数据不会以图像格式转换,并且会出现上面的大厦错误。如何为每个浏览器解决此问题。

1 个答案:

答案 0 :(得分:0)

你之前在这个页面上调过了getWriter()somwhere吗? 来自文档:

  

可以调用此方法或getWriter()来编写正文,而不是两者。

http://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#getOutputStream()

相关问题