在线等:JAVA中如何以大字段(BLOB)存储各类文件(ORACLE数据库)

解决方案 »

  1.   

    给你段代码:
    写入:
    // 准备数据流
                if (pItem.getPointDrawType() != null) {
                    try {
                        // 将对象转为二进制流保存
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutputStream oos = new ObjectOutputStream(bos);
                        // 输出每个对象
                        oos.writeObject(pItem.getPointDrawType());                    byte[] bb = bos.toByteArray();
                        stmType.setBytes(3, bb);
                    } catch (Exception e) {
                        System.out
                                .println("无法保存坐标到数据库!\t代码:PointTypeItemManager.addTypeItem()\t错误内容:"
                                        + e);                    stmType.setBytes(3,null);
                    }
                } else {
                    stmType.setBytes(3,null);
                }读取:
    byte[] typeByte = rsType.getBytes("TypeMapType");
                    if (!rsType.wasNull()) {
                        try {
                            // 从数据流构造对象
                            ByteArrayInputStream bis = new ByteArrayInputStream(
                                    typeByte);
                            ObjectInputStream ois = new ObjectInputStream(bis);                        // 读取对象
                            PointDrawType pdt = (PointDrawType) ois.readObject();
                            pst.setPointDrawType(pdt);
                        } catch (Exception ie) {
                            System.out
                                    .println("获取数据时出错!\t代码:PointManager.getPointTypeItem()\t错误内容:"
                                            + ie);
                        }
                    }
      

  2.   

    <%
    //request.getCharacterEncoding("gb2312");
    String content = "";
    InitialContext ctx=new InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/ccc");
    Connection con =  ds.getConnection();
    String sql="select photoStream,content from Newspaper_Photo where id =1";
    Statement stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery(sql);  try {
    while(rs.next()) {
    content =rs.getString("content");
    //rs.setContentType("image/jpg");
    ServletOutputStream sout = response.getOutputStream(); 
    InputStream in = rs.getBinaryStream(1);
    byte b[] = new byte[0x7a120];
    for(int i = in.read(b); i != -1;) 
    {
    sout.write(b);
    in.read(b);
    }
    sout.flush();
    sout.close();
    sout =null;
    }
    rs.close();
    stmt.close();
    con.close();
    rs =null;
    stmt =null;
    con =null; }
    catch(Exception e){
    System.out.println(e);
    }
    //out.println(sql1);
    %>
    sout =null;
    }
      

  3.   

    懒得写,给你段HIBERNATE操作BLOB的代码
    public Guoyf setBlob(Guoyf guoyf, String file)//实现了2进制文件存入数据库.
        {
         try{
         session = sf.openSession();
         guoyf.setB(Hibernate.createBlob(new byte[1]));    //---------------------B需要改为相应的BLOB键名
         transaction = session.beginTransaction();
         session.save(guoyf);
         session.flush();
         session.refresh(guoyf, LockMode.UPGRADE);
         BLOB blob = (BLOB)guoyf.getB();                    //---------------------B需要改为相应的BLOB键名
         OutputStream out = blob.getBinaryOutputStream();
         FileInputStream fis = new FileInputStream(file);
         byte[] buf = new byte[10240];
         int len;
         while((len=fis.read(buf))>0)
         {
         out.write(buf, 0, len);
         }
         fis.close();
         out.close();
         }catch(Exception e){
         e.printStackTrace();
         }
         return guoyf;
        }
      

  4.   

    印象当中,应该先插入一个空Blob的记录
    insert into table values(id, blob_col) values('XXX', empty_blob());
    然后ResultSet.getBlob("blob_col");
    在然后用流的形式向Blob对象写入数据,最后update这一条记录。