java定时备份mysql,定时ftp上传,下载,删除,重命名,文件(改)

需要的jar包有这么几个如下:

org.apache.commons.io-1.4.0.jar

commons-codec-1.6.jar

commons-collections-3.2.1.jar

commons-httpclient-3.0-rc1.jar

commons-logging-1.0.4.jar

commons-net-3.1.jar

quartz-all-1.6.0.jar

配置文件信息 times.properties

ss:10                                   //不能为空
mm:*                                    //不能为空
hh:*                                    //不能为空
dd:*                                    //不能为空
mo:*                                    //不能为空
we:?                                    //不能为空
yy:*                                    //不能为空
logFile:d:/log/t.log                    //不能为空
batFile:D:/news_back/backup.bat         //不能为空
sqlFile:D:/news_back/backup.sql         //不能为空
ftpIp:127.0.0.1                         //不能为空
ftpUserName:user                        //不能为空
ftpPassword:user                        //不能为空
ftpPort:21                              //不能为空
ftpDirectory:shuiyi                   //不能为空
ftpRemoteFile:*                         //
ftpLocalFilePath:shuiyi.xml             //
ftpMyFileString:myfile.xml              //
ftpOldFileNameString:viewDetail.jsp     //
ftpNewFileNameString:newName.jsp        //
ftpDeleteFile:UpdateData.class          //
ftpDownFile:UpdateData.java             //
ftpDownFileNewNameString:loadFile.java  //

//ss 秒 0-59 ,-*/
//mm 分 0-59 ,-*/
//hh 时 0-23 ,-*/
//dd 日 1-31 ,-*/?LW
//mo 月 1-12 ,-*/
//we 周 1-7 ,-*/?L#
//yy 年 1970-2099 ,-*/
//ftpDirectory进入文件夹webroot
//ftpRemoteFile:列出webroot目录下所有jsp文件
//上传文件woxingwosu.xml,重新命名为myfile.xml
//日志目录不能自动创建手动创建

定时器

package com.db;

import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzMs {
	public static void main(String[] args) {
		
		try{
			try {
				Util_log.testLog();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			}
			Prop.setArg("./times.properties");
			String timeString=Prop.getSsString()+" "+Prop.getMmString()+" "+Prop.getHhString()+" "+Prop.getDdString()+" "+Prop.getMoString()+" "+Prop.getWeString();
			Scheduler scheduler=new StdSchedulerFactory().getScheduler();
			JobDetail jobdetail001=new JobDetail(timeString,"backup001",CreateFileUtil.class);
			CronTrigger cronTrigger=new CronTrigger("备份中","group001",timeString);
			JobDetail jobdetail002=new JobDetail(timeString,"backup001",MiniFtp.class);
			CronTrigger cronTrigger2=new CronTrigger("上传中","group001",timeString);
			
			//JobBuilder jobBuilder001=(JobBuilder) newJob(CreateFileUtil.class).withIdentity("myJob").build();
			//Trigger trigger001= (Trigger) newTrigger().withIdentity("triggerName001","triggerNameGroup");
			scheduler.scheduleJob(jobdetail001,cronTrigger);
			scheduler.scheduleJob(jobdetail002,cronTrigger2);
			scheduler.start();
			
		}catch(SchedulerException se){
			Util_log.logger.log(Level.SEVERE,se.getLocalizedMessage(), se);
		}catch(ParseException pe){
			Util_log.logger.log(Level.SEVERE,pe.getLocalizedMessage(), pe);
		}finally{
			try {
				Util_log.testLog();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			}
		}
	}
}


日志:

package com.db;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Util_log {
	static Logger logger=Logger.getLogger("test.Test");
	public static void testLog() throws SecurityException, IOException{
		Prop.setArg("./times.properties");
		FileHandler fileHandler=new FileHandler(Prop.getLogFileNameString(),true);
		logger.addHandler(fileHandler);
		logger.setLevel(Level.ALL);
		SimpleFormatter simpleFormatter=new SimpleFormatter();
		fileHandler.setFormatter(simpleFormatter);
	}
}
配置文件的实现:

package com.db;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;


public class Prop {
	private static Properties properties=null;
	private static String ssString;
	private static String mmString;
	private static String hhString;
	private static String ddString;
	private static String moString;
	private static String weString;
	private static String yyString;
	
	private static String logFileNameString;
	private static String batFileString;
	private static String sqlFileString;
	
	private static String ftpUserNameString;
	private static String ftpPasswordString;
	private static String ftpIpString;
	private static int ftpPort;
	private static String ftpWorkingDirectoryString;
	private static String ftpRemoteFileString;
	private static String ftpLocalFilePath;
	private static String ftpMyFileString;
	private static String ftpOldFileNameString;
	private static String ftpNewFileNameString;
	private static String ftpDeleteFile;
	private static String ftpDownFile;
	private static String ftpDownFileNewNameString;
	
	public static String getFtpOldFileNameString() {
		return ftpOldFileNameString;
	}
	public static void setFtpOldFileNameString(String ftpOldFileNameString) {
		Prop.ftpOldFileNameString = ftpOldFileNameString;
	}
	public static String getFtpNewFileNameString() {
		return ftpNewFileNameString;
	}
	public static void setFtpNewFileNameString(String ftpNewFileNameString) {
		Prop.ftpNewFileNameString = ftpNewFileNameString;
	}
	public static String getFtpLocalFilePath() {
		return ftpLocalFilePath;
	}
	public static void setFtpLocalFilePath(String ftpLocalFilePath) {
		Prop.ftpLocalFilePath = ftpLocalFilePath;
	}
	public static String getFtpMyFileString() {
		return ftpMyFileString;
	}
	public static void setFtpMyFileString(String ftpMyFileString) {
		Prop.ftpMyFileString = ftpMyFileString;
	}
	public static String getFtpUserNameString() {
		return ftpUserNameString;
	}
	public static void setFtpUserNameString(String ftpUserNameString) {
		Prop.ftpUserNameString = ftpUserNameString;
	}
	public static String getFtpPasswordString() {
		return ftpPasswordString;
	}
	public static void setFtpPasswordString(String ftpPasswordString) {
		Prop.ftpPasswordString = ftpPasswordString;
	}
	public static String getFtpIpString() {
		return ftpIpString;
	}
	public static void setFtpIpString(String ftpIpString) {
		Prop.ftpIpString = ftpIpString;
	}
	public static int getFtpPort() {
		return ftpPort;
	}
	public static void setFtpPort(int ftpPort) {
		Prop.ftpPort = ftpPort;
	}
	
	public static String getSsString() {
		return ssString;
	}
	public static void setSsString(String ssString) {
		Prop.ssString = ssString;
	}
	public static String getMmString() {
		return mmString;
	}
	public static void setMmString(String mmString) {
		Prop.mmString = mmString;
	}
	public static String getHhString() {
		return hhString;
	}
	public static void setHhString(String hhString) {
		Prop.hhString = hhString;
	}
	public static String getDdString() {
		return ddString;
	}
	public static void setDdString(String ddString) {
		Prop.ddString = ddString;
	}
	public static String getMoString() {
		return moString;
	}
	public static void setMoString(String moString) {
		Prop.moString = moString;
	}
	public static String getWeString() {
		return weString;
	}
	public static void setWeString(String weString) {
		Prop.weString = weString;
	}
	public static String getYyString() {
		return yyString;
	}
	public static void setYyString(String yyString) {
		Prop.yyString = yyString;
	}
	public static String getLogFileNameString() {
		return logFileNameString;
	}
	public static void setLogFileNameString(String logFileNameString) {
		Prop.logFileNameString = logFileNameString;
	}
	public static String getBatFileString() {
		return batFileString;
	}
	public static void setBatFileString(String batFileString) {
		Prop.batFileString = batFileString;
	}
	public static String getSqlFileString() {
		return sqlFileString;
	}
	public static void setSqlFileString(String sqlFileString) {
		Prop.sqlFileString = sqlFileString;
	}
	public static String getFtpWorkingDirectoryString() {
		return ftpWorkingDirectoryString;
	}
	public static void setFtpWorkingDirectoryString(
			String ftpWorkingDirectoryString) {
		Prop.ftpWorkingDirectoryString = ftpWorkingDirectoryString;
	}
	
	public static void setArg(String configFile){
		properties=new Properties();
		BufferedInputStream bufferedInputStream=null;
		try{
			bufferedInputStream=new BufferedInputStream(new FileInputStream(configFile));
			properties.load(bufferedInputStream);
			ssString=properties.getProperty("ss");
			mmString=properties.getProperty("mm");
			hhString=properties.getProperty("hh");
			ddString=properties.getProperty("dd");
			moString=properties.getProperty("mo");
			weString=properties.getProperty("we");
			yyString=properties.getProperty("yy");
			logFileNameString=properties.getProperty("logFile");
			batFileString=properties.getProperty("batFile");
			sqlFileString=properties.getProperty("sqlFile");
			ftpIpString=properties.getProperty("ftpIp");
			ftpUserNameString=properties.getProperty("ftpUserName");
			ftpPasswordString=properties.getProperty("ftpPassword");
			ftpPort=Integer.parseInt(properties.getProperty("ftpPort"));
			ftpWorkingDirectoryString=properties.getProperty("ftpDirectory");
			ftpRemoteFileString=properties.getProperty("ftpRemoteFile");
			ftpLocalFilePath=properties.getProperty("ftpLocalFilePath");
			ftpMyFileString=properties.getProperty("ftpMyFileString");
			ftpOldFileNameString=properties.getProperty("ftpOldFileNameString");
			ftpNewFileNameString=properties.getProperty("ftpNewFileNameString");
			ftpDeleteFile=properties.getProperty("ftpDeleteFile");
			ftpDownFile=properties.getProperty("ftpDownFile");
			ftpDownFileNewNameString=properties.getProperty("ftpDownFileNewNameString");
			
			
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}finally{
			try{
				if(bufferedInputStream != null){
					bufferedInputStream.close();
				}
			}catch(Exception e){
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			}
		}
	}
	public static String getFtpRemoteFileString() {
		return ftpRemoteFileString;
	}
	public static void setFtpRemoteFileString(String ftpRemoteFileString) {
		Prop.ftpRemoteFileString = ftpRemoteFileString;
	}
	public static String getFtpDeleteFile() {
		return ftpDeleteFile;
	}
	public static void setFtpDeleteFile(String ftpDeleteFile) {
		Prop.ftpDeleteFile = ftpDeleteFile;
	}
	public static String getFtpDownFile() {
		return ftpDownFile;
	}
	public static void setFtpDownFile(String ftpDownFile) {
		Prop.ftpDownFile = ftpDownFile;
	}
	public static String getFtpDownFileNewNameString() {
		return ftpDownFileNewNameString;
	}
	public static void setFtpDownFileNewNameString(
			String ftpDownFileNewNameString) {
		Prop.ftpDownFileNewNameString = ftpDownFileNewNameString;
	}
}
文件的上传:

