日期:2014-05-18  浏览次数:20824 次

在jsp中如何输出oracle的long raw类型数据(是保存JPG图片)
在jsp中如何输出oracle的long raw类型数据(是保存JPG图片)
下面这段代码,有时能输出图片,有时不能,请帮忙修改下~~
<%@ page contentType="text/html;charset=UTF-8" %>
  <%@ page import="java.sql.*"%>  
  <%@ page import="java.io.*"%>  
  <jsp:useBean id="sqlConn" scope="application" class="dgeip.OraconnDg"/>
<%  

  Connection con=null;
  Statement stmt=null;
try{
  con = sqlConn.getConnection();
  stmt = con.createStatement();//数据库的连接  
String sql="select * from dd_model WHERE model_no= '07024'";
ResultSet rs=stmt.executeQuery(sql);
rs.next();  
  InputStream in=rs.getBinaryStream("shoe_pic");  
  response.reset();  
  response.setContentType("image/jpeg");  
  byte[] b=new byte[1024];  
  int len;  
 while((len=in.read(b))>0)  
{
 response.getOutputStream().write(b,0,len); 
}
in.close();
rs.close();
stmt.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
不能输出时,报错:
getOutputStream() has already been called for this response  



------解决方案--------------------
while((len=in.read(b)) >0)

 response.getOutputStream().write(b,0,len);


response只能get1次OutputStream
------解决方案--------------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="sqlConn" scope="application" class="dgeip.OraconnDg"/>
<%

Connection con=null;
Statement stmt=null;
try{
con=sqlConn.getConnection();
stmt=con.createStatement();//数据库的连接
String sql="select * from dd_model WHERE model_no='07002'";
ResultSet rs=stmt.executeQuery(sql);
rs.next();
InputStream in=rs.getBinaryStream("shoe_pic");
response.reset();
response.setContentType("image/jpeg");
byte[] b=new byte[1024];
int len;
while((len=in.read(b))>0)
{
response.getOutputStream().write(b,0,len); 
}
out.clear();
out = pageContext.pushBody();
in.close();
rs.close();
stmt.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>

直接加上
out.clear();
out = pageContext.pushBody();
就OK了,试试