您的位置:首页 > 编程语言 > Java开发

浅谈Spring中的定时功能使用 FTP判断文件是否存在

2016-08-24 16:30 507 查看
spring中的配置

 <bean id="telnetForSecondMint" class="org.springframework.scheduling.quartz.JobDetailBean">

  <property name="jobClass"

   value="net.ssd.dianLianDataUpload.task.MsgSendFileUploadForSecondMint"/>

 </bean>

 <bean id="telnetForSecondTask" class="org.springframework.scheduling.quartz.CronTriggerBean">

  <property name="jobDetail" ref="telnetForSecondMint"/>

  <property name="cronExpression" value="0 0/2 * * * ?"/>

 </bean>

 

 <bean id="taskScheduler"

  class="org.springframework.scheduling.quartz.SchedulerFactoryBean"

  autowire="no">

  <property name="triggers">

   <list>

    <ref bean="speakUpladTaskItems" />

    <ref bean="msgSendUploadTaskItems"/>

    <ref bean="telnetForSecondTask"/>

   </list>

  </property>

  <property name="quartzProperties">

   <props>

    <prop key="org.quartz.threadPool.threadCount">1</prop>

   </props>

  </property>

 </bean> 


创建对应的类文件即可

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

import java.util.Properties;

import net.ssd.framework.core.ApplicationContext;

import net.ssd.framework.util.ftp.FtpClient;

import org.apache.cxf.common.util.StringUtils;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

/**

 * @ClassName MsgSendFileUpload

 * @Description TODO: add type description

 * @author wangzm

 * @2014-2-26上午9:04:12

 */

public class MsgSendFileUploadForSecondMint  extends QuartzJobBean{

 

 @Override

 protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

  

  String logInfo = "";

  String fname = "";

  

  InputStream in = this.getClass().getResourceAsStream("/ftp.properties");

  Properties prop = new Properties();

  

  try {

   prop.load(in);

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  String ftpIP = prop.getProperty("ftpip");

  String ftpPortStr = prop.getProperty("ftpport");

  int ftpPort = 21;

  if(!StringUtils.isEmpty(ftpPortStr)){

   ftpPort = Integer.parseInt(ftpPortStr);

  }

  String ftpUser = prop.getProperty("ftpuser");

  String ftpPwd = prop.getProperty("ftppassword");

  String ftpRootPath = prop.getProperty("ftprootpath");

  String ftpUpLoadLogPath = prop.getProperty("ftpuploadlogpath");

  

  if("yyyy".equals(ftpRootPath)){//配置文件是yyyy 的话则根据相应的年来生成目录

   ftpRootPath = Calendar.getInstance().get(Calendar.YEAR)+"";

  }else{//自定义目录

   

   if(ftpRootPath ==null || "".equals(ftpRootPath)){

    ftpRootPath=File.separator;

   }

  }

  

  

  String LocalSysRootPath = ApplicationContext.getRootPath();

  File txtFile = new File(LocalSysRootPath + File.separator + "datatxt");

  if(!txtFile.exists() && !txtFile.isDirectory()){

   txtFile.mkdirs();

  }else{

   File[] fList = txtFile.listFiles();

   if(fList.length > 0 ){

    //FtpClient ftp = new FtpClient("192.168.0.178", 21,"ftp1","ftp1"); //测试用的地址

    //实际的地址需要配置在 ftp.properties中   将上面的注释

    FtpClient ftp = new FtpClient(ftpIP, ftpPort,ftpUser,ftpPwd); 

    

    if(ftp.connect()){

     //String telnetPath = File.separator + "datatxt" + File.separator;

     List<String> fileList = new ArrayList<String>();

     //开始上传文件

     //此标志用于判断是否可以上传该文件

     for(File f:fList){

      fname = f.getName();

      boolean isUp = false;

      try {

       String[] isEx = ftp.getFtpClient().listNames(f.getName().replaceAll(".txt",
""));

       if(isEx == null || "".equals(isEx)){

        //System.out.println("服务器上不存在此文件,可以上传");

        isUp = true;

       }else{

        //System.out.println("此文件已经存在不上传:" + isEx.length);

        //将本地的此文件添加到待删除列表中

        fileList.add(f.getPath());

       }

      } catch (IOException e) {

       e.printStackTrace();

      }

      //若 isUp为true,说明服务器上没有此文件 ,可以上传

      if(isUp){

       if(f.exists()){

        if(ftp.uploadFile(f, ftpRootPath)){

         //System.out.println("上传成功。。for mint、。。" + f.getName() + ":ftpRootPath:" + ftpRootPath);

         //上传成功后将文件删除掉 先添加到待删除列表中

         fileList.add(f.getPath());

         logInfo = "文件上传成功";

        }else{

         //上传失败后不对文件进行处理,等下次重新传送

         logInfo = "文件上传失败";

         //System.out.println("datatxt文件上传失败 for mint:" + f.getName());

        }

        this.addLog(ftpUpLoadLogPath, fname, logInfo);

        

       }

       

      }

      

     }

     //上传文件完成后,关闭ftp连接

     ftp.disconnect();

     

     //上传成功后将文件删除掉

     for(String fdel:fileList){

      File delFile = new File(fdel);

      delFile.delete();

     }

     

    }else{

     //ftp连接失败

     this.addLog(ftpUpLoadLogPath, "", "FTP连接失败");

    }

   }else{

    //System.out.println("datatxt中没有文件,不进行上传操作");

   }

    

   

  }

  

 }

 //记录日志的函数

 public void addLog(String logPath,String fileName,String logInfo){

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  Date dt = new Date();

  String logName = sdf.format(dt);

  File lf = new File(logPath);

  if(!lf.exists()){

   lf.mkdirs();

  }

  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

  try {

   RandomAccessFile raf = new RandomAccessFile(

     logPath + File.separator + logName + ".txt", "rw");

   long flength = raf.length();

   raf.seek(flength);

   String fileLog = "";

   if(flength>0){

    fileLog = "\r\n";

   }

   fileLog += sdf.format(new Date()) + ":"+logInfo+"!文件名称2:"+fileName;

   //System.out.println("fileName:" + fileName);

   raf.write(fileLog.getBytes());

   

   raf.close();

   

   

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }

  

  

  

  

  

 }

 

 

 

}

cron表达式的基本使用:

 

字段 允许值 允许的特殊字符

秒 0-59 , - * /

分 0-59 , - * /

小时 0-23 , - * /

日期 1-31 , - * ? / L W C

月份 1-12 或者 JAN-DEC , - * /

星期 1-7 或者 SUN-SAT , - * ? / L C #

年(可选) 留空, 1970-2099 , - * /

表达式意义

"0 0 12 * * ?" 每天中午12点触发

"0 15 10 ? * *" 每天上午10:15触发

"0 15 10 * * ?" 每天上午10:15触发

"0 15 10 * * ? *" 每天上午10:15触发

"0 15 10 * * ? 2005" 2005年的每天上午10:15触发

"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

"0 15 10 15 * ?" 每月15日上午10:15触发

"0 15 10 L * ?" 每月最后一日的上午10:15触发

"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

每天早上6点

0 6 * * *

每两个小时

0 */2 * * *

晚上11点到早上8点之间每两个小时,早上八点

0 23-7/2,8 * * *

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 11 4 * 1-3

1月1日早上4点

0 4 1 1 *



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息