资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

oracle如何上传图片 oracle 图片存储

oracle 中如何插入 图片?

将图片插入到BLOB类型的字段中

专注于为中小企业提供网站制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业虎丘免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

建立一个表:create table a(a blob);

建立一个目录:create directory tmp as 'e:\';

建立一个PL/SQL块:

declare

b_file bfile;

b_lob blob;

begin

insert into a(a) values(empty_blob()) return a into b_lob;

--为了使PL/SQL编译成功,必须先为该列插入一个empty_blob() 值,return a into b_lob将该列与一个blog类型的变量绑定在一起,以后只要为b_lob赋值,即等于将该值插入了表中。

b_file:=bfilename('tmp','filename.jpg');

--tmp是建立的文件所在的目录,filename.jpg是文件名;将文件转换为bfile类型

dbms_lob.open(b_file,dbms_lob.file_readonly);--将b_file 以只读的方式打开

dbms_lob.loadfromfile(b_lob,b_file,dbms_lob.getlength(b_file));-- 将b_file中的内容转换到b_lob中

dbms_lob.close(b_file);

commit;

end;

关于JAVA~~~~ 如何将图片等大对象存入ORACLE中~~求详解!(代码)

插入图片/文本(blob /clob)到oracle数据库(引用)

我们在写OA的时候经常遇到的问题就是员工图片档案的储存问题,解决这个问题有两个方法,

1.JSP/html页面里面读取web服务器上的图片,也就是把图片放到(上传)到web 服务器上,然后用html 语句读取:

img src=" 绝对或相对路径 " border="0" /

2.就是上传到数据库里面(oracle).关于oracle 数据库,它支持blob, 和clob, 分别对应着图片和文本(长字符串)操作

由于性能原因,我们还是要采用第二种方法,而且存到数据库里面比较容易管理,是吧?

首先,我们要解决上传问题,这里采用普遍使用的apache commons 组件里面的FileUpload class.

具体步骤如:

DiskFileUpload dfu=new DiskFileUpload();

dfu.setSizeMax(100000000);

dfu.setSizeThreshold(100000);

dfu.setRepositoryPath("f:\\public");

