Students apply for Subject contest activities, the form has a subject contest declaration information and part of the attachment, you need to upload files and form data submitted to maintain consistency.
Put the upload file and insert form data into the transaction summary to deal with, because the form of data we can control, but the uploaded document is not good control. On the one hand, I put the form data submitted in front of the file upload, so that when the form data submission exception we can roll back, the file is not uploaded, when the file upload exception, we can roll back the database data, only when the file is properly uploaded we submit database data ...
--------------------------------------------------------------------------------------------------------------- ------------------------
1 @Override2 Public Booleanapplactivity (Xkteam team, Document Doc, DocType DocType,3 fileinputstream Fin) {4 5 BooleanFlag =true;6FileOutputStream fos =NULL;//Document output Stream7 //------------Process documents, document types, and reporting information that needs to be submitted to the database--------------------8 //turn on transaction, set not auto commit9 jdbctools.starttransaction ();Ten One Try { A //determine whether a personal declaration or a group declaration calls a different method - //Personal Declaration - if(Team.getapptype () = = 0) { the -Flag =Idao.addpersonapp (team); - //Update current reporting number -Flag = Idao.updatecurrentnum (Team.getapptype (), 1); + } - + //Group Declaration A if(Team.getapptype () = = 1) { at - -Flag =idao.addteaminfor (team); -Flag =Idao.addteammember (Team.getmemberlist ()); - //Update current reporting number -Flag =Idao.updatecurrentnum (Team.getactiid (), Team.getmemberlist (). Size ()); in } - to if(Doc! =NULL&& DOCTYPE! =NULL){//process whether a user uploads a document + //method of invoking the DAO layer to insert a file -Flag =Idao.adddocument (DOC); the //invoke the DAO layer Insert File type method *Flag =Idao.adddoctype (DOCTYPE); $ Panax Notoginseng //---------------Handle uploading documents------------ - //1. Create an Upload Document file theFile File =NewFile (Doc.getdoc_path ()); + //2. Create a write Document Object AFOS =Newfileoutputstream (file); the //3. Create a buffered stream + byte[] buffer =New byte[1024]; - intLen = 0; $ while(len = fin.read (buffer)) > 0) { $ //Write File -Fos.write (buffer, 0, Len); - } the } - //SubmitWuyi jdbctools.commit (); the}Catch(Exception E2) { - e2.printstacktrace (); Wu //An exception has been rolled back - Jdbctools.rollback (); About Throw NewRuntimeException ("Reporting failed, please contact the Administrator!"), E2); $}finally { - //Close Resource - jdbctools.setcommittrue (); - jdbctools.close (); A + if(Fos! =NULL){ the Try { - fos.close (); $}Catch(IOException e) { the e.printstacktrace (); the } the } the } - in returnFlagView Code
How does uploading files and form data submissions in Java maintain data consistency?