看下下面的示例
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;public class photo extends HttpServlet
{
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;   
    public void init(ServletConfig conf) throws ServletException
     {
super.init(conf); try{
            Class.forName("org.gjt.mm.mysql.Driver");      
   }catch(ClassNotFoundException e){}
String url="jdbc:mysql://localhost:3306/Sample?user=root;password=";
// String user="root",password="";
try{
      conn=DriverManager.getConnection(url);
      pstmt=conn.prepareStatement("select photo from person where id=?");
   }catch(SQLException e){System.err.println("Something is error!");}
     }
    public void doGet(HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException
     {
int id=Integer.parseInt(request.getParameter("id"));
int length;
InputStream is=null;
byte[] buffer=new byte[4096];
response.setContentType("images/*");
DataOutputStream os=null;
try{
     pstmt.setInt(1,id);
     rs=pstmt.executeQuery();
     os=new DataOutputStream(response.getOutputStream());
     is=rs.getBinaryStream("photo");
   }catch(SQLException e){} 
while((length=is.read(buffer))!=-1)
   {
      os.write(buffer,0,length);
      os.flush();
   }   
     }
    public void doPost(HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException
     {
doGet(request,response);
     }
    public void destroy()
     {
try{
      rs.close();
      pstmt.close();
      conn.close();
           }catch(SQLException e){}
     }
}
在JSP里的调用方式:
<img src="/homepage/servlet/photo?id=<%=id%>">

解决方案 »

  1.   

    谢谢您。
    还想请教:如果image对象中存的是word或者excel文档呢?
      

  2.   

    如果image对象中存的是word或者excel文档的话,你也可以通过上面的方法取出来,但是不能直接发到客户端,这在servlet中比较难实现,如果用jsp可能可以,如果使用jsp的话,你可以用javascript来创建相应的com对象,然后一切都ok了。
      

  3.   

    应该也可以的,不过需要改变一下response.setContentType("images/*");
    即改images/*为Application/msword(Application/msexcel)试试。在ASP里可以,JSP里没有用过,应该也可以的。
      

  4.   

    import beanPackage.*;
    import java.io.*;
    import java.sql.*;import javax.servlet.*;
    import javax.servlet.http.*;public class OpenExcel extends javax.servlet.http.HttpServlet {public void service(HttpServletRequest request, HttpServletResponse response) {
    try {
    byte b[] = new byte[102400];
    int length = 0;
    InputStream in = null;                  //连接数据库
             Connection conDB = (new ConnectDB()).connectDB("auth");
    String sql = " SELECT FILE_WORD FROM WORD_TABLE WHERE FILE_ID='01'"; PreparedStatement stmt = conDB.prepareStatement(sql);
    ResultSet rs = null;
    rs = stmt.executeQuery(); while (rs.next()) {
    in = rs.getBinaryStream(1);
    length = in.read(b);
    }
    response.setContentType("Application/msword");
    DataOutputStream os = null;
    os = new DataOutputStream(response.getOutputStream());
    if (length != -1) {
    os.write(b, 0, length);
    os.flush();
    }
    } catch (Exception e) {
    System.out.println("e: " + e);
    }
    }
    }
    在jsp中调用<img src=/servlet/OpenWord>,还是不能正常显示,请教应该怎样处理?
    还有:javascript来创建相应的com对象怎么用呢?
      

  5.   

    如果我在链接中调用这个class,就会显示一堆乱码,是需要进行内码转换吗?
      

  6.   

    http://somurl/servlet/OpenWord看看。当然不能用<img src=/servlet/OpenWord>拉,因为那是图象的显示方法啊,你做个连接<a href="/servlet/OpenWord">点这里</a>看看。
    javascript来创建相应的com对象我也不会拉,不好意思
    内码转换?不清楚
      

  7.   

    我知道<img……>不行啦,但是我不知道应该用什么呀,所以就先这样写了,不好意思。
    链接我也做过,仍是乱码。
    我用的环境:
    sybase数据库,win2000 server,Websphere Application Server3.5
    会不会是环境的问题呢?
      

  8.   

    终于解决了,程序没有问题,是安装office的格式转换工具时出了问题。
    response.setContentType("Application/msword"); //(Application/msexcel)
    可以通过执行servlet在浏览器中打开word和excel了。
    谢谢苯笨和草中宝!