Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. DROP JAVA SOURCE BALT_CHECK."WebDocs";
  2.  
  3. CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BALT_CHECK."WebDocs" as import java.io.*;
  4. import java.sql.*;
  5. import java.util.Date;
  6. import java.text.SimpleDateFormat;
  7. import java.lang.String;
  8.  
  9. public class WebDocs
  10. {
  11. public static long fileID;
  12. public static void GetDocs(String rootdirectory) throws SQLException
  13. {
  14. walkin(rootdirectory);
  15. }
  16.  
  17. public static void walkin(String rootdirectory) throws SQLException
  18. {
  19. File path = new File( rootdirectory );
  20. String[] DirList = path.list();
  21.  
  22. for( int x = 0; x < DirList.length; x++)
  23. {
  24. String newPath = rootdirectory + DirList[x];
  25. if (newPath != null) {
  26. File f = new File(newPath);
  27. if (f.isDirectory()) {
  28.  
  29. String dirName = DirList[x];
  30. GetDocs(newPath +"/");
  31. }
  32. if (f.isFile()){
  33. WriteFile(f);
  34. }else{
  35. }
  36. }
  37. }
  38. }
  39.  
  40. public static void WriteFile(File file) throws SQLException
  41. {
  42. String fileName;
  43. String filePath;
  44. String elementID;
  45. long len;
  46. Date date;
  47. String sqlDate;
  48. SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
  49.  
  50. fileID = fileID + 1;
  51.  
  52. elementID = String.valueOf(fileID);
  53. fileName = file.getName();
  54. filePath = file.getPath();
  55. len = file.length();
  56. date = new Date(file.lastModified());
  57. sqlDate = df.format(date);
  58.  
  59.  
  60. #sql { INSERT INTO WEB_DIRLIST (ID, FILE_NAME, FILE_SIZE, CREATE_DATE, FILE_PATH)
  61. VALUES (:elementID, :fileName, :len, to_date(:sqlDate, 'YYYY-MM-DD HH24:MI:SS'), :filePath) };
  62.  
  63. }
  64.  
  65. }
  66.  
  67. /
  68.  
  69. CREATE OR REPLACE PROCEDURE BALT_CHECK.insert_doc as
  70. f_lob BFILE;
  71. b_lob BLOB;
  72. BEGIN
  73. INSERT INTO WEB_DOCS VALUES('2',EMPTY_BLOB(),'PDF','Acctg001-travelexpensereport.pdf')
  74. RETURN blob_col INTO b_lob;
  75.  
  76. f_lob := BFILENAME('ACCTG','Acctg001-travelexpensereport.pdf');
  77. dbms_lob.fileopen(f_lob, dbms_lob.file_readonly);
  78. dbms_lob.loadfromfile( b_lob, f_lob, dbms_lob.getlength(f_lob));
  79. dbms_lob.fileclose(f_lob);
  80. COMMIT;
  81. END;
  82. /
  83.  
  84. /**
  85. * Method used to write binary data contained in a file to an Oracle BLOB
  86. * column. The method used to write the data to the BLOB uses the putBytes()
  87. * method. This is one of two types of methods used to write binary data to
  88. * a BLOB column. The other method uses Streams.
  89. *
  90. * @throws java.io.IOException
  91. * @throws java.sql.SQLException
  92. */
  93. public void writeBLOBPut()
  94. throws IOException, SQLException {
  95.  
  96. FileInputStream inputFileInputStream = null;
  97. String sqlText = null;
  98. Statement stmt = null;
  99. ResultSet rset = null;
  100. BLOB image = null;
  101. int chunkSize;
  102. byte[] binaryBuffer;
  103. long position;
  104. int bytesRead = 0;
  105. int bytesWritten = 0;
  106. int totbytesRead = 0;
  107. int totbytesWritten = 0;
  108.  
  109. try {
  110.  
  111. stmt = conn.createStatement();
  112.  
  113. inputBinaryFile = new File(inputBinaryFileName);
  114. inputFileInputStream = new FileInputStream(inputBinaryFile);
  115.  
  116. sqlText =
  117. "INSERT INTO test_blob (id, image_name, image, timestamp) " +
  118. " VALUES(1, '" + inputBinaryFile.getName() + "', EMPTY_BLOB(), SYSDATE)";
  119. stmt.executeUpdate(sqlText);
  120.  
  121. sqlText =
  122. "SELECT image " +
  123. "FROM test_blob " +
  124. "WHERE id = 1 " +
  125. "FOR UPDATE";
  126. rset = stmt.executeQuery(sqlText);
  127. rset.next();
  128. image = ((OracleResultSet) rset).getBLOB("image");
  129.  
  130. chunkSize = image.getChunkSize();
  131. binaryBuffer = new byte[chunkSize];
  132.  
  133. position = 1;
  134. while ((bytesRead = inputFileInputStream.read(binaryBuffer)) != -1) {
  135. bytesWritten = image.putBytes(position, binaryBuffer, bytesRead);
  136. position += bytesRead;
  137. totbytesRead += bytesRead;
  138. totbytesWritten += bytesWritten;
  139. }
  140.  
  141. inputFileInputStream.close();
  142.  
  143. conn.commit();
  144. rset.close();
  145. stmt.close();
  146.  
  147. System.out.println(
  148. "==========================================================n" +
  149. " PUT METHODn" +
  150. "==========================================================n" +
  151. "Wrote file " + inputBinaryFile.getName() + " to BLOB column.n" +
  152. totbytesRead + " bytes read.n" +
  153. totbytesWritten + " bytes written.n"
  154. );
  155.  
  156. } catch (IOException e) {
  157. System.out.println("Caught I/O Exception: (Write BLOB value - Put Method).");
  158. e.printStackTrace();
  159. throw e;
  160. } catch (SQLException e) {
  161. System.out.println("Caught SQL Exception: (Write BLOB value - Put Method).");
  162. System.out.println("SQL:n" + sqlText);
  163. e.printStackTrace();
  164. throw e;
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement