chick_su:
非常感谢!我已编译通过,但在网页上浏览时不显图形,显示一小红叉,表明
图像链接有误服务器也不报错,网页也不报错,库中确实有记录。
附源:
//////////////////////////////////////////////////////////////////
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class ImageTest extends HttpServlet{
public voiddo
Get(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,java.io.IOException{
String sql="select image from T_CPXX";
ResultSet rs=null;
String sqlstr;
Statement stmt=null;
Connection con = null;
try
{
String jdbcDriver="oracle.jdbc.driver.OracleDriver";
String jdbcURL="jdbc
racle:thin
10.71.247.136:1521
ra8i";
String jdbcUser="12345";
String jdbcPassword="12345";
try{
Class.forName(jdbcDriver);
}catch(Exception e){
System.out.println("not found:"+e.toString());
}
try{
con =DriverManager.getConnection(jdbcURL,jdbcUser,jdbcPassword);
}catch(SQLException e){
e.printStackTrace();
System.out.println("stmi:"+e.toString());
throw new SQLException("Connect to db error");
}
stmt = con.createStatement();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(!rs.next()){
response.setContentType("text/html");
PrintWriter pout =new
PrintWriter(response.getOutputStream());
pout.print("No matching record found!");
pout.flush();
pout.close();
}//if
response.setContentType("image/gif");
javax.servlet.ServletOutputStream out=response.getOutputStream();
InputStream in=rs.getBinaryStream(1);
ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();
byte b[]=new byte[2048];
while(true){
int bytes=in.read(b);
if(bytes==-1) break;
baos.write(b,0,bytes);
}//while
b=baos.toByteArray();
response.setContentLength(b.length);
out.write(b,0,b.length);
out.flush();
out.close();
rs.close();
// pool.releaseConnection(con);
}catch(Exception e){
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.write("Exception ! ");
e.printStackTrace(pw);
pw.flush();
pw.close();
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null)
con.close();
}catch(Exception ex){}
}
}//doGet()
}//public class
/////////////////////////////////////////////////////////////////////////