How to overload files to the BLOB field of the Oracle database with an OCI protocol through Struts

zhaozj2021-02-16  56

The Class file is as follows:

Import java.io. *; import java.sql. *; import java.util.date; import oracle.sql. *; import javax.servlet.http. *; import org.apache.struts.Action. *; import ORG .apache.struts.upload. *;

public class UploadAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {if (form instanceof UploadForm) {Date date = new Date (); System.out.println ( "--- ---------- File Upload Begins ----------------- "); UPLOADFORM Theform = (UPLOADFORM) Form; File: // Through Struts FormFile class to get uploaded files, code File: // formfile file = theform.getthefile (); connection conn = null; preparedState ps = null;

String union_id = ""; string union_version = ""; union_id = theform.getunion_id (); union_version = theform.getunion_version (); file: // ActionPart is the field name in the database table, because there are multiple blob fields in the table, So use variables to represent its name String ActionPart = theform.getActionPart (); System.out.Println ("ActionPart IS:" ActionPart); File: // Get Database Connection, DBPOOL Source Code Attach DBPOOL DBP = New DBPOOL (); conn = dbp.getConnection ();

ResultSet blobaldetails = null; statement stmt = null;

try {InputStream stream = null; FileInputStream fstream = null; stream = file.getInputStream (); System.out.println ( "------------ uploadFileSize is:" stream.available () "------------");

Conn.setautocommit (false); stmt = conn.createstatement (); file: // first use EMPTY_BLOB () to initialize this field SQL = "Update regunion set" ActionPart "= EMPTY_BLOB () Where union_id =" union_id "and union_Version =" union_Version; stmt.executeUpdate (sql); conn.commit (); stmt.close (); stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); sql = "select" actionpart "from regunion where union_id =" union_Id "and union_Version =" union_Version "for update"; BlobDetails = stmt.executeQuery (sql); if (BlobDetails.next ()) {BLOB l_mapBlob = (BLOB) BlobDetails.getBlob (1); OUTPUTSTREAM L_BLOBOUTPUTSTREAM = ((blob) l_mapblob) .GetbinaryoutputStream ();

Byte [] l_buffer = new byte [10 * 1024];

INT L_NREAD = 0; While ((l_nread = stream.read (l_buffer))! = -1) // read from file {l_bloboutputstream.write (l_buffer, 0, l_nread); // Write to blob}

stream.close (); l_blobOutputStream.close ();} BlobDetails.close (); conn.commit ();} catch (FileNotFoundException fnfe) {fnfe.printStackTrace (); return mapping.findForward ( "error");} catch (IOE.PRINTSTACKTRACE (); return mapping.findforward ("error") ;;} catch (sqlexception ex) {EX.PrintStackTrace (); return mapping.findforward ("error") ;;} finally { (conn! = null) {Try {stmt.close (); conn.close ();} catch (Exception SQLE) {SQLE.PRINTSTACKTRACE ();}}} Request.setattribute ("Union_ID", Union_ID); Request. SetAttribute ("Union_Version", Union_Version; System.out.Println ("----------- File Upload Ends -------------------------------------------------------------------------------------------------------------- - "); return mapping.findforward (" success ");} else {return null;}}}

Attached: DBPOOL.JAVA program

package com.prient.nbsc.unifylaw; import java.sql *;. import com.prient.nbsc.common.DBPOOL; public class dbPool {public Connection getConnection () {try {Connection conn = null; Class.forName ( "oracle .jdbc.driver.racledriver "). Newinstance (); string bridge =" JDBC: Oracle: Thin: @ 10.6.89.2: 1521: sjk "; conn = drivermanager.getConnection (Bridge," User "," User "); Return conn;} catch (exception e) {system.out.println (e); return null;}}}

转载请注明原文地址:https://www.9cbs.com/read-24313.html

New Post(0)