打印本文 打印本文 关闭窗口 关闭窗口
在Oracle中存取BLOB对象实现文件的上传和下载
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2798  更新时间:2009/4/22 22:04:22  文章录入:mintao  责任编辑:mintao
   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输出文件来说就是格式错误不可读。

上一页  [1] [2] [3] [4] 

打印本文 打印本文 关闭窗口 关闭窗口