@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 agents = new ArrayList();try{
CommonsMultipartFile cf=(CommonsMultipartFile) file;
DiskFileItem fi=(DiskFileItem) cf.getFileItem();
File f=fi.getStoreLocation();//t.xls为要读取的excel文件名
book =Workbook.getWorkbook(f);//获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
sheet = book.getSheet(0);
i= 1;
FtpBean ftpBean=null;
FtpServerBean ftpServerBean= null;
CustomerBean customerBean= null;while (true) {//获取每一行的单元格
ftpServerBean = newFtpServerBean();
customerBean= newCustomerBean();
ftpBean= newFtpBean();
cell1= sheet.getCell(0, i);//(列,行)
if ("".equals(cell1.getContents()) == true)break; //如果读取的数据为空
ftpServerBean.setServerName(cell1.getContents());
cell2= sheet.getCell(2,i);
ftpServerBean.setServerIp(cell2.getContents());//serverid begin
ftpServerBean =ftpServerService.getFtpServerByServerIP(ftpServerBean);if(ftpServerBean!=null && ftpServerBean.getId()>0)
{
ftpBean.setServerId(ftpServerBean.getId());//存在
}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//customer
cell1 = sheet.getCell(1, i);//客户名
customerBean.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());int count =customerService.insertCustomer(customerBean);
ftpBean.setCustomerId(customerBean.getId());
ftpBean.setCustomerName(PinYin.addPinYinHeadChar(cell1.getContents()));
ftpBean.setIpAddr(ftpServerBean.getServerIp());
ftpBean.setCreateUser(ftpServerBean.getCreateUser());
}//customer
cell1 = sheet.getCell(3, i);
ftpBean.setUserName(cell1.getContents());//用户名
cell1 = sheet.getCell(4, i);
ftpBean.setPassword(cell1.getContents());//密码
cell1 = sheet.getCell(5, i);
ftpBean.setDomainName(cell1.getContents());//绑定域名
cell1 = sheet.getCell(6, i);
ftpBean.setDeveLanguage(cell1.getContents());//开发语言
cell1 = sheet.getCell(7, i);
ftpBean.setSpaceSize(cell1.getContents());//空间大小
try{
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
cell1= sheet.getCell(8, i);
Date date= dateFormat.parse(cell1.getContents().replace("/", "-"));
ftpBean.setBeginTime(date);//开始时间
}catch(Exception e) {
System.out.println(e);
ftpBean.setBeginTime(new Date());//开始时间
}try{
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
cell1= sheet.getCell(9, i);
Date date= dateFormat.parse(cell1.getContents().replace("/", "-"));
ftpBean.setEndTime(date);//到期时间
}catch(Exception e) {
System.out.println(e);
ftpBean.setBeginTime(new Date(new Date().getTime()+365*24*3600));//开始时间
}
cell1= sheet.getCell(10, i);
ftpBean.setSqlType(cell1.getContents());//数据库
cell1 = sheet.getCell(11, i);
ftpBean.setSqlName(cell1.getContents());//数据库名
cell1 = sheet.getCell(12, i);
ftpBean.setSqlUserName(cell1.getContents());//用户名
cell1 = sheet.getCell(13, i);
ftpBean.setSqlPassword(cell1.getContents());//数据库密码
cell1 = sheet.getCell(14, i);
ftpBean.setInfo(cell1.getContents());//备注
ftpService.addFtp(ftpBean);
i++;
}
book.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}