设为首页 收藏本站
查看: 1225|回复: 0

[经验分享] JAVA学习(一)__操作FTP

[复制链接]
累计签到:29 天
连续签到:1 天
发表于 2015-11-6 11:07:44 | 显示全部楼层 |阅读模式
//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(&quot;UTF-8&quot;));
int i = 0;
while (reader.readRecord())   
{  
//跳过没用的行数。
if(i++ <= 15){
String[] str = reader.getValues();
System.out.println(&quot;数字编号:&quot;+ (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(&quot;,&quot;, &quot;&quot;).trim()));   
omniture.setSearches(new Integer(str[2].replace(&quot;,&quot;, &quot;&quot;).trim()));
omniture.setCardAdd(new Integer(str[3].replace(&quot;,&quot;, &quot;&quot;).trim()));
omniture.setCheckOuts(new Integer(str[4].replace(&quot;,&quot;, &quot;&quot;).trim()));
omniture.setPament(new Integer(str[5].replace(&quot;,&quot;, &quot;&quot;).trim()));
omniture.setOrders(new Integer(str[6].replace(&quot;,&quot;, &quot;&quot;).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(&quot;yyMMdd&quot;);
String dateString = formatter.format(currentTime);
String fileName = &quot;Report.csv&quot;;
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(&quot;file:[&quot; + path + fileName + &quot;] is deleted on ftp:&quot; + serverIP);
} catch (Exception e) {
flag = false;
} finally {
try {
client.disconnect();
} catch (IOException e) {
logger.info(&quot;disconnect FTP failed:&quot; + e.getMessage());
}
}
return flag;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-135824-1-1.html 上篇帖子: 通过quartz定时任务完成对FTP服务器上文档的下载 下篇帖子: 用Delphi做了个从FTP服务器更新的程序,把遇到的一些小问题在这儿,以备查用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表