定时使用多线程从ftp读取excel文件并写入数据库

**话不多说,直接附代码**
			**第一步**建立远程连接,使用xftp连接**
						FTPClient ftpClient = new FTPClient();
        				ftpClient.connect("ip", 端口);//连接ftp
        				ftpClient.login("用户名", "密码");//登陆ftp
        				ftpClient.setControlEncoding("gbk"); // 中文支持
        				ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
       					ftpClient.enterLocalPassiveMode();//设置ftp 模式,有被动模式和活动模式,这里设置为被动模式
					    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))//是否连接成功,成功true,失败false
					    	{
					    		try {
									ftpClient.changeWorkingDirectory("zxcx-ftp");// 更改当前工作目录,zxcx-ftp
			**第二步**读取excel文件集
					            	FTPFile[] file = ftpClient.listFiles(); // 从ftp上获取zxcx-ftp目录下的文件
					            	for (int m = 0; m < file.length; m++) {  // 遍历所有文件,匹配需要查找的文件
					            		if (file[m].isFile()) {  // 判断是否为文件
					                        ftpClient.enterLocalPassiveMode();  
					                        String content = new String(file[m].getName().getBytes("GB2312"), "ISO-8859-1");//此处改为ISO-8859-1  是因为ftp的中文只接受 ISO-8859-1类型
					                        FileThread thread = new FileThread(ftpClient.retrieveFileStream(content)); // 将文件写入多线程
					                        thread.start();  启用线程
					                        ftpClient.completePendingCommand(); //使用一次必须使用它来检查返回值是否验证成功  如果没有 后续命令会出错,这里出现了读取第二个文件的时候null  正确码 226
					                    } else {
					                        // 这边为目录 可以选择进入下一级菜单
					                        continue;
					                    }
					                }
					            } catch (IOException e) {
					                e.printStackTrace();
					            }
					        }
					    }
			**第四步**:创建线程类 构造方法自己实现 直接附上run方法
							@Override
						    public void run() {
						        InputStream input = this.stream; //获取文件流
						        try {
						            XSSFWorkbook book = new XSSFWorkbook(input); 
						            XSSFSheet sheet = book.getSheetAt(0);  // 第一个工作表
						            EleValueVO eleValueVO = new EleValueVO();   // 固定实体类
			**第三步**:获取单元格内容
						            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {  //总行数
						                XSSFRow row = sheet.getRow(i);  // 获取行
						                for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {   //获取总列数
						                    getCellValue(row.getCell(j));// 设置类型
						                }
						                if (row == null) {  //判断是否行为空
						                    System.out.println("this row is null ----------------------");
						                    continue;
						                } else {
						            			使用GETSETTER和SETTER进行传参
						               	}
						            }
						            book.close();  //依次关闭流
						            input.close();
						        } catch (IOException e) {
						            e.printStackTrace();
						        }
						
						    }
						//此处为转换类型
							public static String getCellValue(Cell cell) {
							   String cellValue = "";
   							   if (cell == null) {
						           return cellValue;
						       }
						       // 判断数据的类型
						       switch (cell.getCellType()) {
						           case Cell.CELL_TYPE_NUMERIC: // 数字
						               //short s = cell.getCellStyle().getDataFormat();
						               if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
						                   SimpleDateFormat sdf = null;
						                   // 验证short值
							                   if (cell.getCellStyle().getDataFormat() == 14) {
							                       sdf = new SimpleDateFormat("yyyy/MM/dd");
							                   } else if (cell.getCellStyle().getDataFormat() == 21) {
							                       sdf = new SimpleDateFormat("HH:mm:ss");
							                   } else if (cell.getCellStyle().getDataFormat() == 22) {
							                       sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
							                   } else {
							                       throw new RuntimeException("日期格式错误!!!");
							                   }
							                   Date date = cell.getDateCellValue();
							                   cellValue = sdf.format(date);
							               } else if (cell.getCellStyle().getDataFormat() == 0) {//处理数值格式
							                   cell.setCellType(Cell.CELL_TYPE_STRING);
							                   cellValue = String.valueOf(cell.getRichStringCellValue().getString());
							               }
							               break;
							           case Cell.CELL_TYPE_STRING: // 字符串
							               cellValue = String.valueOf(cell.getStringCellValue());
							               break;
							           case Cell.CELL_TYPE_BOOLEAN: // Boolean
							               cellValue = String.valueOf(cell.getBooleanCellValue());
							               break;
							           case Cell.CELL_TYPE_FORMULA: // 公式
							               cellValue = String.valueOf(cell.getCellFormula());
							               break;
							           case Cell.CELL_TYPE_BLANK: // 空值
							               cellValue = null;
							               break;
							           case Cell.CELL_TYPE_ERROR: // 故障
							               cellValue = "非法字符";
							               break;
							           default:
							               cellValue = "未知类型";
							               break;
							       }
							       return cellValue;
							   }
     		**第五步** 加入定时任务
					创建config文件夹 写入config配置文件
					@Configuration
					@EnableScheduling
					public class SpringTaskConfig {
					    @Bean
					    public TaskScheduler taskScheduler() {
					        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
					        scheduler.setPoolSize(10); //线程池大小
					        scheduler.setThreadNamePrefix("spring-task-thread");//线程名字前缀
					        return scheduler;
					    }
					 }
  					//在定时方法加入注解
					@Scheduled(cron = "0/60 * 60 * 24 *  * ? ")   为一天执行一次,0/1为一秒执行一次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值