java读取oracle图片_oracle读写图片(C#,Java)

//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 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 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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值