1 package dinamica; 2 3 import java.sql.*; 4 import javax.servlet.ServletOutputStream ; 5 import javax.sql.DataSource ; 6 7 21 public class BlobOutputPGSQL extends GenericOutput 22 { 23 24 27 public void print(GenericTransaction data) throws Throwable 28 { 29 30 String jndiPrefix = getContext().getInitParameter("jndi-prefix"); 32 String dataSourceName = getContext().getInitParameter("def-datasource"); 33 34 35 if (getConfig().transDataSource!=null) 36 dataSourceName = getConfig().transDataSource; 37 38 if (jndiPrefix==null) 39 jndiPrefix=""; 40 41 DataSource ds = Jndi.getDataSource(jndiPrefix + dataSourceName); 42 43 Connection conn = null; 44 Statement s = null; 45 ResultSet rs = null; 46 47 ServletOutputStream out = null; 48 49 try 50 { 51 conn = ds.getConnection(); 53 s = conn.createStatement(); 54 55 Recordset info = data.getRecordset("blobinfo"); 57 58 String sql = info.getString("sql"); 60 61 getResponse().setContentType(info.getString("format")); 63 64 String fileName = info.getString("filename"); 66 if (fileName!=null) 67 { 68 getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";"); 69 } 70 71 out = getResponse().getOutputStream(); 73 74 rs = s.executeQuery(sql); 76 if (rs.next()) 77 { 78 byte[] blob = rs.getBytes(1); 80 81 int size = (int)blob.length; 83 getResponse().setContentLength(size); 84 out.write(blob); 85 } 86 87 } 88 catch (Throwable e) 89 { 90 throw e; 91 } 92 finally 93 { 94 if (rs!=null) rs.close(); 95 if (s!=null) s.close(); 96 if (conn!=null) conn.close(); 97 if (out!=null) out.close(); 98 } 99 100 } 101 102 } 103 | Popular Tags |