最近做一个J2EE项目,需要在JSP页面实现对文件的上传和下载。很早以前就知道JDBC支持大对象(LOB)的存取,以为很容易,做起来才发现问题多多,读了一大堆文章,反而没有什么头绪了。正如一位网友文章所讲:“…网络上的教程99%都是行不通的,连SUN自己的文档都一直错误……”,实际情况大致如此了。 
  
存取BLOB出现这么多问题,我认为大半是由数据库开发商、应用服务器商在JDBC驱动上的不兼容性带来的。而实际应用中,每个人的开发运行环境不同,使得某个网友的solution没有办法在别人的应用中重现,以至于骂声一片。至于为什么会不兼容、有哪些问题,我没有时间去弄清,这里只说说我们怎样解决了问题的。 
  
基于上述原因,先列出我们的开发环境,免得有人配不出来,招人唾骂。 
数据库 Oracle 9i 
应用服务器 BEA Weblogic 8.11 
开发工具 JBuilder X 
  
在JSP实现文件Upload/Download可以分成这样几块 :文件提交到形成InputSteam;InputSteam以BLOB格式入库;数据从库中读出为InputSteam;InputStream输出到页面形成下载文件。先说BLOB吧。 
  
1.  BLOB入库 
(1)       直接获得数据库连接的情况 
这是Oracle提供的标准方式,先插入一个空BLOB对象,然后Update这个空对象。代码如下: 
//得到数据库连接(驱动包是weblogic的,没有下载任何新版本) 
Class.forName("oracle.jdbc.driver.OracleDriver"); 
Connection con = DriverManager.getConnection( 
          "jdbc:oracle:thin:@localhost:1521:testdb", "test", "test"); 
//处理事务 
con.setAutoCommit(false); 
Statement st = con.createStatement(); 
//插入一个空对象 
st.executeUpdate("insert into BLOBIMG  values(103,empty_blob())"); 
//用for update方式锁定数据行 
ResultSet rs = st.executeQuery( 
          "select contents from  BLOBIMG  where  id=103 for update"); 
if (rs.next()) { 
   //得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB 
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1).; 
   //到数据库的输出流 
OutputStream outStream = blob.getBinaryOutputStream(); 
   //这里用一个文件模拟输入流 
File file = new File("d:\\proxy.txt"); 
  InputStream fin = new FileInputStream(file); 
//将输入流写到输出流 
byte[] b = new byte[blob.getBufferSize()]; 
        int len = 0; 
        while ( (len = fin.read(b)) != -1) { 
          outStream.write(b, 0, len); 
          //blob.putBytes(1,b); 
        } 
  
   //依次关闭(注意顺序) 
fin.close(); 
   outStream.flush(); 
   outStream.close(); 
   con.commit(); 
   con.close(); 
  
(2)       通过JNDI获得数据库连接 
在Weblogic中配置到Oracle的JDBC Connection Pool和DataSource,绑定到Context中,假定绑定名为”orads”。 
为了得到数据库连接,做一个连接工厂,主要代码如下: 
Context context = new InitialContext(); 
ds = (DataSource) context.lookup("orads"); 
return ds.getConnection(); 
以下是BLOB写入数据库的代码: 
  
Connection con = ConnectionFactory.getConnection(); 
con.setAutoCommit(false); 
Statement st = con.createStatement(); 
st.executeUpdate("insert into BLOBIMG values(103,empty_blob())"); 
ResultSet rs = st.executeQuery( 
          "select contents from  BLOBIMG  where  id=103 for update"); 
