import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
/**
* @author 李怀北
*
* Discription:下载FTP上的文件
*
*/
public class DownLoadFTPFile implements Job {
/**
* @param start
* @param end
*/
public void DownLoad(String start, String end) {
HashMap detailsMap = null;//存储某个人每天的信息
//获取属性文件
Properties p = null;
try {
InputStream is = getClass().getResourceAsStream(
"../properties/setMessageServerPath.properties");
p = new Properties();
p.load(is);
} catch (IOException ex) {
System.err.println("无法打开属性配置文件!");
ex.printStackTrace();
}
//获取配置信息
String userName = p.getProperty("userName");
String passWord = p.getProperty("passWord");
String IPAdd = p.getProperty("IPAdd");
String filePath = p.getProperty("filePath");
String downLoadPath = p.getProperty("downLoadPath");
/**
* 获取所有的参加比武人员对象的集合
*/
OperateDB objOD = OperateDB.getInstance();
List compareList = objOD.getComObjList("WHERE isornot = '1'");
/* FTP操作 */
FTPClient ftp = new FTPClient();
//存储某一时段所有的原文件和修改后的文件
ArrayList sameFiles = null;
FTPFile[] ftpfiles = null;
File localFile = null;
String[] localFileNames = null;
ArrayList date = new ArrayList();
try {
ftp.connect(IPAdd);
ftp.login(userName, passWord);
//根据FTP文件路径获取所有的文件
ftpfiles = ftp.listFiles(filePath);
//获取本地所有文件的名称
localFile = new File(downLoadPath);
localFileNames = localFile.list();
//进行比较把本地没有而FTP上有的日期存储进数组
int index = 0;
//从2开始,因为. .. 这样的目录问题
for (int i = 2; i < ftpfiles.length; i++) {
if (ftpfiles[i] != null) {
int no = 1;
for (int j = 0; j < localFileNames.length; j++) {
//两个时段只要有一个这一天就算
if (ftpfiles[i].getName().toString().substring(2, 8)
.equals(
localFileNames[j].toString().substring(
2, 8))) {
no = 0;
break;
}
}
if (1 == no
&& !date.contains(ftpfiles[i].getName().substring(
2, 8))) {//如果本地没有某一天06/21的文件的话就把这一天存储
int nodate = 1;
//两个时段只添加一天就可以
for (int k = 0; k < date.size(); k++) {
if (date.get(k).toString().substring(0, 4).equals(
ftpfiles[i].getName().substring(2, 6))) {
nodate = 0;
break;
}
}
if (nodate == 1) {
date.add(ftpfiles[i].getName().substring(2, 8));
index++;
}
}
}
}
} catch (Exception e) {
System.out.println(e + "--获取没有的报文的日期出错!");
} finally {
try {
ftp.quit();
ftp.disconnect();
} catch (IOException e1) {
System.out.println("IOException1" + e1.toString());
}
}
for (int e = 0; e < date.size(); e++) {//天数循环
for (int j = 0; j < compareList.size(); j++) {//人数循环
sameFiles = new ArrayList();
String fileName = "";
detailsMap = new HashMap();
detailsMap.put("name", ((CompareNames) compareList.get(j))
.getRaleName());//第一个元素是人名
/**
* 获取修正后的文件的名称(文件就只上传一次就得到这个文件的名称)
*/
String year = "";
String month = date.get(e).toString().substring(0, 2);
String day = date.get(e).toString().substring(2, 4);
String personNum = ((CompareNames) compareList.get(j))
.getNumber();//人员的编号
String fileNamePart02 = "PF" + month + day + "02" + personNum;
String fileNamePart06 = "PF" + month + day + "06" + personNum;
String fileNamePart21 = "PF" + month + day + "21" + personNum;//所有修改后的文件包含的相同部分
if (ftpfiles.length > 0) {//如果下载的时候没有下载成功,需要第二次下载就要全部进行筛选
for (int i = 0; i < ftpfiles.length; i++) {
try {
String namePart = ftpfiles[i].getName().substring(
0, 10);
String nameLast = ftpfiles[i].getName().substring(
13, 16);
//如果文件名包含这个人这段时间(06或21两个时段)
if ((namePart.equals(fileNamePart06) || namePart
.equals(fileNamePart21) || namePart
.equals(fileNamePart02))
&& (nameLast.equals("FSE"))) {
sameFiles.add(ftpfiles[i].getName());
}
} catch (Exception e4) {
// System.out.println(""."和”..“的时候会报超出范围的错误这里捕捉");
}
}
} else {
//这个时段没有文件
System.out.println("一个文件也没有获取!");
}
//寻找最后修改的文件版本,并给本为“”的文件名赋值
if (sameFiles.size() > 0) {
int last = Integer.parseInt(sameFiles.get(0).toString()
.substring(10, 12));
fileName = sameFiles.get(0).toString();
for (int i = 0; i < sameFiles.size(); i++) {
if (last < Integer.parseInt(sameFiles.get(i).toString()
.substring(10, 12))) {
fileName = sameFiles.get(i).toString();
last = Integer.parseInt(sameFiles.get(i).toString()
.substring(10, 12));
}
}
} else {
fileName = "";
}
if (fileName != "") {
URL url = null;
try {
url = new URL("ftp://" + userName + ":" + passWord
+ "@" + IPAdd + "/" + filePath + fileName);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
URLConnection urlconn = null;
try {
urlconn = url.openConnection();
} catch (IOException e2) {
e2.printStackTrace();
}
BufferedReader br = null;
InputStream inS = null;
try {
inS = urlconn.getInputStream();
} catch (IOException e3) {
e3.printStackTrace();
}
br = new BufferedReader(new InputStreamReader(inS));
String line;
String allContent = "";
try {
while (null != (line = br.readLine())) {
allContent += line.trim() + " "; //设法把字符串设置的符合一个空格分隔两个内容
}
} catch (IOException e4) {
e4.printStackTrace();
}
//关闭资源
try {
br.close();
inS.close();
} catch (IOException e5) {
e5.printStackTrace();
}
urlconn = null;
url = null;
try {
//生成文件的目录
File Path = new File(downLoadPath);
Path.mkdir();
//创建一个文件
String downPath = downLoadPath;
String filename = fileName;
new File(downPath, filename);
//把Writer变为输出的内容
FileOutputStream outf = new FileOutputStream(downPath
+ filename);
BufferedOutputStream bufferout = new BufferedOutputStream(
outf);
byte b[] = allContent.getBytes();//fileContent
bufferout.write(b);
bufferout.flush();
bufferout.close();
SimpleDateFormat nowsdf = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
System.out.println(nowsdf.format(new Date()) + fileName
+ " 自动下载FTP文成功结束!");
} catch (Exception e1) {
System.out.println("错误:" + e1.toString());
}
}
}
}
System.out.println(new Date() + " 自动下载FTP报文完成!");
}
public static void main(String[] args) {
DownLoadFTPFile obj = new DownLoadFTPFile();
// 获取属性文件
Properties p = null;
try {
InputStream is = obj.getClass().getResourceAsStream(
"../properties/setMessageServerPath.properties");
p = new Properties();
p.load(is);
} catch (IOException ex) {
System.err.println("无法打开属性配置文件!");
ex.printStackTrace();
}
//获取配置信息
String start = p.getProperty("startDate");
String end = p.getProperty("endDate");
/**
* 处理日期
*/
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Date EndDate = null;
// try {
// EndDate = sdf.parse(end.trim());
// } catch (Exception e) {
// e.printStackTrace();
// }
// Calendar cal = Calendar.getInstance();
// cal.setTime(new Date());
// cal.add(Calendar.DAY_OF_YEAR, -2);
//if (cal.getTime().getTime() < EndDate.getTime()) {
obj.DownLoad(start, end);
// }
}
/*
* (非 Javadoc)
*
* @see com.shuangmeng.quartz.Job#execute(com.shuangmeng.quartz.JobExecutionContext)
*/
public void execute(JobExecutionContext arg0) throws JobExecutionException {
DownLoadFTPFile obj = new DownLoadFTPFile();
// 获取属性文件
Properties p = null;
try {
InputStream is = getClass().getResourceAsStream(
"../properties/setMessageServerPath.properties");
p = new Properties();
p.load(is);
} catch (IOException ex) {
System.err.println("无法打开属性配置文件!");
ex.printStackTrace();
}
//获取配置信息(不用担心年,根本就用不到年)
String start = p.getProperty("startDate");
String end = p.getProperty("endDate");
/**
* 处理日期,(目的是不比武的时不下载但是现在一年都在比武)
*/
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// Date EndDate = null;
// try {
// EndDate = sdf.parse(end.trim());
// } catch (Exception e) {
// e.printStackTrace();
// }
// Calendar cal = Calendar.getInstance();
// cal.setTime(new Date());
// cal.add(Calendar.DAY_OF_MONTH, -2);
// if (cal.getTime().getTime() < EndDate.getTime()) {
obj.DownLoad(start, end);
// }
}
}