当前位置: 首页 > >

将图片数据插入oracle数据库中_file1

发布时间:

Oracle 中插入图片并显示(用 BLOB 类型)
Oracle 中插入图片并显示(用 BLOB 类型) 要在 oracle 里面存入图片 用 blob 类型

首先在数据库里建立: --连接到管理员 conn sys/tbsoft as sysdba; --为 scott 用户授权 grant create any directory to scott; --回到 scott 用户 conn scott/tiger;

--创建存储图片的表 CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL); --创建存储图片的目录 CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\picture'; --在 c:下自己建一个叫 picture 的文件夹 CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS F_LOB BFILE;--文件类型 B_LOB BLOB; BEGIN iNSERT INTO IMAGE_LOB (T_ID, T_IMAGE) VALUES (TID,EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB; --插入空的 blob F_LOB:= BFILENAME ('IMAGES', FILENAME); --获取指定目录下的文件 DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY); --以只读的方式打开文件 DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB)); --传递对象 DBMS_LOB.FILECLOSE (F_LOB); --关闭原始文件

COMMIT; END; /

--在 C:\picture 下放一张图片 1.gif --将该图片存入表 call IMG_INSERT('1','1.gif');

然后创建一个 web 项目 连接数据库后 创建一个 BlobDAO 类 用来取出表中的 blob 类型图片 Java 代码 收藏代码 public class BlobDAO {

private static final BlobDAO instance = new BlobDAO();

private Connection conn = null;

private BlobDAO() {

}

public static BlobDAO getInstance() { return instance; }

private void initConn() { conn = DBAccess.getInstance().getConn(); }

public byte[] getImage(String imgname) { BufferedInputStream ins;//取得 BLOB 的 IO 流 byte[] bt = null;

initConn(); Blob bo = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "select T_IMAGE from IMAGE_LOB where t_id=?"; try { ps = conn.prepareStatement(sql); ps.setString(1, imgname); rs = ps.executeQuery(); if (rs.next()) { bo = rs.getBlob("T_IMAGE");

try { ins = new BufferedInputStream(bo.getBinaryStream()); int bufferSize = (int) bo.length();//取得 BLOB 的长度 bt = new byte[bufferSize]; try { ins.read(bt, 0, bufferSize); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //建立字节缓存 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }

} } catch (SQLException e) {

// TODO Auto-generated catch block e.printStackTrace(); } finally { try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

return bt; } }

在 action 里面调用 getImage()方法并显示图片在页面上 Java 代码 收藏代码 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub

BlobDAO blobDAO = BlobDAO.getInstance();

byte[] bs = blobDAO.getImage("1");

try {

response.getOutputStream().write(bs);

} catch (IOException e) {

// TODO Auto-generated catch block e.printStackTrace(); }

return null; }

添加图片到数据库 请在 c 盘下放入图片--c:\\4.gif Java 代码 收藏代码 public void savaImg(String imgId) { //传的是存入数据库图片的 id initConn(); Statement st = null; BLOB blob = null; //图片类型 OutputStream outputStream = null; //输出流 File file = null; //文件 InputStream inputStream = null; //输入流 ResultSet rs = null; try { conn.setAutoCommit(false); //事物由程序员操作 st = conn.createStatement(); st.executeQuery("insert into IMAGE_LOB values('"+ imgId +"',empty_blob())"); rs = st.executeQuery("select T_IMAGE from IMAGE_LOB where t_id='"+ imgId +"' for update"); if (rs.next()) { blob = (BLOB) rs.getBlob(1); outputStream = blob.getBinaryOutputStream(); file = new File("c:\\4.gif"); inputStream = new FileInputStream(file); byte[] b = new byte[blob.getBufferSize()]; int len = 0;

while ((len = inputStream.read(b)) != -1) { System.out.println(len); outputStream.write(b, 0, len); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { inputStream.close(); outputStream.flush(); outputStream.close(); rs.close(); st.close(); conn.commit(); conn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

操作完毕! 作者“669098238”




友情链接: