FTP上传日志

本类的功能:

1.每天凌晨1点自动上传昨天的一天的日志信息。

2.上传当天的日志信息。

注意点:

1.日志信息为存储于数据库中的json字符串。

2.生产的文件为xml格式,生成文件存储路径:工程名\WEB-INF\classes\4ALog

3.每个日志不超过1M

 

    LogTask.java

 

package org.jasig.cas.a4.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.json.JSONObject;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;


/**
 * Title : AuditLog4AService
 * <p/>
 * Description : 4A审计日志非实时接口
 * <p/>
 * CopyRight : CopyRight (c) 2011
 * <p/>
 * Company : 亚信联创科技(中国)有限公司
 */
public class LogTask extends TimerTask {
	
	private static String propertieName = "4ALog.properties";
    private static String charset = "UTF-8";
	public static A4LogDao a4LogDao;

    private static final int size = 1 * 1024 * 1024;
    //测试
//    private static final int size = 2 * 1024; //* 1024;
    
    private Logger log = Logger.getLogger(LogTask.class);

    private boolean upLoadTodayLog = false;
    /**
     * {@inheritDoc}
     * 
     * @see java.util.TimerTask#run()
     */
    @Override
    public void run() {
        // 获取4A审计日志调度开关
        try {
            String  A4_switch = UtilProperties.getProperty(propertieName, "A4_switch","0");
            if ("0".equals(A4_switch))return;
        } catch (Exception ex) {
            throw new RuntimeException("Error reading 4ALog.properties file. "
                    + ex.getMessage(), ex);
        }
        Date startDate = null;
        List<String> list = null;
      
        	try {
        		if(this.upLoadTodayLog==true){
        			//上传当天的日记
        			startDate = new SimpleDateFormat("yyyy-MM-dd").parse(DateUtil.getToday());
        			list = a4LogDao.query(startDate,DateUtil.nextDate(startDate, 1));
        		}else{
        			Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(DateUtil.getToday());
        			// 凌晨1点开始上传昨天的日志    精确到天的时间 2013-10-22
        			startDate = DateUtil.nextDate(endDate, -1);
        			list = a4LogDao.query(startDate, endDate);
        		}
				
			} catch (ParseException e) {
			}
    
        
        if(list==null || list.size()==0)return;
        
        // 日志文件名定义
       
        String realPath = this.getClass().getClassLoader().getResource(
        		File.separator).getFile();
        String filePath = realPath + File.separator + "4ALog";
        
        File fileDir = new File(filePath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String fileDate = DateUtil.date2String(startDate, DateUtil.YYYYMMDD);
        int index = 1;
        File file = new File(filePath + File.separator + "4A." + fileDate
                + "_"+index + ".log");

        FileOutputStream fos = null;
        try {
            // 写文件头
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version='1.0' encoding='UTF-8'?>\n");
            sb.append("<ROOT>\n");
            fos = new FileOutputStream(file);
            fos.write(sb.toString().getBytes(charset));
           
           
           for (String jsonStr : list) {
        	   JSONObject jsonObj = new JSONObject(jsonStr);

                // 当写日志文件超过1M时,写文件尾,上传到FTP服务器,并新写入另一个日志文件,文件号自加1
                if (file.length() >= size - 1000) {
                    fos.write("</ROOT>".getBytes(charset));
                    fos.flush();
                    fos.close();
                    this.uploadFile(file);
                    index = index + 1;
                    file = new File(filePath + File.separator + "4A."
                            + fileDate+ "_"+ index + ".log");
                    fos = new FileOutputStream(file);
                    fos.write(sb.toString().getBytes(charset));
                }

                String identity_name = "JTNGBAM";
                String resource_code = "JTNGBAM";
                String module_id = "";
                String module_name = "";
                // 写日志文件内容
                StringBuffer log = new StringBuffer();
                log.append("<LOG4A>\n  ");
                log.append("<RESOURCE_KIND>").append("1").append("</RESOURCE_KIND>\n  ");
                log.append("<IDR_CREATION_TIME>").append( DateUtil.date2String(new Date(),
                            DateUtil.YYYY_MM_DD_HH_MM_SS)).append("</IDR_CREATION_TIME>\n  ");
				log.append("<IDENTITY_NAME>").append(identity_name).append("</IDENTITY_NAME>\n  ");
                log.append("<RESOURCE_CODE>").append(resource_code).append("</RESOURCE_CODE>\n  ");
                
                log.append("<MAIN_ACCOUNT_NAME>").append(jsonObj.getString("main_account_name")).append("</MAIN_ACCOUNT_NAME>\n  ");
                log.append("<SUB_ACCOUNT_NAME>").append(jsonObj.getString("sub_account_name")).append("</SUB_ACCOUNT_NAME>\n  ");
                log.append("<OPERATE_TIME>").append(jsonObj.getString("operate_time")).append("</OPERATE_TIME>\n  ");
                log.append("<OP_TYPE_NAME>").append(jsonObj.getString("op_type_name")).append("</OP_TYPE_NAME>\n  ");
                log.append("<OP_TYPE_ID>").append(jsonObj.getString("op_type_id")).append("</OP_TYPE_ID>\n  ");
                log.append("<OP_LEVEL_ID>").append(jsonObj.getString("op_level_id")).append("</OP_LEVEL_ID>\n  ");
                log.append("<OPERATE_RESULT>").append(jsonObj.getString("operate_result")).append("</OPERATE_RESULT>\n  ");
               
                
             
				log.append("<MODULE_ID>").append(module_id).append("</MODULE_ID>\n  ");
                log.append("<MODULE_NAME>").append(module_name).append("</MODULE_NAME>\n  ");
                
                log.append("<OPERATE_CONTENT>").append(jsonObj.getString("operate_content")).append("</OPERATE_CONTENT>\n  ");
                log.append("<OBJECT_ID>").append("").append("</OBJECT_ID>\n  ");
                log.append("<OBJECT_NAME>").append("").append("</OBJECT_NAME>\n   ");
                log.append("<CLIENT_NETWORK_ADDRESS>").append(jsonObj.getString("client_address")).append("</CLIENT_NETWORK_ADDRESS>\n  ");
                log.append("<CLIENT_ADDRESS>").append(jsonObj.getString("client_address")).append("</CLIENT_ADDRESS>\n  ");
                log.append("<SERVER_ADDRESS>").append(jsonObj.getString("server_address")).append("</SERVER_ADDRESS>\n  ");
               
                log.append("<CLIENT_CPU_SERIAL>").append("").append("</CLIENT_CPU_SERIAL>\n  ");
                log.append("<CLIENT_PORT>").append("").append("</CLIENT_PORT>\n  ");
                log.append("<CLIENT_MAC>").append("").append("</CLIENT_MAC>\n  ");
                log.append("<SERVER_PORT>").append("").append("</SERVER_PORT>\n  ");
                log.append("<SERVER_MAC>").append("").append("</SERVER_MAC>\n  ");
                log.append("<SERVER_MAC>").append("").append("</SERVER_MAC>\n  ");
                
                log.append("<BANKAPPROVE>").append("").append("</BANKAPPROVE>\n  ");
                log.append("<BANKFLAG>").append("").append("</BANKFLAG>\n  ");
                /*   
                log.append("<DB_INFO_CODE>").append(db_info_code).append("</DB_INFO_CODE>\n  ");
                log.append("<SQL_TABLES>").append(jsonObj.getString("sql_tables")).append("</SQL_TABLES>\n  ");
                log.append("<SQL_WHERE_CONDITIONS>").append(jsonObj.getString("sql_where_conditions")).append("</SQL_WHERE_CONDITIONS>\n  ");
                log.append("<TASK_CODE>").append("").append("</TASK_CODE>\n  ");
               
                */
                log.append("</LOG4A>\n");
                
                fos.write(log.toString().getBytes(charset));
                fos.flush();
            }
            fos.write("</ROOT>".getBytes(charset));
            fos.flush();
            fos.close();
            this.uploadFile(file);
        } catch (Exception e) {
        	log.info("log:"+file.getPath()+" upload fail");
            throw new RuntimeException("Connect to the database error. "
                    + e.getMessage(), e);
        } 
    }

    /**
     * Description: 上传文件到FTP服务器
     * 
     * @param file
     *        上传文件
     */
    private  void uploadFile(File file) {
    	log.info("start upload log:"+file.getPath());
        String ftpServer = "";
        String ftpUser = "";
        String ftpPwd = "";
        int ftpPort = 21;
        try {
            ftpServer = UtilProperties.getProperty(propertieName, "4a_ftp_server");
            ftpUser = UtilProperties.getProperty(propertieName, "ftp_user");
            ftpPwd = UtilProperties.getProperty(propertieName, "ftp_password");
            ftpPort = Integer.parseInt( UtilProperties.getProperty(propertieName, "ftp_port"));
        } catch (Exception ex) {
        	log.info("fail to read  4ALog.properties");
            throw new RuntimeException("Error reading configuration file. "+ ex.getMessage(), ex);
        }
        // 连接FTP服务器
        FtpClient ftpClient = new FtpClient();
        try {
            ftpClient.openServer(ftpServer, ftpPort);
            ftpClient.login(ftpUser, ftpPwd);
            ftpClient.cd("/home/db2/gather_file/bam_remote/up"); 
//            ftpClient.cd("/home/smc/tomcat6smc/logs"); 
            log.info("上传ftp目录:\"" + ftpClient.pwd());  
            ftpClient.binary();
        } catch (IOException e) {
            throw new RuntimeException("FTP server connection failed. "+ e.getMessage(), e);
        }
        // 上传文件
        TelnetOutputStream tos = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            String fileName = file.getName();
            tos = ftpClient.put(fileName);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = fis.read(bytes)) != -1) {
                tos.write(bytes, 0, c);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException("File is not exist. " + e.getMessage(),e);
        } catch (IOException e) {
            throw new RuntimeException("Error uploading files. " + e.getMessage(), e);
        } finally {
            try {
                if (null != tos) tos.close();
                if (null != fis) fis.close();
                log.info("upload success file:"+file.getPath());
            } catch (IOException ex) {
                throw new RuntimeException(
                    "An error occurred while the stream is closed. " + ex.getMessage(), ex);
            }
        }
    }

	public void setUpLoadTodayLog(boolean upLoadTodayLog) {
		this.upLoadTodayLog = upLoadTodayLog;
	}

}

 

 

 A4LogDao .java

 

package org.jasig.cas.a4.tools;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class A4LogDao extends JdbcDaoSupport {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	/*
	 * public static Map getMap() { HashMap<String, String> paraMap = new
	 * HashMap<String, String>(); paraMap.put("main_account_name",
	 * "test_wlt");// 主账号 paraMap.put("sub_account_name", "OPERATORID");// 从账号
	 * paraMap.put("op_type_id", "操作类型编码");// 操作类型编码 paraMap.put("op_type_name",
	 * "op_type_name");// 操作类型名称 用户登录 paraMap.put("op_level_id", "3");// 级别
	 * paraMap.put("operate_result", "1");// 操作结果 0-成功
	 * paraMap.put("operate_time", DateUtil.date2String(new Date(),
	 * "YYYY-MM-DD HH:mm:ss"));// 操作时间
	 * 
	 * paraMap.put("operate_content", "操作内容描述");// paraMap.put("object_id",
	 * "1-文件、2-库表、3-进程、4-菜单");// paraMap.put("object_name", "对象类型名称 汉字");//
	 * 
	 * paraMap.put("client_address", "127.0.0.1");//
	 * paraMap.put("server_address", "服务器ip");// paraMap.put("sql_tables",
	 * "该操作涉及的表名集合 表名1||表名2");// paraMap.put("sql_where_conditions",
	 * "例如:表名1.条件字段1=‘参数1’||表名2.条件字段1=‘参数2’");// return paraMap; } // 主方法,进行测试
	 * public static void main(String[] args) { A4LogDao dao = new A4LogDao();
	 * Date startDate = DateUtil.nextHour(new Date(), -1000);
	 * dao.insert(getMap());
	 * 
	 * }
	 */

	private String insert_sql;

	private String select_sql;

	public int insert(final Map<String, String> paraMap) {
		final String logMsg = new JSONObject(paraMap).toString();
		log.info("insert into logMsg:" + logMsg);
		log.info("insert_sql:" + insert_sql);

		int i =-1;
		try {
			i = super.getJdbcTemplate().update(insert_sql,
					new PreparedStatementSetter() {

						@Override
						public void setValues(PreparedStatement ps)
								throws SQLException {
							ps.setString(1, paraMap.get("main_account_name"));
							ps.setString(2, paraMap.get("op_level_id"));
							ps.setString(3, logMsg);
							Timestamp date = new Timestamp(new Date().getTime());
							// Timestamp date = new Timestamp(DateUtil.nextDate(
							// new Date(),
							// -4).getTime());

							ps.setTimestamp(4, date);
						}
					});
		} catch (Exception e) {
			log.info(e.getMessage());
			log.info(e+" "+e.getLocalizedMessage());
			e.getStackTrace();
			log.info("insert log  fail");
			log.info("fail logMsg:"+logMsg);
		}

		return i;

	}

	public List<String> query(Date startDate, Date endDate) {
		List<String> dataList = new ArrayList<String>();

		Timestamp date1 = new Timestamp(startDate.getTime());
		Timestamp date2 = new Timestamp(endDate.getTime());

		dataList = (List<String>) super.getJdbcTemplate().queryForList(
				select_sql, new Object[] { date1, date2 }, String.class);

		return dataList;
	}

	public void setInsert_sql(String insert_sql) {
		this.insert_sql = insert_sql;
	}

	public void setSelect_sql(String select_sql) {
		this.select_sql = select_sql;
	}

}

 调用测试

每天凌晨1点上传昨天的日志
------------------------------------------------------------------
Timer timer = new Timer();
	    	LogTask task = new LogTask();
	    	LogTask.a4LogDao = a4LogDao;
	    	 try {
				Date today = new SimpleDateFormat("yyyy-MM-dd").parse(DateUtil.getToday());//精确到天的时间 2013-10-22
	    		Date startTask = DateUtil.nextHour(today, 25);//每天凌晨1点上传昨天的日志
				long step = 1*24*60*60*1000; //时间间隔为24小时
	    		timer.schedule(task, startTask, step);
			} catch (ParseException e) {
			}
----------------------------------------------------------------------
		//上传当天日志
	
			// if("OFF".equals(A4switch)) return;
		     	Timer timer = new Timer();
		    	LogTask task = new LogTask();
		    	task.setUpLoadTodayLog(true);
		    	LogTask.a4LogDao = a4LogDao;
			timer.schedule(task,  new Date());
	

 

 

你可能感兴趣的:(ftp)