oracle读写图片(C#,Java)

来源:互联网 发布:加工中心半圆编程 编辑:程序博客网 时间:2024/05/16 08:03
 
//C#篇            //写入            string connStr = "Data Source=123;user id=123;password=123";            OracleConnection conn = new OracleConnection(connStr);            try            {                conn.Open();                string fileName = @"D:\121.jpg";                                FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);                byte[] image = new byte[fs.Length];                fs.Read(image, 0, System.Convert.ToInt32(fs.Length));                fs.Close();                OracleCommand cmd = new OracleCommand("INSERT INTO table (pic_name,pic_content) VALUES ('123',empty_blob()) ", conn);                cmd.ExecuteNonQuery();                OracleDataAdapter da = new OracleDataAdapter("select * from r_pic_glss where glass_id='AKA0703TUB' and pic_url='123' for update ", conn);                OracleCommandBuilder ocb = new OracleCommandBuilder(da);                DataSet ds = new DataSet();                da.Fill(ds);                da.MissingSchemaAction = MissingSchemaAction.AddWithKey;                ds.Tables[0].Rows[0]["pic_content"] = image;                da.Update(ds.Tables[0]);            }            catch (Exception ex)            {                //            }            finally            {                conn.Close();            }            //读取图片并放到listview            imageList1 = loadPicbyName(picList);            imageList1.ImageSize = new Size(128, 128);            listView1.LargeImageList = imageList1;            public  ImageList loadPicbyName(List<string> picNameList)            {                ImageList il = new ImageList();                foreach (string pic in picNameList)                {                    Image image = queryImageDataByName(pic);                    if (image != null)                    {                        il.Images.Add(image);                    }                }                return il;            }            public Image queryImageDataByName(string pic_name)            {                string sql = string.Format(" select t.* from table t where t.pic_name = '{0}' ", pic_name);                SqlBean sqlBean = new SqlBean();                DataTable dt = sqlBean.doQuery(sql);                if (dt != null && dt.Rows.Count > 0)                {                    System.Byte[] pic_content = (byte[])dt.Rows[0]["pic_content"];                    MemoryStream buf = new MemoryStream(pic_content);                    Image image = Image.FromStream(buf, true);                    return image;                }                else                {                    return null;                }            }

 

//java

private FTPClient ftp;public byte[] downloadFileIntoByte(String remoteFilePath)throws Exception {byte[] byteRtn = new byte[0];try {long time1 = System.currentTimeMillis();if (!ftp.isConnected()) {connect();}ftp.enterLocalPassiveMode();ftp.setFileType(FTP.BINARY_FILE_TYPE);InputStream in = ftp.retrieveFileStream(remoteFilePath);int c;byte[] bytes = new byte[1024];while ((c = in.read(bytes)) != -1) {int preByteRtnLen = byteRtn.length;byte[] byteRtnTemp = byteRtn;byteRtn = new byte[preByteRtnLen + c];System.arraycopy(byteRtnTemp, 0, byteRtn, 0, byteRtnTemp.length);if (c < 1024) {System.arraycopy(bytes, 0, byteRtn, preByteRtnLen, c);} else {System.arraycopy(bytes, 0, byteRtn, preByteRtnLen,1024);}}in.close();ftp.completePendingCommand();} catch (Exception e) {_log.error("downloadFileIntoByte [" + remoteFilePath + "] Fail", e);throw e;}return byteRtn;}private void insertIamgeIntoDB(RPicGlss rpg) throws Exception {StringBuffer sql = new StringBuffer();sql.append("    INSERT INTO table ");sql.append("    (pic_name, pic_content) ");sql.append("    VALUES ");sql.append("    (?, empty_blob) ");SqlBean sqlBean = new SqlBean(sql.toString);sqlBean.addParameter("123");int updateCount = sqlBean.executeUpdate();if (updateCount == 1) {sql.setLength(0);sqlBean = null;StringBuffer sql = new StringBuffer();sql.append("SELECT pic_content ");sql.append("FROM table ");sql.append("WHERE pic_name = ? ");sql.append("    FOR UPDATE ");sqlBean = new SqlBean(sql.toString());sqlBean.addParameter("123");List<Map> rtnLm = sqlBean.executeQuery();Blob blob = (Blob) rtnLm.get(0).get("pic_content");OutputStream os = blob.setBinaryStream(0);os.write(downloadFileIntoByte("D:\121.jpg"));os.flush();os.close();} else {throw new Exception("[Insert Record Fail] " + rpg.getPicUrl());}}

原创粉丝点击