sql = "select tp from gys_sp where id_admin=? and spbm=? FOR UPDATE"; ps = conn.prepareStatement(sql);
ps.setInt(1, _sp.getId());
ps.setString(2, _sp.get_spbm());
rs = ps.executeQuery();
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("tp");
OutputStream outStream = ((BLOB) blob)
.getBinaryOutputStream();
System.out.println(_sp.get_file1());
File file = new File(_sp.get_file1());
BufferedInputStream inStream = new BufferedInputStream(
new FileInputStream(file));
byte[] b = new byte[blob.getBufferSize()]; int lenth = 0; while ((lenth = inStream.read(b)) != -1) {
outStream.write(b, 1, lenth);
} inStream.close();
outStream.flush();
outStream.close();
conn.commit();
写是写到里面了.数据也有.不过读的时候.
 ps = conn.prepareStatement(sql) ;
 ps.setInt(1,id) ;
 rs = ps.executeQuery() ;
 if (rs.next()) {
 blob = ((oracle.jdbc.OracleResultSet)rs).getBLOB("tp");   
 name = rs.getString("tp_name") ;
 InputStream in = blob.getBinaryStream();
//  try {
//                     long nLen = blob.length();
//                     int nSize = (int) nLen;
//                     data = new byte[nSize];
//                     inStream.read(data);
//                     inStream.close();
//                 } catch (IOException e) {
//
//                     System.out.println("获取图片数据失败,原因:" + e.getMessage());
//
//                 }
 out=new FileOutputStream("C:/aaa.jpg");
// long nLen = blob.length();
               // int nSize = (int) nLen;
                data = new byte[1024]; 
            int   bytesRead   =   0;   
            while   ((bytesRead=in.read(data))!= -1)   {  
             out.write(data,   0,   bytesRead);   
            }   
           ps.clearParameters();   
           in.close() ;
           out.close() ;
读出来的图片和原来的图片大小一样.可是打开却什么也没有.为什么?

解决方案 »

  1.   

    我改成这样还是不行.
    if (rs.next()) {
    blob = ((oracle.jdbc.OracleResultSet)rs).getBLOB("tp");   
     name = rs.getString("tp_name") ;
     InputStream in = blob.getBinaryStream();
    BufferedInputStream bin = new BufferedInputStream(in) ;
    //  try {
    //                     long nLen = blob.length();
    //                     int nSize = (int) nLen;
    //                     data = new byte[nSize];
    //                     inStream.read(data);
    //                     inStream.close();
    //                 } catch (IOException e) {
    //
    //                     System.out.println("获取图片数据失败,原因:" + e.getMessage());
    //
    //                 }
     out=new FileOutputStream("C:/aaa.jpg");
    // BufferedOutputStream bout = new BufferedOutputStream(out) ;
    // long nLen = blob.length();
                   // int nSize = (int) nLen;
                    data = new byte[1024]; 
                int   bytesRead   =   0;   
                while   ((bytesRead=bin.read(data))!= -1)   {  
                 out.write(data,   0,   bytesRead);   
                }   
               ps.clearParameters();   
               in.close() ;
               out.close() ;
     }
      

  2.   

       //是用二进制装载blob类型
       public static void readPicture() throws Exception
       {
       Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       String sql="insert into picture values(?)";
       File file=new File("D:\\说明文档\\图片\\dc7d2def849cf221adafd51b.jpg");
       InputStream in=new BufferedInputStream(new FileInputStream(file));
       try
    {
    conn=SqlManagerSingle.getInstance().getConnection();
    ps=conn.prepareStatement(sql);
    ps.setBinaryStream(1,in,(int)file.length());
    int i=ps.executeUpdate();
    System.out.println(i);
    } catch (SQLException e)
    {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    finally
    {
    SqlManagerSingle.getInstance().free(conn, ps, rs);
    in.close();
    }
       }
       public static void writerPicture() throws IOException
       {
       Connection conn=null;
       Statement st=null;
       ResultSet rs=null;
       String sql="select * from picture";
       try
    {
    conn=SqlManagerSingle.getInstance().getConnection();
    st=conn.createStatement();
    rs=st.executeQuery(sql);
    while(rs.next())
    {
    InputStream in=rs.getBinaryStream(1);
    byte[] bin=new byte[1024];
    OutputStream out=new BufferedOutputStream(new FileOutputStream("d:\\dd.jpg"));
    for(int i=0;(i=in.read(bin))>0;)
    {
    out.write(bin, 0, i);
    }
    in.close();
    out.close();
    }
    } catch (SQLException e)
    {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    finally
    {
    SqlManagerSingle.getInstance().free(conn, st, rs);
    }

       }
      

  3.   

    我的这个是可以的,我用的是oracle数据库
      

  4.   

    我试了rabbit_liu的方法.只能插入小的图片.
    我试一个12k和一个1k两张图片.
    结果1k没有问题.但是12就有问题了.
      

  5.   

    你这个只能存放1K的,
    是不是和rabbit_liu给你的文件中的初始化有关?
    byte[] bin=new byte[1024]; 
    你设置大些再试一下,