package com.db;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.logging.Level;
import java.text.SimpleDateFormat;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MiniFtp implements Job{
	private static FTPClient ftpClient=null;
	private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd hh:mm");
	private static final String [] FILE_TYPES={"文件","目录","符号链接","未知类型"};

	/*
	 * 设置ftp客户端的配置--一般不设置
	 * @return
	 */
	private static FTPClientConfig getFtpConfig(){
		FTPClientConfig ftpClientConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);
		ftpClientConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
		return ftpClientConfig;
	}
	/*
	 * 连接服务器
	 */
	public static void connectServer(){
		if(ftpClient == null){
			int reply;
			try{
				Prop.setArg("./times.properties");
				ftpClient =new FTPClient();
				ftpClient.setDefaultPort(Prop.getFtpPort());
				ftpClient.configure(getFtpConfig());
				ftpClient.connect(Prop.getFtpIpString());
				ftpClient.login(Prop.getFtpUserNameString(), Prop.getFtpPasswordString());
				Util_log.logger.log(Level.INFO,ftpClient.getReplyString());
				reply=ftpClient.getReplyCode();
				if(!FTPReply.isPositiveCompletion(reply)){
					ftpClient.disconnect();
					Util_log.logger.log(Level.WARNING,"FTP server refused connection.");
				}
				
			}catch(Exception e){
				Util_log.logger.log(Level.SEVERE,"登录ftp服务器【"+Prop.getFtpIpString()+"】失败");
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			}
		}
	}
	/*
	 * 关闭连接
	 */
	public static void closeConnect(){
		try{
			if(ftpClient != null){
				ftpClient.logout();
				ftpClient.disconnect();
			}
			
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}
	}
	/*
	 * 转码【iso-8859-1】 -> gbk
	 * 不同的平台需要不同的转码
	 * @param obj
	 * @return
	 */
	private static String iso8859togbk(Object obj){
		try{
			if(obj == null)
				return "";
			else 
				return new String(obj.toString().getBytes("iso-8859-1"),"GBK");
			
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			return "";
		}
	}
	/*
	 * 上传文件
	 * @param localFilePath--本地文件路径
	 * @param newFileName--新的文件名
	 */
	public static void uploadFile(String localFilePath,String newFileName){
		connectServer();
		BufferedInputStream bufferedInputStream=null;
		try{
			bufferedInputStream=new BufferedInputStream(new FileInputStream(localFilePath));
			ftpClient.storeFile(newFileName, bufferedInputStream);
			
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}finally{
			if(bufferedInputStream != null)
				try {
					bufferedInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
				}
		}
	}
	/*
	 * 下载文件
	 * @param remoeFile--服务器上文件名
	 * @param localFileName--本地文件名
	 */
	public static void downFile(String remoeFile,String localFileName){
		connectServer();
		BufferedOutputStream bufferedOutputStream=null;
		try{
			bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(remoeFile));
			ftpClient.retrieveFile(localFileName, bufferedOutputStream);
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}finally{
			try{
				if(bufferedOutputStream != null)
					bufferedOutputStream.close();
				
			}catch(Exception e){
				Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
			}
		}
	}
	/*列出服务器上文件和目录
	 * @param regStr--匹配的正则表达式
	 * 
	 */
	public static void listRemoteFiles(String regStr){
		connectServer();
		try{
			FTPFile[] ftpFile=ftpClient.listFiles(regStr);
			if(ftpFile == null || ftpFile.length == 0){
				Util_log.logger.log(Level.INFO,"There has not any file!");
			}else{
				TreeSet<FTPFile> treeSet=new TreeSet<FTPFile>(new Comparator<Object>() {
					public int compare(Object objectFile1,Object objectFile2){
						if(objectFile1 == null)
							return -1;
						else if(objectFile2 == null)
							return 1;
						else{
							FTPFile ftpFile1=(FTPFile) objectFile1;
							FTPFile ftpFile2=(FTPFile) objectFile2;
							if(ftpFile2.getType() != ftpFile1.getType())
								return ftpFile2.getType()-ftpFile1.getType();
							else
								return ftpFile1.getName().compareTo(ftpFile2.getName());
						}
					}
				});
				for(FTPFile file:ftpFile)
					treeSet.add(file);
				System.out.printf("%-35s%-10s%15s%15s\n","名称","类型","修改日期","大小");
				for(FTPFile file:treeSet){
					System.out.printf("%-35s%-10s%15s%15s\n",iso8859togbk(file.getName()),FILE_TYPES[file.getType()]
                            ,simpleDateFormat.format(file.getTimestamp().getTime()),FileUtils.byteCountToDisplaySize(file.getSize()));
                }
			}
			
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}
	}
	/**
	 * 列出服务器上所有文件及目录
	 */
	public static void listAllRemoteFiles(){
		listRemoteFiles("*");
	}
	/**
     * 设置传输文件的类型[文本文件或者二进制文件]
     * @param fileType--BINARY_FILE_TYPE、ASCII_FILE_TYPE 
    */
	public static void setFileType(int fileType){
       try{
            connectServer();
            ftpClient.setFileType(fileType);
        }catch(Exception e){
        	Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
        }
    }
	   /**
	    * 扩展使用
	    * @return
	   */
	  protected static FTPClient getFtpClient(){
	       connectServer();
	      return ftpClient;
	   }
	  /**
	   * 进入到服务器的某个目录下
	   * @param directory
	  */
	 public static void changeWorkingDirectory(String directory){
	     try{
	          connectServer();
	          ftpClient.changeWorkingDirectory(directory);
	      }catch(IOException ioe){
	    	  Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
	      }
	  }
	 /**
	  * 返回到上一层目录
	 */
	public static void changeToParentDirectory(){
	    try{
	         connectServer();
	         ftpClient.changeToParentDirectory();
	     }catch(IOException ioe){
	    	 Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
	     }
	 }
	
	/**
	  * 删除文件
	 */
	public static void deleteFile(String filename){
	    try{
	         connectServer();
	         ftpClient.deleteFile(filename);
	     }catch(IOException ioe){
	    	 Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
	     }
	 }
	
	/**
	  * 重命名文件 
	  * @param oldFileName --原文件名
	  * @param newFileName --新文件名
	 */
	public static void renameFile(String oldFileName,String newFileName){
	    try{
	         connectServer();
	         ftpClient.rename(oldFileName, newFileName);
	     }catch(IOException ioe){
	    	 Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
	     }
	 }
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		Prop.setArg("./times.properties");
		connectServer();
		listAllRemoteFiles();
		if(Prop.getFtpWorkingDirectoryString() == null){
			Util_log.logger.log(Level.INFO,"进入目录中.....");
			changeWorkingDirectory("*");
		}else{
			changeWorkingDirectory(Prop.getFtpWorkingDirectoryString());
		}
		if(Prop.getFtpRemoteFileString() == null){
			listAllRemoteFiles();
		}else{
			listRemoteFiles(Prop.getFtpRemoteFileString());
		}
		
		setFileType(FTP.BINARY_FILE_TYPE);
		if(Prop.getFtpLocalFilePath() == null || Prop.getFtpMyFileString() == null){
			Util_log.logger.log(Level.WARNING,"上传失败...");
		}else{
			uploadFile(Prop.getFtpLocalFilePath(), Prop.getFtpMyFileString());
		}
		if(Prop.getFtpOldFileNameString() == null || Prop.getFtpNewFileNameString() == null){
			
		}else{
			renameFile(Prop.getFtpOldFileNameString(),Prop.getFtpNewFileNameString());
		}
		if(Prop.getFtpDeleteFile() == null){
			
		}else{
			deleteFile(Prop.getFtpDeleteFile());
		}
		if(Prop.getFtpDownFile()==null || Prop.getFtpDownFileNewNameString()==null){
			
		}else{
			downFile(Prop.getFtpDownFile(), Prop.getFtpDownFileNewNameString());
		}
		closeConnect();
	}
  
}


