java讀寫oracle的blob欄位樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

package com.wanmei.meishu;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import oracle.sql.BLOB;

public class BlobUtil {

 private static BlobUtil bu;
 private String env;
 public static BlobUtil getInstance(String env) {
  bu = new BlobUtil();
  bu.env = env;
  return bu;
 }

 /**
  * <p>得到資料庫連結</p>
  * @return
  * @throws Exception
  */
 private Connection getConnection() throws Exception {
  String driver = "oracle.jdbc.driver.OracleDriver";
  Class.forName(driver);
  String env = this.env;
  Properties pro = new Properties();
  // 讀取classes 目錄下的設定檔  
  pro.load(new FileReader(Class.class.getResource("/config.properties").getFile()));
  String host = pro.getProperty(env + ".host");
  String database = pro.getProperty(env + ".database");
  String username = pro.getProperty(env + ".username");
  String password = pro.getProperty(env + ".password");
  String port = pro.getProperty(env + ".port");
  String url = "jdbc:oracle:thin:@" + host + ":" + port +":" + database;
  return DriverManager.getConnection(url, username, password);
 }

 /**
  * <p>傳入項目,任務,附件名稱,檔案路徑,寫入資料庫</p>
  * @param projectId
  * @param taskId
  * @param fileName
  * @param file
  * @return 返回是否成功
  */
 public boolean write(String projectId, String taskId, String fileName, String file) {
  Connection conn = null;
  ResultSet rs = null;
  PreparedStatement ps = null;
  BLOB blob = null;
  boolean flag = false;
  try {
   conn = getConnection();
   conn.setAutoCommit(false);

   String sql = "INSERT INTO PS_ZP_PRJ_WBS_BLOB BLB(ZP_PRJ_ID, ZZ_SEQ_NUM, ZZ_FILE_NAME, ZZ_IMAGE_BLOB) VALUES(?, ? ,? ,empty_blob())";
   ps = conn.prepareStatement(sql);
   ps.setString(1, projectId);
   ps.setString(2, taskId);
   ps.setString(3, fileName);
   ps.executeUpdate();
   sql = "SELECT ZZ_IMAGE_BLOB FROM PS_ZP_PRJ_WBS_BLOB WHERE ZP_PRJ_ID = ? AND ZZ_SEQ_NUM = ? AND ZZ_FILE_NAME = ? FOR UPDATE";
   ps = conn.prepareStatement(sql);
   ps.setString(1, projectId);
   ps.setString(2, taskId);
   ps.setString(3, fileName);
   rs = ps.executeQuery();
   if(rs.next()) {
    blob = (BLOB) rs.getBlob(1);
   }
   InputStream in = new FileInputStream(file);
   OutputStream out = blob.setBinaryStream(1L);
   byte[] buffer = new byte[1024];
   int length = -1;
            while ((length = in.read(buffer)) != -1){
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
            conn.commit();
            conn.setAutoCommit(true);
            flag = true; 
  }
  catch(Exception e) {
   if(conn != null) {
    try {
     conn.rollback();
    } catch (SQLException e1) {
     e1.printStackTrace();
    }
   }
  }
  finally {
   try {
    rs.close();
    ps.close();
    conn.close();
   }
   catch(Exception e) {
    e.printStackTrace();
   }
  }
  return flag;
 }

 /**
  * <p>根據項目Id,任務Id,檔案名稱讀取資料庫blob欄位檔案,寫入指定的檔案路徑</p>
  * @param projectId
  * @param taskId
  * @param fileName
  * @param file
  * @return 返回是否成功
  */
 public boolean read(String projectId, String taskId, String fileName, String file) {
  Connection conn = null;
  ResultSet rs = null;
  PreparedStatement ps = null;
  BLOB blob = null;
  boolean flag = false;
  try {
   conn = getConnection();
   String sql = "SELECT ZZ_IMAGE_BLOB FROM PS_ZP_PRJ_WBS_BLOB WHERE ZP_PRJ_ID = ? AND ZZ_SEQ_NUM = ? AND ZZ_FILE_NAME = ?";
   ps = conn.prepareStatement(sql);
   ps.setString(1, projectId);
   ps.setString(2, taskId);
   ps.setString(3, fileName);
   rs = ps.executeQuery();
   if(rs.next()) {
    blob = (BLOB) rs.getBlob(1);
   }
   InputStream in = blob.getBinaryStream();
   byte[] buf = new byte[1024];
   int bytesIn = 0;
   FileOutputStream out = new FileOutputStream(file);
   while ((bytesIn = in.read(buf, 0, 1024)) != -1) {
    out.write(buf, 0, bytesIn);
   }
            in.close();
            out.close();
            flag = true;
  }
  catch(Exception e) {
   e.printStackTrace();
  }
  finally {
   try {
    rs.close();
    ps.close();
    conn.close();
   }
   catch(Exception e) {
    e.printStackTrace();
   }
  }
  return flag;
 }

 public static void main(String[] args) {
  BlobUtil bu = BlobUtil.getInstance("MSDEV");
  System.out.println(bu.write("CB", "001", "image1", "D:\\61e44b02jw1dw4xbp2zo6j.jpg"));
  System.out.println(bu.read("CB", "001", "image1", "D:\\2.jpg"));
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.