How does uploading files and form data submissions in Java keep the data consistent?

Source: Internet
Author: User

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         returnFlag
View Code

How does uploading files and form data submissions in Java maintain data consistency?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.