Upload javabean / ** * Oracle upload files to upload BLOB field * @param myUpload class object * @param path upload path * @param serialNo sequence number * @throws Exception * / public void InsertMsg (SmartUpload myUpload, String path, String serialNo) THROWS EXCEPTION {/ * Settings Not Auto Submit * /
Connection conn = getConnection (); Statement stmt = conn.createStatement (); PreparedStatement ps = null; boolean defaultCommit = conn.getAutoCommit (); conn.setAutoCommit (false); String strSQL = ""; try {
com.jspsmart.upload.file myfile = myupload.getfiles (). getfile (0); if (! myfile.ismissing ()) {string filename = myfile.getFileName (); string filesis = myfile.getsize () "" ; / ** * Insert additional information outside of BLOB * / strsql = "INTO T_COMMFILES (SERIALNO, FILENAME, FILSIZE) VALUES ('" serialno ",'" filename ", '" filesize "')"; Ps = conn.preparestatement (strsql); ps.executeUpdate (); ps.close (); ps = null;
String filetype = myfile.getContentType (); string bytelen = String.Valueof (MyFile.Getsize ()); java.io.file folder = new java.io.file (path);
IF (! folder.exists () || folder.isfile ()) Folder.mkdirs (); myfile.saveas (path filename); java.io.file file = new java.io.file (Path filename);
/ ** * Clear blob field * / string resetclob = "Update t_commfiles set files = EMPTY_BLOB () where serialno = '" serialno "" "stmt.executeUpdate (resetClob);
strsql = "SELECT FILES from T_Commfiles Where Serialno = '" Serialno "FOR Update"; ResultSet RS = Stmt.executeQuery (strsql); / ** * Write blob * / while (rs.next ()) {/ this object is removed BLOB * * / oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob ( "files"); / * write data to the BLOB object * / BufferedOutputStream out = new BufferedOutputStream (blob.getBinaryOutputStream ()); Bufferedinputstream in = new bufferedinputstream (new fileinputstream (file)); //blob.putbytes(1 ,b); int C;
While ((c = in.read ())! = -1) {
Out.write (c);} in. close (); out.close ();
}
File.delete ();
/ * Officially submit * / conn.commit (); rs.close (); stmt.close ();
}
} catch (exception ex) {/ * error rollback * / system.out.println (ex.getMessage ()); conn.rollback (); throw ex;} / * Restore original submission status * / conn.setautocommit (DefaultCommit) ); ConnLOAD.jsp read files from blob <% string serialno = request.getParameter ("serialno"); java.sql.connection con = NULL; DBCONNECTION DBCON = New dbconnection ();
try {// database connection con = dbCon.connectByIplanet (); // query database String str = "select files from T_COMMFILES where serialNo = '" serialNo "'"; PreparedStatement pst = con.prepareStatement (str); ResultSet rs = Pst.ExecuteQuery (); while (rs.next ()) {// reads Oracle.Sql.blob files = (Oracle.Sql.blob) gs.getblob ("files");
InputStream in = files.getBinaryStream (); response.setContentType ( "application / x-msdownload"); OutputStream os = response.getOutputStream (); byte [] b = new byte [1024]; int len; while ((len = In.read (b))> 0) Os.Write (B, 0, LEN); // Release Resource Os.close (); in .close ();} rs.close (); con.close (); } catch (exception e) {system.out.println (e);}%>