mysql自动备份:

package com.db;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class CreateFileUtil implements Job {
	/*
	 * @param destFileName -- 
	 * @return boolean
	 */
	
	public static boolean createFile(String destFileName){
		Prop.setArg("./times.properties");
		File file=new File(destFileName);
		if(file.exists()){
			Util_log.logger.log(Level.WARNING,"创建单个文件"+destFileName+"失败,目录已存在");
			try{
				FileWriter fileWriter=new FileWriter(file);
				fileWriter.write("mysqldump -uroot -poracle mail > "+Prop.getSqlFileString());
				fileWriter.flush();
				fileWriter.close();
				
			}catch(IOException ioe){
				Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
			}
			return false;
		}
		if(!file.getParentFile().exists()){
			Util_log.logger.log(Level.WARNING,"目标文件所在路径不存在,准备创建......");
			
			if(!file.getParentFile().mkdir()){
				Util_log.logger.log(Level.WARNING,"创建目录文件所在的目录失败!");
				return false;
			}
		}
		//创建目标文件
		try{
			if(file.createNewFile()){
				Util_log.logger.log(Level.WARNING,"创建单个文件"+destFileName+"成功");
				try{
					FileWriter fileWriter=new FileWriter(file);
					fileWriter.write("mysqldump -uroot -poracle mail > "+Prop.getSqlFileString());
					fileWriter.flush();
					fileWriter.close();
					
				}catch(IOException ioe){
					Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
				}
				return true;
			}else{
				Util_log.logger.log(Level.WARNING,"创建单个文件"+destFileName+"失败");
				return false;
			}
			
		}catch(IOException ioe){
			Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
			Util_log.logger.log(Level.SEVERE,"创建单个文件"+destFileName+"失败");
			return false;
		}
	}
	public static boolean createDir(String destDirName){
		File file=new File(destDirName);
		if(file.exists()){
			Util_log.logger.log(Level.WARNING,"创建目录"+destDirName+"失败,目标已存在");
			return false;
		}
		if(!destDirName.endsWith(File.separator))
			destDirName = destDirName+File.separator;
			//创建单个目录
			if(file.mkdir()){
				Util_log.logger.log(Level.INFO,"创建目录"+destDirName+"成功");
				return true;
			}else{
				Util_log.logger.log(Level.INFO,"创建目录"+destDirName+"失败");
				return false;
			}
	}
	public static String createTempFile(String prefix,String suffix,String dirName){
		File tempFile=null;
		try{
			if(suffix == null){
				// 在默认文件夹下创建临时文件
				tempFile=new File(suffix,dirName);
				return tempFile.getCanonicalPath();
			}else{
				File file=new File(dirName);
				// 如果临时文件所在目录不存在,首先创建
				if(!file.exists()){
					if(!CreateFileUtil.createDir(dirName)){
						Util_log.logger.log(Level.INFO,"创建临时文件失败,不能创建临时文件所在目录!");
						return null;
					}
				}
				tempFile= File.createTempFile(prefix, suffix, file);
				return tempFile.getCanonicalPath();
			}
			
		}catch(IOException ioe){
			Util_log.logger.log(Level.SEVERE,ioe.getLocalizedMessage(), ioe);
			Util_log.logger.log(Level.SEVERE,"创建临时文件失败:"+ioe.getMessage());
			return null;
		}
	}
	/*
	 * @param newfile 在某个目录创建一个bat文件
	 * @param dbusername 数据库用户名
	 * @param dbuserpassword 数据库密码
	 * @param dbname 数据库名
	 * 
	 */
	public static void backup() throws IOException{
		Prop.setArg("./times.properties");
		CreateFileUtil.createFile(Prop.getBatFileString());
		try{
			Process process=Runtime.getRuntime().exec(Prop.getBatFileString());
			process.waitFor();
			System.out.println(process.waitFor());
			process.destroy();
		}catch(Exception e){
			Util_log.logger.log(Level.SEVERE,"执行失败");
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}
	}
	@SuppressWarnings("deprecation")
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			CreateFileUtil.backup();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Util_log.logger.log(Level.SEVERE,e.getLocalizedMessage(), e);
		}
		Util_log.logger.log(Level.INFO,new Date().toLocaleString());
	}
	/*
	public static void main(String[] args){
		String dirNameString="d:/avi/text";
		CreateFileUtil.createDir(dirNameString);
		String fileNameString=dirNameString+"text2.txt";
		CreateFileUtil.createFile(fileNameString);
		String prefix="temp";
		String suffix=".txt";
		CreateFileUtil.createTempFile(prefix, suffix, dirNameString);
	}
	*/
}







你可能感兴趣的:(java定时备份mysql,定时ftp上传,下载,删除,重命名,文件(改))