如何在网页上显示数据库(Orale&Sqlserver)中BLOB中的图像文件???(200分)

  • 主题发起人 主题发起人 luokj
  • 开始时间 开始时间
L

luokj

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在网页上显示数据库(Orale&Sqlserver)中BLOB中的图像文件???
 
我是在SQL SERVER中實現的﹕
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import PoolBean;
public class ImageTest extends HttpServlet{
public voiddo
Get(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,java.io.IOException{
String sql="select image from image";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
PoolBean pool=new PoolBean();
try{
if(pool.getDriver()==null){
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
pool.setURL("jdbc:odbc:sule");
pool.setSize(2);
pool.initializePool();
}
con=pool.getConnection();
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// con=DriverManager.getConnection("jdbc:odbc:sule","sa","");
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
其中類PoolBean是一個Connection Pooling
小弟也是剛學﹐請多多包涵﹐
這是一個Servlet直接運行就可以了﹗
 
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:oracle:thin:@10.71.247.136:1521:ora8i";
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
/////////////////////////////////////////////////////////////////////////
 
please sent to sule@y3mail.yydg.com.cn !!!!!
 
不好意思﹐我這里上網不是很方便﹐有事就給我MAIL到 :sule@y3mail.yydg.com.cn
小弟是一個菜鳥﹐也是剛學﹐我試著修改了一下﹕
........
javax.servlet.ServletOutputStream out=response.getOutputStream();
InputStream in=rs.getBinaryStream("image");
//讀取安段名稱
// ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();
byte b[]=new byte[4096];
while(true){
int bytes=in.read(b);
if(bytes==-1) break;
out.write(b,0,bytes);
}//while
out.flush();
out.close();
rs.close();
in.close();
。。。。。。。。
不知道你這樣直接輸出行不行?
 
用Delphi实现不了吗?
 
如果这样的话,这个页面只能显示图像了,如何解决图像文字混合的复杂页面呢?
我目前用iFrame的方法,似乎太笨拉
 
如果这样显示图像了,图像文字混合的复杂页面解决bu xing?
 
参考
http://www.java-cn.com/
 
后退
顶部