数据表:PersonnelPhoto1 字段:photoimage 数据类型image
怎样使用Java读取该字段的值(存取的都是bmp位图)。请各位专家帮忙支招!最好有代码!

解决方案 »

  1.   


    import java.sql.*;
    import java.io.*;
    import java.lang.*;
    import java.awt.*;
    public class fileopen{
    public static void main(String args[]){
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:test","sa","");
    Statement stmt=con.createStatement();
    ResultSet rs;
    String sql="select  photoimage from PersonnelPhoto1  where xx=xx";
    rs=stmt.executeQuery(sql);
     while(rs.next())
     {
             InputStream image = rs.getBinaryStream("photoimage");//photoimg为image类型
             String  len=image.toString();
             long length=len.length();
             int ch = 0;
             byte[] buffer = new byte[(int)length/7];
             File file = new File("c:\\test.jpg");//将数据写入文件
            if(!file.exists())
           {
             file.createNewFile();
           }
          FileOutputStream newFile = new FileOutputStream(file,true);      boolean go = true;
          while(go)
         {
          while((ch = image.read(buffer))!=-1)
          {
            newFile.write(buffer);
          }
         go=false;
         }
      }
     }
      catch(Exception e){System.out.print(e);}
    }}
      

  2.   

    [code]
        OutputStream out;
        out = new FileOutputStream(file);
        /** //or for javax.servlet.http.HttpServletResponse
         *  response.setContentType("image/bmp");
         *  out = response;
         */
        rs = stmt.executeQuery(sql);
        while (rs.next())
        {
            InputStream in = rs.getBinaryStream("photoimage");
            byte[] buffer = new byte[256];        while (in.read(buffer) != -1)
            {
                out.write(buffer);
            }
            in.close();
        }
        out.close();[code]
      

  3.   

    谢谢楼上两位,你们的代码提取Jpeg文件是可以,但Bmp位图好像不行吧?