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?
我现在也不明白
!
估计
上的人也不明白
,
否则我发个帖子半天没有人回答
,
也许是很烂
,
也许是太简单了
,
大家不屑一顾
,
看来我还要继续追赶
!
不发牢骚了
,
回到程序里
(
总觉得自己的发散思维很强
,
看来写程序的时候不能这样
,
多亏
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 里面的程序,关于显示还要麻烦一点,借助资料我实现了,明天再研究一下.
<html>
<head>
<meta http-equiv="Content-Type" content="image/jpeg; charset=GBK">
<title>showDBImage.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>