//FFP初始化连接
public static void init(){
Properties config = new Properties();
try{
System.out.println("访问ftp.properties");
//读取配置文件
InputStream inputStream = FTPUtils.class.getClassLoader().getResourceAsStream("/ftp.properties");
config.load(inputStream);
//FTP服务器IP
serverIP = config.getProperty("ftp.serverName");
//FTP服务器端口
port = config.getProperty("ftp.port");
//FTP服务器用户名
userName = config.getProperty("ftp.userName");
//FTP服务器密码
password = config.getProperty("ftp.password");
//服务器名称。(这里组装的文件名,如:report_2013_02_01.csv)
FTPConst.REMORT_FILE_NAME = config.getProperty("ftp.reportDateFile")+getFtpDay()+".csv";
FTPConst.LOCAL_FILE_NAME = config.getProperty("local.reportDateFile")+getFtpDay()+".csv";
FTPConst.FTP_REPORT_FLOW_BROWSERS_FILE_NAME = config.getProperty("ftp.browsersMonthFile")+getFtpMonth()+".csv";
FTPConst.LOCAL_REPORT_FLOW_BROWSERS_FILE_NAME = config.getProperty("local.BrowsersMonthFile")+getFtpMonth()+".csv";
}catch(Exception e){
e.printStackTrace();
logger.error("ftp.properties加载失败:",e);
}
}
/**
* 获得BROWSERS报表的名称拼接信息,获得前一个月。如:现在2月。获得字符串为2012-01
* @return
*/
public static String getFtpMonth(){
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String dateStr = sdf.format(cal.getTime());
return dateStr;
}
/**
* 获得每日OMNITURE报表的名称拼接信息,获得前一日。如:现在为2013年02月22日。获得字符串为2013年02月21日
* @return
*/
public static String getFtpDay(){
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(cal.getTime());
// System.out.println(dateStr);
return dateStr;
}
/**
* 将FTP服务器文件
下载到本地,返回本地文件。
* @param remoteFileName
* @param localFileName
* @return
*/
public static synchronized String downloadFile(String remoteFileName, String localFileName){
try{
File localFile = new File(localFileName);
//如果文件不存在。则创建此文件
if(!localFile.exists()){
localFile.createNewFile();
}else{
logger.info("创建文件已存在");
return localFileName;
}
OutputStream output = new FileOutputStream(localFile);
FTPClient client = new FTPClient();
client.connect(serverIP);
client.login(userName, password);
boolean flag = client.retrieveFile(remoteFileName, output);
client.logout();
if(flag){
return localFileName;
}else{
return null;
}
}catch(Exception e){
e.printStackTrace();
logger.error("omniture数据下载错误:",e);