BlobUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @(#)file BlobUtils.java
  3. * @(#)author [개발자명]
  4. * @(#)version 1.0
  5. * @(#)date 2014-06-24
  6. * @(#)since JDK 1.6
  7. *
  8. * Copyright (c) www.udapsoft.co.kr, Inc.
  9. */
  10. package com.udapsoft.waf.common.util;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.OutputStream;
  14. import java.sql.Blob;
  15. import java.sql.Connection;
  16. import java.sql.ResultSet;
  17. import java.sql.Statement;
  18. import kr.co.hsnc.common.base.WAFLogger;
  19. import kr.co.hsnc.common.logger.Logger;
  20. import weblogic.jdbc.vendor.oracle.OracleThinBlob;
  21. public class BlobUtils {
  22. public BlobUtils() {
  23. super();
  24. }
  25. /**
  26. * 입력받은 (String)strData 를 clob 컬럽에 등록시킨다.
  27. * @param conn
  28. * @param tableName 테이블명
  29. * @param columnName 컬럼명
  30. * @param keyColumnName 키컬럼명
  31. * @param keyColumnValue 키컬럼값
  32. * @param file 등록할 파일
  33. * @throws Exception
  34. */
  35. public static void setBlob(Connection conn, String tableName, String columnName, String keyColumnName, String keyColumnValue, File file) throws Exception{
  36. Statement stmt = null;
  37. ResultSet rs = null;
  38. FileInputStream fis = null;
  39. OutputStream os = null;
  40. String up_sql = "UPDATE " + tableName
  41. + " SET " + columnName + " = empty_blob() "
  42. + " WHERE " + keyColumnName + " = " + keyColumnValue;
  43. String sel_sql = " SELECT " + columnName
  44. + " FROM " + tableName
  45. + " WHERE " +keyColumnName + " = " + keyColumnValue + " FOR UPDATE ";
  46. try {
  47. stmt = conn.createStatement();
  48. stmt.executeUpdate(up_sql);
  49. rs = stmt.executeQuery(sel_sql);
  50. if( rs.next() ) {
  51. Blob blob = rs.getBlob(1);
  52. fis = new FileInputStream(file);
  53. byte[] temp = new byte[(int)file.length()];
  54. fis.read(temp);
  55. os = blob.setBinaryStream(0);
  56. os.write(temp);
  57. }
  58. }
  59. catch(Exception e) {
  60. Logger.err.printStackTrace(e);
  61. }
  62. finally {
  63. if( rs != null ) rs.close();
  64. if( stmt != null ) stmt.close();
  65. if( fis != null ) fis.close();
  66. if( os != null ) os.close();
  67. }
  68. }
  69. /**
  70. * 입력받은 File을 BLOB Column에 등록시킨다.
  71. * @param conn
  72. * @param tableName 테이블명
  73. * @param columnName 컬럼명
  74. * @param keyColumnName 키컬럼명
  75. * @param keyColumnValue 키컬럼값
  76. * @param file 등록할 파일
  77. * @throws Exception
  78. */
  79. public static void setBlob2(Connection conn, String tableName, String columnName, String whereSql, File file) throws Exception{
  80. Statement stmt = null;
  81. Statement stmt1 = null;
  82. ResultSet rs = null;
  83. FileInputStream fis = null;
  84. OutputStream os = null;
  85. boolean isLocalTrx = false;
  86. String up_sql = "UPDATE " + tableName
  87. + " SET " + columnName + " = empty_blob() "
  88. + " WHERE " + whereSql;
  89. String sel_sql = " SELECT " + columnName
  90. + " FROM " + tableName
  91. + " WHERE " + whereSql + " FOR UPDATE ";
  92. try {
  93. if( conn.getAutoCommit() == true )
  94. isLocalTrx = true;
  95. if( isLocalTrx )
  96. conn.setAutoCommit(false);
  97. stmt = conn.createStatement();
  98. stmt.executeUpdate(up_sql);
  99. stmt1 = conn.createStatement();
  100. rs = stmt1.executeQuery(sel_sql);
  101. if( rs.next() ) {
  102. /*Oracle 10g이하 */
  103. OracleThinBlob blob = (OracleThinBlob)rs.getBlob(1);
  104. long fileLength = (long)file.length();
  105. fis = new FileInputStream(file);
  106. os = blob.getBinaryOutputStream();
  107. int size = blob.getBufferSize();
  108. byte[] buffer = new byte[size];
  109. int length = -1;
  110. while ((length = fis.read(buffer)) != -1) {
  111. os.write(buffer, 0, length);
  112. }
  113. /*Oracle 10g이상 */
  114. // Blob blob = (Blob)rs.getBlob(1);
  115. // long fileLength = (long)file.length();
  116. // fis = new FileInputStream(file);
  117. // os = blob.setBinaryStream(0);
  118. // int size = (int)blob.length();
  119. // byte[] buffer = new byte[size];
  120. // int length = -1;
  121. // while ((length = fis.read(buffer)) != -1) {
  122. // os.write(buffer, 0, length);
  123. // }
  124. }
  125. }
  126. catch(Exception e) {
  127. Logger.err.printStackTrace(e);
  128. WAFLogger.debug(e.getLocalizedMessage());
  129. WAFLogger.debug(e.getMessage());
  130. WAFLogger.debug(e.getStackTrace());
  131. if( isLocalTrx )
  132. conn.rollback();
  133. throw e;
  134. }
  135. finally {
  136. if( rs != null ) rs.close();
  137. if( stmt != null ) stmt.close();
  138. if( stmt1 != null ) stmt1.close();
  139. if( fis != null ) fis.close();
  140. if( os != null ) os.close();
  141. if( isLocalTrx ) {
  142. conn.commit();
  143. conn.setAutoCommit(true);
  144. }
  145. }
  146. }
  147. }