JDBC上传文件存入BLOB字段

   在将文件转成的二进制/base64形式的的会报数据太长,那么这个时候就要先插入一个空的blob值(这里是oracle),代码如下
	public class fillUtil{
	
		Connection connDoc = null;
   		PreparedStatement stmtDoc = null;
  		ResultSet res = null;
  		public int saveFill(InputStream is) {
  			byte[] value = null;
  			//利用Driver对象
  			Driver driver = new OracleDriver();
            DriverManager.deregisterDriver(driver);
            
			Properties pro = new Properties();
            pro.put("user", "root");
            pro.put("password", "root");
            connDoc = driver.connect("jdbc:oracle:thin:@localhost:1521:XE", pro);
  		}
		try {
		   connDoc.setAutoCommit(false); //关闭自动提交
		   String sql3 = "insert into files (code,content values(?,?)";//插入语句
            stmtDoc = connDoc.prepareStatement(sql3);
            stmtDoc.setString(1, 1001);
            stmtDoc.setBlob(2, oracle.sql.BLOB.getEmptyBLOB());
			
			stmtDoc.executeUpdate();

			String sql4 = "select content from files where code = ?";
            stmtDoc = connDoc.prepareStatement(sql4);
            stmtDoc.setString(1, requestId);
            res = stmtDoc.executeQuery();
            res.next();
            BLOB contentBlob = (BLOB) res.getBlob(1);   //得到该空的blob
            OutputStream outputstream = contentBlob.getBinaryOutputStream();
            outputstream.write(value);
			
			outputstream.flush();
            outputstream.close();
            connDoc.commit();
            }catch (Exception e) {
            try {
                if (connDoc != null)
                    connDoc.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return 0;

        } finally {
            try {
                res.close();
                stmtDoc.close();
                connDoc.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	
	//转换成二进制
    public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buff = new byte[4096];
        int index = 0;
        while ((index = inputStream.read(buff, 0, 4096)) > 0) {
            baos.write(buff, 0, index);
        }
        return baos.toByteArray();
    }
	
	//转换Base64
	public static String byte2Base64StringFun(byte[] b) {
        String str = "";
        try {
            BASE64Encoder encoder = new BASE64Encoder();
            str = Util.null2String(encoder.encode(b)).replaceAll("[\\s*\t\n\r]", "");
        } catch (Exception e) {
            log.writeLog("byte[]转base64报错,错误信息---->" + e.toString());
        }
        return str;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值