try{

List fileItems=dfu.parseRequest(request);

Iterator i=fileItems.iterator();

while(i.hasNext()){

FileItem fi=(FileItem)i.next();

if(!fi.isFormField()){

name=fi.getName();

size=fi.getSize();

if((name==null||name.equals(""))size==0)

continue;

}

name=fi.getName();

size=fi.getSize();

(InputStream)is=fi.getInputStream();

}

上面的代码是web服务器接受上传的代码,参考文件已经在我上篇写的上传文本文件里给出,今天,终于想明白了:

dfu.setRepositoryPath("f:\\public"); 的意思

原来是转义字符也就是说\n\t等而要打印反斜杠要用\\,其实这个问题原先已经知道,可是由于经验没有写过图片上传处理什么的,觉得很高深,也很可怕,哈哈,心里有点畏惧.看来基础的东西,那怕一点点小细节也很重要,接着还有下面的java IO 问题.刚才读core java 的时候突然发现在讲io的时候特意提醒了这个问题,可是我没有注意!

通过上面的代码已经实现文件上传了.然后,我们要实现JDBC数据源链接,目的是要把数据插入到oracle.

Context ctx=new InitialContext();

DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");

conn=ds.getConnection();

conn.setAutoCommit(false);

关于要import java.sql.* javax.sql.* java.naming.* 不再详细叙述了

接着根据很有用的一篇文章的提示,插入blob类型一定要先1.插入一个空的

String insert=" insert into uploadpicture "+

" values(?, empty_blob()) " ;

2.然后找到这个blob的oracle 里面的游标:

String findCursor=" select content "+

" from uploadpicture "+

" where name=? for update ";

注意这个for update(注意!!!必须加for update,这将锁定该行,直至该行被修改完毕,保证不产生并发冲突。这里还是难以理解,先记下来吧)

3.然后再修改

String update=" update uploadpicture "+

" set content=? "+

" where name=? ";

这里的问号是为PreparedStatement参数处理而写的!

写这个程序用到了oracle.sql.BLOB class ,这个类是用来操作BLOB数据类型的

当我们通过ResultSet 对象得到

blob=(BLOB)rs.getBlob(1);

的时候我不知道如何处理了,Blob 是什么?String, int ,long? 我现在也不明白!估计CSDN上的人也不明白,否则我发个帖子半天没有人回答,也许是很烂,也许是太简单了,大家不屑一顾,看来我还要继续追赶!

不发牢骚了,回到程序里(总觉得自己的发散思维很强,看来写程序的时候不能这样,多亏java 是纯面向对象语言,如果是过程就麻烦了)

我们如何处理这个blob 呢?回答是,不管它是什么,直接写入 BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

这里是建立了缓冲写如blob 的流(注意getBinaryOutputStream()已经不被赞成使用了,一定有更优秀的方法替代!),说到流,我到现在还有点晕,类很多,不知道究竟用哪个好!

基础的东西非常重要,这曾经是我的口头禅,这里用到了流的读入写和写入,有些流是从文件或其它位置上读取字节(如, FileInputStream),有写流是把字节组合成有用的数据(如, DataInputStream).我们读取数字的时候,需要首先建议一个FileInpuStream, 然后, 再把该类的对象传递给DataInputStream

FileInputStream fin=new FileInputStream(“emp.dat”);

DataInputStream din=new DataInputStream(fin);//把fin传递给din

double s=din.readDouble();

默认情况下,流是没有缓冲的, 如果使用缓冲就是

DataInputStream din=new DataInputStream(

new BufferedInputStream(new FileINputStream(“emp.dat”)));

有了这点理解也很管用,

BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

就是建立一个缓冲写的对象到blob.注意这里的out1 不是out,否则程序运行的时候不能打印了temp 数据了!

已经准备好如何写了, 可是如何读呢?

BufferedInputStream in=new BufferedInputStream(is);

在我们上传的时候 (InputStream)is=fi.getInputStream();

读取图片为输入的流.保存为is 对象,然后就用到这里了,准备好了读和写了,我们开始干活:

int c;

while((c=in.read())!=-1) {out1.write(c);}

in.close();

out1.close();

通过缓冲一个个读数据,然后一个个写数据.-1 为文件的末尾,

最后当读写完成后我们要关闭读写对象!

程序分析就是这样,以后还要对此问题进行研究,最后还要注意,

%@ page contentType="image/jpeg;charset=GBK"%

不是

%@ page contentType="text/html;charset=GBK"%

否则是以文字显示图片---乱码.

这里研究了上传图片到oralce 里面的程序,关于显示还要麻烦一点,借助资料我实现了,明天再研究一下.

//插入上传图片到数据库

%@ page contentType="text/html;charset=GBK"%

%@ page import="java.util.*"%

%@ page import="java.io.*"%

%@ page import="org.apache.commons.*"%

%@ page import="org.apache.commons.fileupload.*"%

%@ page import="java.sql.*"%

%@ page import="javax.sql.*"%

%@ page import="javax.naming.*"%

%@ page import="oracle.sql.*"%

html

head

meta http-equiv="Content-Type" content="text/html; charset=GBK"

titlegetPicture.jsp/title

/head

body

%

request.setCharacterEncoding("GBK");

String name=null;

long size=0;

Connection conn=null;

String insert=" insert into uploadpicture "+

" values(?, empty_blob()) " ;

String findCursor=" select content "+

" from uploadpicture "+

" where name=? for update ";

String update=" update uploadpicture "+

" set content=? "+

" where name=? ";

BLOB blob=null;

InputStream is=null;

DiskFileUpload dfu=new DiskFileUpload();

dfu.setSizeMax(100000000);

dfu.setSizeThreshold(100000);

dfu.setRepositoryPath("f:\\public");

try{

List fileItems=dfu.parseRequest(request);

Iterator i=fileItems.iterator();

while(i.hasNext()){

FileItem fi=(FileItem)i.next();

if(!fi.isFormField()){

name=fi.getName();

size=fi.getSize();

if((name==null||name.equals(""))size==0)

continue;

}

name=fi.getName();

size=fi.getSize();

is=fi.getInputStream();

}

Context ctx=new InitialContext();

DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");

conn=ds.getConnection();

conn.setAutoCommit(false);

//step 1

PreparedStatement ps=conn.prepareStatement(insert);

ps.setString(1, name);

int a=ps.executeUpdate();

if(a0)

out.println("insert success!"+"br");

//step 2

ps=conn.prepareStatement(findCursor);

ps.setString(1, name);

ResultSet rs=ps.executeQuery();

while(rs.next())

{

blob=(BLOB)rs.getBlob(1);

out.println("find cursor success!"+"br");

out.println("cursor :"+blob+"br");

//step 3

ps=conn.prepareStatement(update);

ps.setBlob(1, blob);

ps.setString(2, name);

ps.executeUpdate();

ps.close();

BufferedOutputStream out1 =new BufferedOutputStream(blob.getBinaryOutputStream());

BufferedInputStream in=new BufferedInputStream(is);

int c;

while((c=in.read())!=-1) {out1.write(c);}

in.close();

out1.close();

out.println("update success!"+"br");}

conn.commit();

}

catch(SQLException se)

{se.printStackTrace();}

catch(FileUploadException fue)

{fue.printStackTrace();}

%

/body

/html

//显示数据库里面的图片

%@ page contentType="image/jpeg;charset=GBK"%

%@ page import="java.sql.*"%

%@ page import="javax.sql.*"%

%@ page import="javax.naming.*"%

%@ page import="java.io.*"%

%@ page import="com.sun.image.codec.jpeg.*"%

%@ page import="javax.imageio.*"%

%@ page import="java.util.*"%

%@ page import="java.awt.image.*"%

html

head

meta http-equiv="Content-Type" content="image/jpeg; charset=GBK"

titleshowDBImage.jsp/title

/head

body

%

String showImage=" select * "+

" from uploadpicture "+

" where name=´TXC with snow.JPG´ " ;

Connection conn=null;

BufferedInputStream inputImage=null;

try{

Context ctx=new InitialContext();

DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS");

conn=ds.getConnection();

Statement st=conn.createStatement();

ResultSet rs=st.executeQuery(showImage);

while(rs.next())

{

oracle.sql.BLOB blob=(oracle.sql.BLOB)rs.getBlob("content");

inputImage =new BufferedInputStream(blob.getBinaryStream());

/*String name=rs.getString(1);

String content=rs.getString(2);

out.println(name+"br");*/}

BufferedImage image=null;

image=ImageIO.read(inputImage);

ServletOutputStream sos=response.getOutputStream();

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(sos);

encoder.encode(image);

inputImage.close();

conn.commit();

}

catch(SQLException se)

{se.printStackTrace();

conn.rollback(); }

catch(IOException ie)

{ie.printStackTrace();}

%

/body

/html

oracle PL/SQL 怎样将图片上传图片到表

我给你个例子你就知道了。。表和列名改成你相应的列和表就可以了。不明白的继续问我吧。

declare

src_lob blob;

file_lob bfile;

amount int;

src_offset int:=1;

file_offset int:=1;

filename_in varchar2(20):='filename_in';--为你要插入的文件名称

id_in number(6):=id;

begin

file_lob:=bfilename('UTLLOBDIR',filename_in);--UTLLOBDIR为你所建立的目录名称

select photo into src_lob from lob_employee where id=id_in for update;

if dbms_lob.fileexists(file_lob)=1 then

dbms_output.put_line('文件名为:'||filename_in||'的图片存在,正在进行图片写入');

dbms_lob.fileopen(file_lob,0);--打开你要插入的文件

amount:=dbms_lob.getlength(file_lob);

dbms_lob.loadblobfromfile(src_lob,file_lob,amount,src_offset,file_offset);

dbms_output.put_line('文件名为:'||filename_in||'的图片写入完毕!');

dbms_lob.close(file_lob);

else

raise_application_error(-20001,'文件名为:'||filename_in||'的图片不存在,请检查!');

dbms_output.put_line('文件名为:'||filename_in||'的图片不存在,请检查!');

end if;

exception

when no_data_found then

raise_application_error(-20002,'个人编码为:'||id_in||'的个人信息不存在,请检查!');

when others then

dbms_output.put_line('错误编码:'||SQLCODE);

dbms_output.put_line('错误信息:'||SQLERRM);

end;

如何向ORACLE数据库表中存入带图片的文件?

create table test

(

NID VARCHAR2(1) not null,

IMG BLOB,

zhengqueFlag VARCHAR2(1),

cuowuFlag VARCHAR2(1)

)

说明:

1、IMG字段只需要保存图片,将图片信息经过Base64编码,存到数据库BLOB字段中。显示的时候要经过Base64解码。

2、对于像“正确”、“错误”用标志位在区别。你可以用1表示正确、0表示错误。当然你也可以用汉字的保存这样的信息。自己来设计。

然后你到网上查一下怎么样对BLOB字段进行操作。如果不行,我在增加回复。

java中使用jsp向oracle实现图片上传(自定义路径)

我只说说思路吧,代码你自己写哈!

首先我建议使用SmartUpload这个jar包,至于怎么使用,可以百度

点击上传的时候,后台要处理的步骤是:

a.根据“电影类型”判断当前路径是否已经存在系统所在物理环境的指定位置,如果不存在创建该路径文件夹

b.如果存在,将该文件存在指定位置.

然后将该路径保存到oracle中


文章题目:oracle如何上传图片 oracle 图片存储
网页链接:http://cdkjz.cn/article/dooopoh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220