package com.eastlsoft;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import oracle.sql.DATE;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;
public class FtpTool {
private String ip = Configuration.getIp();// ftp服务器的IP地址
private String username = Configuration.getUsername();// 用户名
private String password = Configuration.getPassword();// 密码
private int port = Integer.parseInt(Configuration.getPort());// 默认端口
private String dirPath=Configuration.getDownFilePath();
FtpClient ftpclient = null;
FileOutputStream os = null;
FileInputStream is = null;
/**
* 比较哪些文件需要下载
* 循环下载时 如果本地已存在 则不再下载
* 此方法目的是防止有些文件没有被下载 和下载中断
* @param localPath
* @param remotePath
*/
public ArrayList compore(String remotePath){
ArrayList unLoadList = new ArrayList();
ORSQL orsql=new ORSQL();
List<String> arr=orsql.readDirAllFile(dirPath);
try {
ArrayList<String> list = getFileList(remotePath);
for (int i = 0; i < list.size(); i++) {
String ftpfileName=this.getFileName(list
.get(i).toString());
boolean mark = false;//默认没有下载过
for(int j = 0;j<arr.size();j++){
String fileName = arr.get(j).substring(Configuration.getDownFilePath().length()+1);
FtpClient fc = new FtpClient(ip);
fc.login(username, password);
fc.binary();
//服务器文件的路径 可配置
File fileFtp = new File("C:/ftp/a/"+ftpfileName);
System.out.println("fileFtp............"+fileFtp.length());
long ftpSize = fileFtp.length();
fc.closeServer();
File fileLocal = new File(arr.get(j));
System.out.println("fileLocal............"+fileLocal.length());
if(ftpfileName.equals(fileName)&&ftpSize==fileLocal.length()){//如果相等 说明下载过
mark = true;
break;
}
System.out.println("输出我。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
}
if(mark == false){
unLoadList.add(list.get(i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("unLoadList"+unLoadList.size());
return unLoadList;
}
public List processdownload(String localPath, String remotePath) {
ArrayList list = null;
try {
ftpclient.binary();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
//list = getFileList(remotePath);
list=compore(remotePath);
System.out.println("ftp上文件的个数:"+list.size());
File temp = null;
for (int i = 0; i < list.size(); i++) {
FileOutputStream os = null;
System.out.println("fffffffffff:"+this.getFileName(list
.get(i).toString()));
TelnetInputStream is = ftpclient.get(this.getFileName(list
.get(i).toString()));
System.out.println("download");
temp = new File(localPath+"/"+this.getFileName(list
.get(i).toString()));
os = new FileOutputStream(temp);
//BufferedReader fis=new BufferedReader(new FileReader(new InputStreamReader(arg0)));
// byte[] bytes = new byte[1024];
// int c=0;
// while ((c = is.read(bytes)) != -1) {
// os.write(bytes, 0, c);
// }
int b=0;
while((b=is.read())!=-1){
os.write(b);
}
os.flush();
}
} catch (IOException ex) {
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}
public String getFileName(String line) {
String filename = (String) parseLine(line).get(8);
return filename;
}
public ArrayList getFileList(String remotePath) throws IOException {
ftpclient.cd(remotePath);
BufferedReader dr = new BufferedReader(new InputStreamReader(ftpclient
.list()));
ArrayList al = new ArrayList();
String s = "";
while ((s = dr.readLine()) != null) {
if ((!((String) parseLine(s).get(8)).equals("."))
&& (!((String) parseLine(s).get(8)).equals(".."))) {
////////////////////////////////////
/**
* 1.匹配所有此文件夹中命名格式为
* useryyyymmddhhmmxxxnnnn.ttt,
* yyyymmddhhmm为批文件产生的年月日时分。
* xxx为省分代码。
* nnnn 是4位序列号,每天从“0001”开始。
* ttt表示文件类型,req表示请求.
*/
//得到文件名称
String fileName=this.getFileName(s);
System.out.println("fileName这是下载时候的文件名字"+fileName);
//从配置文件中得到相应的条件供查询使用
String username="user";
String type="txt";
//得到配置中的省份列表
String provinceList=Configuration.getProvinceList();
String[] provices=provinceList.split(",");
System.out.println("省份个数"+provices.length);
//得到配置中读取时间
int rizhi=Integer.parseInt(Configuration.getRiZhi());
String[] gh=fileName.split("\\.");
//筛选符合条件的文件
//1.暂时不开启接口(传值:省份)
//2.设置时间区间,在此区间的符合条件的文件下载下来,时间类型(yyyymmddhhmm每天4点的时候进行更新)
//只需将此时的时间向前推移一天(多读取一定的区间也就是昨天4点后今天6点前)
//获得系统当前日期
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddhh");
System.out.println("下载时截取文件时间"+gh[0]);
for (int i = 0; i < provices.length; i++) {
if(gh[0].length()==23){
//截取user
String suser=fileName.substring(0, 4);
//截取省份代码
String sprovince=fileName.substring(16, 19);
//截取时间yyyymmddhh
String fileTime=fileName.substring(4, 14);
//转换成Date类型
try {
Date date = dateformat1.parse(fileTime);
//文件生成的时间
long mfileTime=date.getTime();
//程序调用的时间
long a1Time=new Date().getTime();
//配置文件读取设置上次文件生成时间
long firatTime=a1Time-rizhi*60*60*1000;
//如果满足条件 则添加到ArrayList中
if(type.equals(gh[1])&&username.equals(suser)&&provices[i].equals(sprovince)&&mfileTime<a1Time&&mfileTime>firatTime){
System.out.println("dddddddddddddddddddddddddddddddd");
al.add(s);//过滤后的文件
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
}
return al;
}
private ArrayList parseLine(String line) {
ArrayList s1 = new ArrayList();
StringTokenizer st = new StringTokenizer(line, " "
没有合适的资源?快使用搜索试试~ 我知道了~
从Ftp服务器定时下载文件 并对文件解析
共46个文件
jar:20个
class:5个
java:5个
5星 · 超过95%的资源 需积分: 35 95 下载量 54 浏览量
2013-11-21
15:08:29
上传
评论 1
收藏 8.05MB RAR 举报
温馨提示
从Ftp服务器定时下载文件 并对文件解析 其中用到数据库 可以自己根据需求配置
资源推荐
资源详情
资源评论
收起资源包目录
TimerTest.rar (46个子文件)
TimerTest
.project 2KB
.mymetadata 300B
src
config.properties 190B
applicationContext.xml 1KB
com
eastlsoft
Timer.java 1009B
Configuration.java 2KB
ReMapInsertOracle.java 4KB
ORSQL.java 7KB
FtpTool.java 9KB
ftpdownload
WebRoot
WEB-INF
classes
config.properties 190B
applicationContext.xml 1KB
com
eastlsoft
FtpTool.class 8KB
ORSQL.class 6KB
Configuration.class 3KB
ReMapInsertOracle.class 5KB
Timer.class 1KB
ftpdownload
lib
commons-lang3-3.1.jar 308KB
spring-aop-3.2.2.RELEASE.jar 328KB
jdom.jar 124KB
spring-jdbc-3.2.2.RELEASE.jar 396KB
spring-web-3.0.5.RELEASE.jar 386KB
evarj2ee.jar 1.33MB
spring-context-3.2.2.RELEASE.jar 842KB
spring-tx-3.2.2.RELEASE.jar 236KB
commons-logging-1.1.1.jar 59KB
spring-core-3.2.2.RELEASE.jar 846KB
spring-expression-3.2.2.RELEASE.jar 191KB
commons-io-2.0.1.jar 156KB
jsch-0.1.50.jar 243KB
xercesImpl-2.6.2.jar 987KB
aopalliance-1.0.jar 4KB
common-annotations.jar 6KB
ftp.jar 40KB
spring-beans-3.2.2.RELEASE.jar 593KB
ojdbc14.jar 1.47MB
commons-collections-3.2.1.jar 562KB
web.xml 684B
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 465B
.jsdtscope 500B
.classpath 2KB
共 46 条
- 1
资源评论
- LaBiXiaoXinXin5212017-02-24多谢分享值得学习,能构建一个maven项目就更好了
- haust022016-08-10谢谢啦!推荐下载。
- cuiping51222018-05-17还得调试jdk1.8的兼容性
- 曹老师1232018-03-24推荐下载,还不错
- agzilfkczp2017-10-25谢谢啦!推荐下载。
angel豆蔻年华
- 粉丝: 30
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
- 贪心算法介绍及代码示例讲解
- CR13SP35MSI64 Crystal 水晶报表运行组件最后版本64位
- 图像分类数据集:玉米叶是否感染分类数据集(2分类,包含训练集、验证集)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功