微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Oracle数据库保存、下载图片

存储图片class:
test表结构( id name image)

public class Test{
    PreparedStatement pst = null;
    Result rs = null;
    Connection conn = ConnectionManager.getConnection();
    String sql = "insert into test(id,name,image) values(?,?,?)";
    try {
          pst = conn.prepareStatement(sql);
          pst.setInt(0,1);
          pst.setString(1,"testData");
          //事先插入空对象empty_blob()  
          pst.setBlob(2,BLOB.empty_lob()); 
          pst.executeUpdate();

          OutputStream os = null;
          // for update 锁定数据行进行更新
          String q_sql = "select image from test_img where id = ? for update"; 
          pst = conn.prepareStatement(q_sql);
          pst.setInt(1,1);
          rs = pst.executeQuery();
          if (rs.next()) {
              oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image");
              os = blob.getBinaryOutputStream();
              InputStream is = new FileInputStream("C:\\image.jpg");
              int i = 0;
              while ((i = is.read()) != -1) {
                  os.write(i);
              }
          }
          is.close();
          os.flush();
          os.close();
          ConnectionManager.closeAll(rs,pst,conn); // 关闭资源

      } catch (sqlException e) {
            e.printstacktrace();
      } catch (FileNotFoundException e) {
            e.printstacktrace();
      } catch (IOException e) {
            e.printstacktrace();
      }
    }
}

根据id获取图片byte数据

public class Query{
 public byte[] GetImgByteById(Int id,Connection conn){   
    byte[] data = null;  
    try {  
            String sql = "select image from test where id = ?";
            PreparedStatement pst = null;  
            pst = conn.prepareStatement(sql);
            pst.setInt(0,id);
            rs = pst.executeQuery();

            StringBuffer myStringBuffer = new StringBuffer();  
            if (rs.next()) {  
                java.sql.Blob blob = rs.getBlob("image");  
                InputStream in = blob.getBinaryStream();  
                try {  
                    long nLen = blob.length();  
                    int nSize = (int) nLen;  
                    data = new byte[nSize];  
                    in.read(data);  
                    in.close();  
                } catch (IOException e) {  
                    System.out.println("获取图片数据失败,原因:" + e.getMessage());  
                }  

            }  
            System.out.println(myStringBuffer.toString());  
            conn.commit();  
            conn.close();  
        } catch (sqlException ex) {  
            System.out.println(ex.getMessage());  
        }  
        return data;  
    }
}

处理byte数据:

//获取图片的byte数据  
        id = 1;
        data = Query.GetImgByteById(id); 
        ServletoutputStream op = response.getoutputStream();         
       op.write(data,data.length); 
       op.close()

获取conn省略。

原文地址:https://www.jb51.cc/oracle/209621.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