if (rs.next()) { 
    //上面代码不变 
//这里不能用oracle.sql.BLOB,会报ClassCast 异常 
weblogic.jdbc.vendor.oracle.OracleThinBlobblob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob(1); 
    //以后代码也不变 
OutputStream outStream = blob.getBinaryOutputStream(); 
File file = new File("d:\\proxy.txt"); 
  InputStream fin = new FileInputStream(file); 
byte[] b = new byte[blob.getBufferSize()]; 
        int len = 0; 
        while ( (len = fin.read(b)) != -1) { 
          outStream.write(b, 0, len); 
        } 
  
fin.close(); 
   outStream.flush(); 
   outStream.close(); 
   con.commit(); 
   con.close(); 
  
2.  BLOB出库 
从数据库中读出BLOB数据没有上述由于连接池的不同带来的差异,只需要J2SE的标准类java.sql.Blob就可以取得输出流(注意区别java.sql.Blob和oracle.sql.BLOB)。代码如下: 
Connection con = ConnectionFactory.getConnection(); 
con.setAutoCommit(false); 
Statement st = con.createStatement(); 
//这里的SQL语句不再需要”for update” 
ResultSet rs = st.executeQuery( 
          "select contents from  BLOBIMG  where  id=103 "); 
if (rs.next()) { 
   java.sql.Blob blob = rs.getBlob(1); 
  
   InputStream ins = blob.getBinaryStream(); 
  
    //用文件模拟输出流 
File file = new File("d:\\output.txt"); 
   OutputStream fout = new FileOutputStream(file); 
  
    //下面将BLOB数据写入文件 
    byte[] b = new byte[1024]; 
    int len = 0; 
        while ( (len = ins.read(b)) != -1) { 
          fout.write(b, 0, len); 
        } 
  //依次关闭 
  fout.close(); 
  ins.close(); 
  con.commit(); 
  con.close(); 
  
3.  从JSP页面提交文件到数据库 
  
(1)       提交页面的代码如下: 
<form action="handle.jsp" enctype="multipart/form-data" method="post" > 
<input type="hidden" name="id" value="103"/> 
<input type="file"  name="fileToUpload"> 
<input type="submit"  value="Upload"> 
</form> 
  
(2)       由于JSP没有提供文件上传的处理能力,只有使用第三方的开发包。网络上开源的包有很多,我们这里选择Apache Jakarta的FileUpload,在http://jakarta.apache.org/commons/fileupload/index.html 可以得到下载包和完整的API文档。法奥为adajspException 
处理页面(handle.jsp)的代码如下 
<% 
boolean isMultipart = FileUpload.isMultipartContent(request); 
    if (isMultipart) { 
      // 建立一个新的Upload对象 
      DiskFileUpload upload = new DiskFileUpload(); 
  
    // 设置上载文件的参数 
    //upload.setSizeThreshold(yourMaxMemorySize); 
    //upload.setSizeMax(yourMaxRequestSize); 
    String rootPath = getServletConfig().getServletContext().getRealPath("/") ; 
    upload.setRepositoryPath(rootPath+"\\uploads"); 
  
     // 分析request中的传来的文件流,返回Item的集合, 
     // 轮询Items,如果不是表单域,就是一个文件对象。 
      List items = upload.parseRequest(request); 
      Iterator iter = items.iterator(); 
      while (iter.hasNext()) { 
        FileItem item = (FileItem) iter.next(); 
        //如果是文件对象 
if (!item.isFormField()) { 
  
          //如果是文本文件,可以直接显示 
          //out.println(item.getString()); 
  
          //将上载的文件写到服务器的\WEB-INF\webstart\下,文件名为test.txt 
          //File uploadedFile = new File(rootPath+"\\uploads\\test.txt"); 
          //item.write(uploadedFile); 
  
        //下面的代码是将文件入库(略): 
        //注意输入流的获取 
… 
InputStream uploadedStream = item.getInputStream(); 
… 
        } 
        //否则是普通表单 
else{ 
          out.println("FieldName: " + item.getFieldName()+"<br>"); 
          out.println("Value: "+item.getString()+"<br>");        } 
      } 
    } 
%> 
  
4.  从数据库读取BLOB然后保存到客户端磁盘上 
这段代码有点诡异,执行后将会弹出文件保存对话窗口,将BLOB数据读出保存到本地文件。全文列出如下: 
<%@ page contentType="text/html; charset=GBK" import="java.io.*" import="java.sql.*" import="test.global.ConnectionFactory"%><% 
      Connection con = ConnectionFactory.getConnection(); 
      con.setAutoCommit(false); 
      Statement st = con.createStatement(); 
  
      ResultSet rs = st.executeQuery( 
                     "select contents from  BLOBIMG  where  id=103 "); 
      if (rs.next()) { 
        Blob blob = rs.getBlob(1); 
        InputStream ins = blob.getBinaryStream(); 
  
        response.setContentType("application/unknown"); 
        response.addHeader("Content-Disposition", "attachment; filename="+"output.txt"); 
  
        OutputStream outStream = response.getOutputStream(); 
        byte[] bytes = new byte[1024]; 
        int len = 0; 
        while ((len=ins.read(bytes))!=-1) { 
            outStream.write(bytes,0,len); 
        } 
        ins.close(); 
        outStream.close(); 
        outStream = null; 
        con.commit(); 
        con.close(); 
      } 
%> 
注意,在<% … … %>之外,绝对不能有任何字符,空格或回车都不行,不然会导致outputStream出错,对非ASCII输出文件来说就是格式错误不可读。 
  
5.    
本文参考了大量网友文章,特此表示感谢。 
1.        通过JDBC操纵Oracle数据库LOB字段的几种情况分析http://dev.csdn.net/develop/article/26/26786.shtm  
2.        关于将文件用java.sql.Blob类型的blob操作写入oracle数据库中的blob http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=197116  
3.        weblogic7可以操作Oracle9i的大字段,Weblogic8为什么不可以? http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=123&threadID=5390&messageID=23598  
4.        How to show file download dialog box in IE 6.0 http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20842012.html   
 
  |