1 package dinamica; 2 3 import java.io.BufferedInputStream ; 4 import java.io.IOException ; 5 import java.sql.*; 6 7 import javax.servlet.ServletOutputStream ; 8 import javax.sql.DataSource ; 9 10 21 public class BlobOutput extends GenericOutput 22 { 23 24 27 public void print(GenericTransaction data) throws Throwable 28 { 29 30 final int BUFFER_SIZE = 8192; 31 32 String jndiPrefix = getContext().getInitParameter("jndi-prefix"); 34 String dataSourceName = getContext().getInitParameter("def-datasource"); 35 36 37 if (getConfig().transDataSource!=null) 38 dataSourceName = getConfig().transDataSource; 39 40 if (jndiPrefix==null) 41 jndiPrefix=""; 42 43 DataSource ds = Jndi.getDataSource(jndiPrefix + dataSourceName); 44 45 Connection conn = null; 46 Statement s = null; 47 ResultSet rs = null; 48 49 BufferedInputStream buf = null; 50 ServletOutputStream out = null; 51 52 try 53 { 54 conn = ds.getConnection(); 56 s = conn.createStatement(); 57 58 Recordset info = data.getRecordset("blobinfo"); 60 61 String sql = info.getString("sql"); 63 64 getResponse().setContentType(info.getString("format")); 66 67 String fileName = info.getString("filename"); 69 if (fileName!=null) 70 { 71 getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";"); 72 } 73 74 out = getResponse().getOutputStream(); 76 77 rs = s.executeQuery(sql); 79 if (rs.next()) 80 { 81 Blob blob = rs.getBlob(1); 83 84 int size = (int)blob.length(); 86 getResponse().setContentLength(size); 87 88 int bytes = 0; 89 byte buffer[] = new byte[BUFFER_SIZE]; 90 buf = new BufferedInputStream ( blob.getBinaryStream() ); 91 92 while( bytes != -1 ) 93 { 94 bytes = buf.read(buffer); 95 if (bytes>0) 96 out.write(buffer,0,bytes); 97 } 98 } 99 } 100 catch (Throwable e) 101 { 102 throw e; 103 } 104 finally 105 { 106 107 try 108 { 109 if (buf!=null) buf.close(); 110 } 111 catch (IOException e1) 112 { 113 e1.printStackTrace(); 114 } 115 116 try 117 { 118 if (rs!=null) rs.close(); 119 } 120 catch (SQLException e2) 121 { 122 e2.printStackTrace(); 123 } 124 125 try 126 { 127 if (s!=null) s.close(); 128 } 129 catch (SQLException e3) 130 { 131 e3.printStackTrace(); 132 } 133 134 try 135 { 136 if (conn!=null) conn.close(); 137 } 138 catch (SQLException e4) 139 { 140 e4.printStackTrace(); 141 } 142 143 try 144 { 145 if (out!=null) 146 { 147 out.close(); 148 } 149 } 150 catch (IOException e5) 151 { 152 e5.printStackTrace(); 153 } 154 155 } 156 157 } 158 159 } 160 | Popular Tags |