1 package transactions; 2 3 import dinamica.*; 4 import java.io.*; 5 import java.sql.*; 6 7 14 public class SaveImageOracle extends GenericTransaction 15 { 16 17 20 public int service(Recordset inputParams) throws Throwable 21 { 22 int rc = super.service(inputParams); 23 24 String path = (String )inputParams.getValue("file"); 26 File f = new File(path); 27 Integer size = new Integer ((int)f.length()); 28 inputParams.setValue("image_size", size); 29 30 String fileName = (String )inputParams.getValue("file.filename"); 32 fileName = fileName.substring(fileName.lastIndexOf(File.separator)+1); 33 inputParams.setValue("file.filename", fileName); 34 35 String sql = getResource("query.sql"); 37 sql = getSQL(sql, inputParams); 38 39 40 41 Db db = getDb(); 43 db.exec(sql); 44 45 Recordset rsId = db.get("select seq_image.currval id from dual"); 47 rsId.next(); 48 String id = rsId.getString("id"); 49 50 Class.forName("oracle.sql.BLOB"); 52 sql = getResource("query-blob.sql"); 53 sql = StringUtil.replace(sql, "${id}", id); 54 55 Connection conn = this.getConnection(); 56 Statement s = null; 57 ResultSet rs = null; 58 BufferedOutputStream out = null; 59 BufferedInputStream inp = null; 60 61 try { 62 s = conn.createStatement(); 63 rs = s.executeQuery(sql); 64 while (rs.next()) 65 { 66 oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("image_data"); 68 69 out = new BufferedOutputStream(blob.getBinaryOutputStream()); 71 byte buffer[] = new byte[8192]; 72 int c = 0; 73 inp = new BufferedInputStream( new FileInputStream(f) ); 74 while (c >= 0) 75 { 76 c = inp.read(buffer); 77 if (c>0) 78 out.write(buffer, 0, c); 79 } 80 } 81 } 82 catch (Throwable e) 83 { 84 throw e; 85 } 86 finally 87 { 88 if (inp!=null) inp.close(); 89 if (out!=null) out.close(); 90 if (rs!=null) rs.close(); 91 if (s!=null) s.close(); 92 } 93 94 95 96 return rc; 97 98 } 99 100 } 101 | Popular Tags |