1、首先读取硬盘上的某一具体图片文件,读取模式设置为readBinary方式:
<cffile
action
=
"readBinary"
file
=
"temp
directory
here#file.serverFile#"
variable
=
"test">
2、将读取出来的二进制内容存储至数据库中(注:数据库字段需设置成能存储图片类型的字段,如blob类型):
<cfquery
datasource
=
"datasource">
insert
into
imageTest
values
(<cfqueryparam
cfsqltype="cf_sql_blob"
value="#test#">)
</cfquery>
通过1、2两个步骤,我们轻松实现了读取图片文件并存储至数据库的 *** 作过程。
3、从数据库中读取图片信息,该文件可命名为dispImage.cfm:
<!---
在此需特别注意enablecfoutputonly的压缩空白功能,如果不对该页面进行空白压缩,很可能会造成图片无法显示的问题
--->
<cfprocessingdirective
suppressWhiteSpace="yes">
<cfsetting
enablecfoutputonly="yes">
<!---
读取相应的图片信息
--->
<cfquery
datasource
=
"datasource">
select
image
from
imageTest
where
variable
here#
</cfquery>
<!---
设置浏览器输出的格式,我们将它设置为图片的JPG类型,用户可根据实际情况改动类型设置
--->
<cfcontent
type="image/jpg">
<!---
输出图片
--->
<cfoutput>#toString(imageTest.image)#</cfoutput>
</cfprocessingdirective>
<cfabort>
4、显示图片内容,调用dispImage.cfm页面:
<img
src
=
"dispImage.cfm?id=your
variable
here">
通过3、4两个步骤,我们也很容易的就完成了从数据库中读取图片信息并在页面显示的功能。
总结:实际上,除了图片文件可以如此处理,其它的文件也能通过类似方式进行处理,可将任意文件类型存储至数据库,只是文件大小的原因以及数据库存储读取速度性能限制,我们基本上还是不建议将文件存储至数据库,毕竟硬盘读取要快得多。
先把文件读取到内存,再以二进制格式保持到数据库中的大字段中(clob或clob)。写大对象。
Java code
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null
Statement stat = null
ResultSet rs = null
OutputStream os = null
FileInputStream fis = null
int bs = 0
try {
Class.forName("oracle.jdbc.driver.OracleDriver")
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---")
conn.setAutoCommit(false)
stat = conn.createStatement()
stat.executeUpdate("insert into t_video(id,video) values(1,empty_blob())")
rs = stat.executeQuery("select video from t_video where id = 1")
rs.next()
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1)
os = blo.getBinaryOutputStream()
bs = blo.getBufferSize()
fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-K&K\\mplayer.exe")
byte[] buf = new byte[bs]
int length = 0
while(true)
{
length = fis.read(buf)
if(length == -1) break
os.write(buf,0,length)
}
os.close()
os = null
fis.close()
fis = null
conn.commit()
conn.setAutoCommit(true)
conn.close()
} catch(Exception ex) {
ex.printStackTrace()
}
}
读大对象
Java code
InputStream is = null
FileOutputStream fos = null
byte[] buf = null
int bs = 0
try {
Class.forName("oracle.jdbc.driver.OracleDriver")
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-")
conn.setAutoCommit(false)
stat = conn.createStatement()
rs = stat.executeQuery("select video from t_video where id = 1")
rs.next()
oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1)
bs = blo.getBufferSize()
buf = new byte[bs]
int length = 0
is = blo.getBinaryStream()
fos = new FileOutputStream("d:\\test.exe")
while(true) {
length = is.read(buf)
if(length == -1) break
fos.write(buf,0,length)
}
fos.close()
fos = null
is.close()
is = null
conn.commit()
conn.setAutoCommit(true)
conn.close()
...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)