Java将zip存入oralce的blob

72 篇文章 0 订阅

加入4个jar包(按照一下顺序加入):

ojdbc14.jar

com.bea.core.datasource6.binding_1.4.0.0.jar

com.bea.core.datasource6_1.4.0.0.jar

com.bea.core.datasource_1.4.0.0.jar

 

//后三个包可以用weblogic内的bea\weblogic81\server\lib下的weblogic.jar

但是在8启动时服务稍慢,11g时会快!网上说是因为weblogic区分jar浪费时间

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

public class blob1 {

 /**
  * @param args
  */
 public static boolean insertFile(String table, String cpcode, String sbym,
  String fileName, String file_) {
  String strSql = "";
  Connection conn = null;
  Statement pstmt = null;
  FileInputStream fs = null;
  long flength = 0;
  File file = new File(file_);
  if (file.exists()) {
   try {
    fs = new FileInputStream(file_);
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   flength = file.length();
  } else {
   return false;
  }
  try {
   
    conn = DBConnection.getConnection();
    if(table.equals("zsfkinfo")){
    strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,note,maildate) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R','123',sysdate)";
    }else if(table.equals("fkinfo")){
     strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,maildate,fk_code) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R',sysdate,'123')";
    }else if(table.equals("dzfkinfo")){
     strSql = "insert into "
      + table
      + " (id,sb_id,cpcode,sb_ym,cpemail,subject,body,filename,attachment,FK_FLAG,flag,maildate,fk_code) values("+table+"_ID_SEQ.NEXTVAL,"+table+"_ID_SEQ.NEXTVAL,"+cpcode+","+sbym+",'123','123','123','"+fileName+"',empty_blob(),'R','R',sysdate,'123')";
    }
   conn.setAutoCommit(false);
   pstmt = conn.createStatement();
   pstmt.executeUpdate(strSql);
   String strSql2 =null;
   
    strSql2 = "SELECT attachment FROM "+table+" WHERE id=(select max(id) from "+table+") and cpcode="+cpcode+" and sb_ym="+sbym+" FOR UPDATE";
  
   ResultSet rs = pstmt.executeQuery(strSql2);
   while (rs.next()) {
    System.out.println( rs.getBlob(1).getClass());
    /* 取出此BLOB对象 */
    weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob(1);
             //os.write(arrayByte, iStartData, flength);
    OutputStream outStream = blob.getBinaryOutputStream();
    byte[] b = new byte[blob.getBufferSize()];

            int len = 0;

            try {
       while ( (len = fs.read(b)) != -1) {

         outStream.write(b, 0, len);

       }
       fs.close();

       outStream.flush();

       outStream.close();

       conn.commit();

      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

   }   
   try {
    fs.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return true;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    if (pstmt != null) {
     pstmt.close();
    }
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return false;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //blob1 b = new blob1();
  
  //blob1.insertFile("zsfkinfo","2102210097","201203","2102210097_wm20120328.zip","C:/Documents and Settings/Administrator/桌面/fk/2102210097_wm20120328.zip");
  blob1.insertFile("fkinfo","2102230429","201203","ysfk_xgm_2102230429.zip","C:/Documents and Settings/Administrator/桌面/fk/ysfk_xgm_2102230429.zip");
  blob1.insertFile("dzfkinfo","2102230429","201203","fk_xgm.zip","C:/Documents and Settings/Administrator/桌面/fk/fk_xgm.zip");

 


  
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值