java 读取oracle 的BLOB字段读成文件

package kou.inspector.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.risesoft.commons.database.Conn;
import oracle.sql.BLOB;

public class download extends HttpServlet {

 /**
  * 寇新磊
  *
  */
 public download() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doPost(request, response);

 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  String tablename=request.getParameter("tablename");
  String guid=request.getParameter("guid");
  String pkname=request.getParameter("pkname");

        Connection conn=null;
     Statement stem=null;
     ResultSet rs=null;
    try{
        
    //for update 锁定数据行
  String sql="select ACCESSORIES,FILENAME,FILETYPE from "+tablename+" where "+pkname+"='"+guid+"'";
  String type="";
  String filename="";
  conn=Conn.getConnection();
  stem=conn.createStatement();

     rs=stem.executeQuery(sql);
     while(rs.next()){
           //读取blob对象
      BLOB blob=(oracle.sql.BLOB)rs.getBlob("ACCESSORIES");
      filename=rs.getString("FILENAME");
      type=rs.getString("FILETYPE");
      String contenttype="";
      if("doc".equalsIgnoreCase(type)){
      contenttype="application/msword; charset=gbk";
      }
      else if("xls".equalsIgnoreCase(type)){
      contenttype="application/vnd.ms-excel";
      }
      else if("pdf".equalsIgnoreCase(type)){
      contenttype="application/pdf";
      }
      else if("xml".equalsIgnoreCase(type)){
      contenttype="text/xml";
      }
      
      else if("gif".equalsIgnoreCase(type)){
      contenttype="image/gif";
      }
      else if("gpeg".equalsIgnoreCase(type)){
      contenttype="image/jpeg";
      }
      else if("zip".equalsIgnoreCase(type)){
      contenttype="application/zip";
      }
      else if("rat".equalsIgnoreCase(type)){
      contenttype="application/octet-stream";
      }

      else if("rat".equalsIgnoreCase(type)){
      contenttype="text/plain";
      }
      else{
      contenttype="application/s2";
      }
      filename=new String(filename.getBytes("GBK"),"iso-8859-1");
                  response.setContentType(contenttype);     
                  response.setHeader("Content-Disposition", "attachment;filename="+filename);
      InputStream input=blob.getBinaryStream(); 
         OutputStream  fo=response.getOutputStream();

          byte[] b=new byte[1024];
         
          int len=0;
         
          while((len=input.read(b))!=-1){
           fo.write(b, 0, len);
          }
      
      fo.close();
      input.close();
      rs.close();
      stem.close();
      conn.commit();
            
     }    
     
   }
    catch(Exception e){
    }
    finally{try{conn.close();}catch(Exception e){}}


 }

 public void init() throws ServletException {
  // Put your code here
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值