Created
September 26, 2014 09:46
-
-
Save JRGGRoberto/120146a196d0d627b2cc to your computer and use it in GitHub Desktop.
Oracle - column CLOB Base 64 to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package extractclob; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.Reader; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import net.iharder.Base64; | |
/** | |
* @author rgomes | |
*/ | |
public class Extractclob { | |
static String horas(int a) { | |
String formado; | |
if (a == 1){ | |
formado = "HH:mm:ss"; | |
} else { | |
formado = "dd/MM/yyyy HH:mm:ss"; | |
} | |
DateFormat dateFormat = new SimpleDateFormat(formado); | |
Date date = new Date(); | |
return dateFormat.format(date); | |
} | |
static void extractfiles(){ | |
try{ | |
Class.forName("oracle.jdbc.driver.OracleDriver"); | |
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:apps/apps@10.50.100.70:1611:TEST"); | |
String sql = "SELECT attach_filename, attach_content_bytes FROM CC_CEM_ATTACHMENTS where attach_filename <> 'Thumbnail' and attach_id in (83,103)"; | |
PreparedStatement stmt = conn.prepareStatement(sql); | |
ResultSet resultSet = stmt.executeQuery(); | |
while (resultSet.next()) { | |
String file_name = resultSet.getString(1); | |
File data = new File("C:\\kbfiles\\attach\\"+file_name); | |
Reader reader = resultSet.getCharacterStream(2); | |
FileWriter writer = new FileWriter(data); | |
char[] buffer = new char[1]; | |
String a = null; | |
while (reader.read(buffer) > 0) { | |
writer.write(buffer); | |
} | |
writer.close(); | |
Base64.decodeFileToFile("C:\\kbfiles\\attach\\"+file_name, "C:\\kbfiles\\attach\\"+file_name); | |
} | |
conn.close(); | |
}catch (Exception e) { | |
System.err.println("Exception: "+e.getMessage()); | |
System.err.println(); | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
extractfiles(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment