JAVA学习(一)__操作FTP

时间:2021-09-24 14:10:50
//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);
return null;
}
}

/**
* 利用javacsv解析csv文件
* @param file
* @return
*/
private List<OmnitureReportBean> javaCsv(String file){
try{
List<OmnitureReportBean> omnitureList = new ArrayList<OmnitureReportBean>();
CsvReader reader = new CsvReader(file, ',', Charset.forName("UTF-8"));
int i = 0;
while (reader.readRecord())
{
//跳过没用的行数。
if(i++ <= 15){
String[] str = reader.getValues();
System.out.println("数字编号:"+ (i-1) + str[0]);
continue;
}

//读取每行数据以数组形式返回
String[] str = reader.getValues();

OmnitureReportBean omniture = new OmnitureReportBean();
String timeStr = file.trim().substring(21, 28);
Date date = smipleDateFormat.parse(timeStr);
omniture.setStartTime(date);
omniture.setEndTime(date);
omniture.setBrowsers(str[0]);
omniture.setVisits(new Integer(str[1].replace(",", "").trim()));
omniture.setSearches(new Integer(str[2].replace(",", "").trim()));
omniture.setCardAdd(new Integer(str[3].replace(",", "").trim()));
omniture.setCheckOuts(new Integer(str[4].replace(",", "").trim()));
omniture.setPament(new Integer(str[5].replace(",", "").trim()));
omniture.setOrders(new Integer(str[6].replace(",", "").trim()));
omniture.setType(2);
omnitureList.add(omniture);
List<OmnitureReportBean> list = this.omnitureDao.findOmnitureMonthList(omniture.getStartTime(),omniture.getBrowsers());
//若表里面已有该时间段的数据,则不重复插入本次csv中的数据
if(list!=null&&list.size()>0){
continue;
}else{
omnitureDao.saveOmniture(omniture);
}
}
return omnitureList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}

/**
* 与当前日期比较。如早于当前时间返回true.否而返回false
* @param endDate
* @return
*/
public static boolean compareToDate(Date date){
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
Date nowDate = new Date();
cal1.setTime(date);
cal1.add(Calendar.HOUR_OF_DAY, 00);
cal1.add(Calendar.MINUTE, 00);
cal1.add(Calendar.SECOND, 00);
cal2.setTime(nowDate);
cal2.add(Calendar.HOUR_OF_DAY, 00);
cal2.add(Calendar.MINUTE, 00);
cal2.add(Calendar.SECOND, 00);
boolean flag = cal1.before(cal2);
if(flag){
//早于当前系统时间
return true;

}else{
//晚于当前系统时间
return false;
}
}
/**
* 删除服务器上的文件
* @param path
* @return
* @throws IOException
*/
public boolean delFile(String path) throws IOException {
boolean flag = false;
FTPClient client = new FTPClient();
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");

String dateString = formatter.format(currentTime);
String fileName = "Report.csv";
try {
client.connect(serverIP);
client.login(userName, password);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
client.changeWorkingDirectory(path);
client.deleteFile(path + fileName);
flag = true;
logger.info("file:[" + path + fileName + "] is deleted on ftp:" + serverIP);
} catch (Exception e) {
flag = false;
} finally {
try {
client.disconnect();
} catch (IOException e) {
logger.info("disconnect FTP failed:" + e.getMessage());
}
}
return flag;

}