1. Create a new JSP table to multipart
<form id= "Form2" method= "post" enctype= "multipart/form-data" action= "<%=basePath%> Uploadftp.shtml "> <input type=" file "name=" file "> <input type=" Submit "value=" ftp Commit "> </form>
2.Controller Code ftpserver judgment and then username judgment.
If there is no ftpserver add add Mapper in the set primary key to add
Usegeneratedkeys= "true" and the primary key ID correspond to Conroller below the red code to get the primary key
<insert id= "Addftpserver" usegeneratedkeys= "true" parametertype= "Ftpserverbean" keyproperty= "id" >
@Controller @requestmapping ("/") Public classUploadftpcontroller {@AutowiredPrivateFtpservice Ftpservice; @AutowiredPrivateFtpserverservice Ftpserverservice; @AutowiredPrivateCustomerService CustomerService; @RequestMapping ("Uploadftppage") PublicString uploadcustomerpage () {return"Help/uploadcustomer"; } /** * @paramRequest *@paramfile*/ /** * @paramRequest *@paramfile*/@RequestMapping ("Uploadftp") Public voidUploadcustomer (httpservletrequest request,multipartfile file) {System.out.println ("-----------"+file); UserBean UserBean= (UserBean) request.getattribute ("UserBean"); inti; Sheet Sheet; Workbook Book; Cell cell1,cell2; List<CustomerBean> agents =NewArraylist<customerbean>(); Try{Commonsmultipartfile CF=(commonsmultipartfile) file; Diskfileitem fi=(Diskfileitem) Cf.getfileitem (); File F=fi.getstorelocation (); //T.xls is the Excel file name to readBook =Workbook.getworkbook (f); //get the first sheet object (sheet's number in Ecxel starts with 0, 0,1,2,3,....)Sheet = book.getsheet (0); I= 1; Ftpbean Ftpbean=NULL; Ftpserverbean Ftpserverbean=NULL; Customerbean Customerbean=NULL; while(true) { //get the cells for each rowFtpserverbean =NewFtpserverbean (); Customerbean=NewCustomerbean (); Ftpbean=NewFtpbean (); Cell1= Sheet.getcell (0, I);//(column, row) if("". Equals (Cell1.getcontents ()) = =true) Break;//if the data being read is emptyFtpserverbean.setservername (Cell1.getcontents ()); Cell2= Sheet.getcell (2, i); Ftpserverbean.setserverip (Cell2.getcontents ()); //ServerID beginFtpserverbean =Ftpserverservice.getftpserverbyserverip (Ftpserverbean); if(ftpserverbean!=NULL&& Ftpserverbean.getid () >0) {Ftpbean.setserverid (Ftpserverbean.getid ());//exist } Else{Ftpserverbean=NewFtpserverbean (); Ftpserverbean.setservername (Pinyin.addpinyinheadchar (Cell1.getcontents ())); Ftpserverbean.setserverip (Cell2.getcontents ()); Ftpserverbean.setcreateuser (Userbean.getid ()); int count = ftpserverservice.addftpserver (Ftpserverbean); Ftpbean.setserverid (Ftpserverbean.getid ()); } //ServerID End//CustomerCell1 = Sheet.getcell (1, i);//Customer NameCustomerbean.setname (Pinyin.addpinyinheadchar (Cell1.getcontents ())); Customerbean=Customerservice.selectcustomerbyname (Customerbean); if(customerbean!=NULL&& Customerbean.getid () >0) {ftpbean.setcustomername (Customerbean.getname ()); Ftpbean.setcustomerid (Customerbean.getid ()); Ftpbean.setipaddr (Ftpserverbean.getserverip ()); Ftpbean.setcreateuser (Ftpserverbean.getcreateuser ()); } Else{Customerbean=NewCustomerbean (); Customerbean.setname (Pinyin.addpinyinheadchar (Cell1.getcontents ())); Customerbean.setadduser (Userbean.getid ()); Customerbean.setcontacts ("**"); Customerbean.setcontacttel ("13156878391"); Customerbean.setsigningtime (NewDate ()); Customerbean.setcompletetime (NewDate ()); intCount =Customerservice.insertcustomer (Customerbean); Ftpbean.setcustomerid (Customerbean.getid ()); Ftpbean.setcustomername (Pinyin.addpinyinheadchar (Cell1.getcontents ())); Ftpbean.setipaddr (Ftpserverbean.getserverip ()); Ftpbean.setcreateuser (Ftpserverbean.getcreateuser ()); } //CustomerCell1 = Sheet.getcell (3, i); Ftpbean.setusername (Cell1.getcontents ());//User nameCell1 = Sheet.getcell (4, i); Ftpbean.setpassword (Cell1.getcontents ());//PasswordCell1 = Sheet.getcell (5, i); Ftpbean.setdomainname (Cell1.getcontents ());//bind domain nameCell1 = Sheet.getcell (6, i); Ftpbean.setdevelanguage (Cell1.getcontents ());//Development LanguageCell1 = Sheet.getcell (7, i); Ftpbean.setspacesize (Cell1.getcontents ());//Space Size Try{SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Cell1= Sheet.getcell (8, i); Date Date= Dateformat.parse (Cell1.getcontents (). Replace ("/", "-")); Ftpbean.setbegintime (date);//Start Time } Catch(Exception e) {System.out.println (e); Ftpbean.setbegintime (NewDate ());//Start Time } Try{SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); Cell1= Sheet.getcell (9, i); Date Date= Dateformat.parse (Cell1.getcontents (). Replace ("/", "-")); Ftpbean.setendtime (date);//Expiry Time } Catch(Exception e) {System.out.println (e); Ftpbean.setbegintime (NewDate (NewDate (). GetTime () +365*24*3600));//Start Time} cell1= Sheet.getcell (10, i); Ftpbean.setsqltype (Cell1.getcontents ());//DatabaseCell1 = Sheet.getcell (11, i); Ftpbean.setsqlname (Cell1.getcontents ());//Database nameCell1 = Sheet.getcell (12, i); Ftpbean.setsqlusername (Cell1.getcontents ());//User nameCell1 = Sheet.getcell (13, i); Ftpbean.setsqlpassword (Cell1.getcontents ());//Database PasswordCell1 = Sheet.getcell (14, i); Ftpbean.setinfo (Cell1.getcontents ());//Notesftpservice.addftp (Ftpbean); I++; } book.close (); } Catch(Exception e) {e.printstacktrace (); } } }
SPRINGMVC JXL database into an Excel file