纯JAVA程序开发的一款,文件上传FTP处理程序异常关闭问题 使用upLoadFromProduction()读取本地文件夹的内容进行上传目标,打包好的程序异常关闭,有异常信息么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个是一个方法源码:File directory = new File("FTPUpload.xml"); if (!directory.exists()) { try { XMLGenerate j2x = new XMLGenerate(); System.out.println("生成 mxl 文件..."); j2x.BuildXMLDoc(); } catch (Exception e) { e.printStackTrace(); } } // TODO Auto-generated method stub java.io.File file = new java.io.File(directory.getCanonicalPath()); // 创建一个读取XML文件的对象 SAXReader reader = new SAXReader(); // 创建一个文档对象 Document document; FTPRead read = new FTPRead(); document = reader.read(file); // 获取文件的根节点 Element element = document.getRootElement(); for (Iterator i = element.elementIterator("disk"); i.hasNext();) { // 获取节点元素 element = (Element) i.next(); String name = element.attributeValue("name"); // 存储本地文件路径 String Localpath = element.elementText("Localpath"); // 上传FTP目录 String ftppath = element.elementText("ftppath"); // 回传FTP IP地址 String FTPIP = element.elementText("FTPIP"); // Ftp端口号 int port = Integer.parseInt(element.elementText("port")); // FTP登录帐号 String username = element.elementText("username"); // FTP登录密码 String password = element.elementText("password"); // FTP回传数据报错反馈文件夹 String ftperrors = element.elementText("ftperrors"); // 本地存储FTP回传报错目录 String ftpLocal = element.elementText("ftpLocal"); // 上传报错信息 String ftpLocalerror = element.elementText("ftpLocalerror"); // 文件备份路径 String Localfilegx = element.elementText("Localfilegx"); // 文件备份路径 String Localfiletx = element.elementText("Localfiletx"); //FtpDownFiles fd = new FtpDownFiles(); //fd.connectServer(FTPIP, username, password); //fd.ftpDownFiles(ftperrors, ftpLocal); if (!(new File(Localpath).isDirectory())) { new File(Localpath).mkdir(); } if (!(new File(ftpLocal).isDirectory())) { new File(ftpLocal).mkdir(); } if (!(new File(Localfilegx).isDirectory())) { new File(Localfilegx).mkdir(); } if (!(new File(Localfiletx).isDirectory())) { new File(Localfiletx).mkdir(); } if (!(new File(ftpLocalerror).isDirectory())) { new File(ftpLocalerror).mkdir(); } // 获取配置目录下的.txt和.OK文件 List list = getTextFile(Localpath); TxTtoFtpaAtion log = new TxTtoFtpaAtion(); if(list.size()!=0) { log.log4j(name + ":准备开始上传FTP"); for (int j = 0; j < list.size(); j++) { if(j==list.size()-1) { Timer timer1 = new Timer(); MonitorTimerTask monitor=new MonitorTimerTask(); timer1.scheduleAtFixedRate(monitor, 0, 5*60*1000L); } //System.out.println(list.get(j)); // 获取txt的文件路径名 String filepath; // 获取txt的文件名 String filename; // 获取的文件路径名 String filepathok; // 获取的文件名 String filenameok; boolean flag; filepath = list.get(j).toString(); filename = filepath.substring(Localpath.length() + 1); // 获取当前txt文件所对应的.ok文件路径 filepathok = filepath.replace("txt", "ok"); // 获取当前txt文件所对应的.ok文件名 filenameok = filepathok.substring(Localpath.length() + 1); // 回传数据类别 String type = null; // 文件备份路径 String Localfile; // 区别种类name属性 String Category = ".txt"; String Category1 = ".txt"; String Category2 = ".txt"; if (filename.contains(Constant.LOGISTICS)) { type = name + "信息上传:"; } else if (filename.contains(Constant.SUCCESS)) { type = name + "信息上传:"; } else if (filename.contains(Constant.STAMPP)) { type = "信息上传:"; } else if (filename.contains(Constant.STAMPV)) { type = "信息上传:"; } else if (filename.contains(Constant.LOGISTICS)) { type = name + "物流信息上传:"; } else if (filename.contains(Constant.SUCCESS)) { type = name + "保单信息上传:"; } else if (filename.contains(Constant.REPORT)) { type = "清单生产信息上传:"; } if ("...".equals(name.trim())) { Category = Constant.SUCCESS; Category1 = Constant.LOGISTICS; Category2 = Constant.REPORT; Localfile = Localfilegx; } else if ("..".equals(name.trim())) { Category = Constant.STAMPP; Category1 = Constant.STAMPV; Category2 = Constant.STAMPP; Localfile = Localfilegx; } else { Category = ".txt"; Category1 = ".txt"; Category2 = ".txt"; Localfile = Localfiletx; } if (filename.contains(".txt") && (filename.contains(Category) || filename.contains(Category1) || filename .contains(Category2))) { // 获取当前读取的txt中有多少条记录 int txtRecord = TxTtoFtpaAtion.FilenumberTxt(filepath); // System.out.println(txtRecord); // 获取当前txt中应导出记录数 int txtnumber = TxTtoFtpaAtion.GetQuantity(filename); if (filename.contains(Constant.STAMPP) || filename.contains(Constant.STAMPV) || filename.contains(Constant.LOGISTICS) || filename.contains(Constant.SUCCESS)) { if (txtRecord == txtnumber) { // FTP上传方法 flag = FileTool.upLoadFromProduction(FTPIP, port, username, password, ftppath, filename, filepath); // 判断文件是否存在,不存在就创建 java.io.File myFilePath = new java.io.File( filepathok); if (!myFilePath.exists()) { try { myFilePath.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // FTP上传方法 if (flag) { log.log4j(type + filename + "上传成功,共上传" + txtnumber + "条记录"); flag = FileTool.upLoadFromProduction(FTPIP, port, username, password, ftppath, filenameok, filepathok); TxTtoFtpaAtion.copyFile(filepath, Localfile + "\\" + filename); log.log4j(type + filename + "备份成功"); TxTtoFtpaAtion.deleteFile(filepath); log.log4j(type + filename + "原始数据删除成功"); System.out.println(type + filepath + ":文件处理成功"); if (flag) { log.log4j(type + filepathok + "效验文件上传成功"); TxTtoFtpaAtion.copyFile(filepathok, Localfile + "\\" + filenameok); log.log4j(type + filepathok + "备份成功"); TxTtoFtpaAtion.deleteFile(filepathok); log.log4j(type + filepathok + "备份成功"); System.out.println(type + filepathok + ":文件处理成功"); } } else { log.log4j("FTP上传失败,请检查网络或配置是否正确"); } } else { log.log4j(type + filename + "上传失败,文件内容数据与回传数据已一致,可能数据丢失,请排查"); TxTtoFtpaAtion.copyFile(filepath, ftpLocalerror + "\\" + filename); TxTtoFtpaAtion.deleteFile(filepath); TxTtoFtpaAtion.copyFile(filepathok, ftpLocal + "\\" + filenameok); TxTtoFtpaAtion.deleteFile(filepathok); log.log4j(type + filename + "文件已移至错误目录"); log.log4j(type + filename + "文件已删除"); log.log4j(type + filenameok + "文件已移至错误目录"); log.log4j(type + filenameok + "文件已删除"); System.out.println(type + filename +"文件异常,请排查处理,数据不一致"); } } else { // FTP上传方法 flag = FileTool.upLoadFromProduction(FTPIP, port, username, password, ftppath, filename, filepath); // FTP上传方法 if (flag) { flag = FileTool.upLoadFromProduction(FTPIP, port, username, password, ftppath, filenameok, filepathok); TxTtoFtpaAtion.copyFile(filepath, Localfile + "\\" + filename); TxTtoFtpaAtion.deleteFile(filepath); TxTtoFtpaAtion.copyFile(filepathok, Localfile + "\\" + filenameok); TxTtoFtpaAtion.deleteFile(filepathok); log.log4j(filename + "上传成功"); log.log4j(filepathok + "上传成功"); System.out.println(filepath+"文件处理成功"); System.out.println(filepathok+"文件处理成功"); } else { log.log4j("网络异常,请协助处理"); } } } } } } } catch (DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 是不是操作的文件过大使用"jconsole"这个JDK自带工具,监控一下在JAVA安装目录的bin目录下.GUI工具,你应该看了就明白了. 给了你方法了,后面还有几个选项卡.你如果是内存不够,你应该看一下到底是什么吃了内存.而且你这个图现在也没有问题,线程不多,内存占用正常...CPU也没怎么用. 关于SQL注入 多线程加载 关于解析XML的 高手们帮帮忙啊 真的很急啊! j2ee中的一些疑问(编译,配置),谢谢! 武汉JAVA/android培训最优选择--传智播客武汉分校! Struts 异常的问题 请问,如何实现读取Word文件内容的功能? 深圳市招商迪辰商务系统有限公司高薪诚聘高级 Java 软件工程师 list去相邻重复怎么搞? 面试碰到J2EE系统安全性的问题应该怎么回答? 新手提问,求帮助 Hibernate 保存数据时报错:ids for this class must be....
File directory = new File("FTPUpload.xml");
if (!directory.exists())
{
try
{
XMLGenerate j2x = new XMLGenerate();
System.out.println("生成 mxl 文件...");
j2x.BuildXMLDoc();
} catch (Exception e)
{
e.printStackTrace();
}
}
// TODO Auto-generated method stub
java.io.File file = new java.io.File(directory.getCanonicalPath()); // 创建一个读取XML文件的对象
SAXReader reader = new SAXReader();
// 创建一个文档对象
Document document; FTPRead read = new FTPRead();
document = reader.read(file);
// 获取文件的根节点
Element element = document.getRootElement();
for (Iterator i = element.elementIterator("disk"); i.hasNext();)
{
// 获取节点元素
element = (Element) i.next(); String name = element.attributeValue("name"); // 存储本地文件路径
String Localpath = element.elementText("Localpath"); // 上传FTP目录
String ftppath = element.elementText("ftppath"); // 回传FTP IP地址
String FTPIP = element.elementText("FTPIP"); // Ftp端口号
int port = Integer.parseInt(element.elementText("port")); // FTP登录帐号
String username = element.elementText("username"); // FTP登录密码
String password = element.elementText("password"); // FTP回传数据报错反馈文件夹
String ftperrors = element.elementText("ftperrors"); // 本地存储FTP回传报错目录
String ftpLocal = element.elementText("ftpLocal"); // 上传报错信息
String ftpLocalerror = element.elementText("ftpLocalerror"); // 文件备份路径
String Localfilegx = element.elementText("Localfilegx"); // 文件备份路径
String Localfiletx = element.elementText("Localfiletx");
//FtpDownFiles fd = new FtpDownFiles();
//fd.connectServer(FTPIP, username, password);
//fd.ftpDownFiles(ftperrors, ftpLocal); if (!(new File(Localpath).isDirectory()))
{
new File(Localpath).mkdir();
}
if (!(new File(ftpLocal).isDirectory()))
{
new File(ftpLocal).mkdir();
}
if (!(new File(Localfilegx).isDirectory()))
{
new File(Localfilegx).mkdir();
}
if (!(new File(Localfiletx).isDirectory()))
{
new File(Localfiletx).mkdir();
}
if (!(new File(ftpLocalerror).isDirectory()))
{
new File(ftpLocalerror).mkdir();
}
// 获取配置目录下的.txt和.OK文件
List list = getTextFile(Localpath); TxTtoFtpaAtion log = new TxTtoFtpaAtion();
if(list.size()!=0)
{
log.log4j(name + ":准备开始上传FTP");
for (int j = 0; j < list.size(); j++)
{
if(j==list.size()-1)
{
Timer timer1 = new Timer();
MonitorTimerTask monitor=new MonitorTimerTask();
timer1.scheduleAtFixedRate(monitor, 0, 5*60*1000L);
}
//System.out.println(list.get(j));
// 获取txt的文件路径名
String filepath; // 获取txt的文件名
String filename; // 获取的文件路径名
String filepathok; // 获取的文件名 String filenameok;
boolean flag;
filepath = list.get(j).toString();
filename = filepath.substring(Localpath.length() + 1); // 获取当前txt文件所对应的.ok文件路径
filepathok = filepath.replace("txt", "ok"); // 获取当前txt文件所对应的.ok文件名
filenameok = filepathok.substring(Localpath.length() + 1); // 回传数据类别
String type = null; // 文件备份路径
String Localfile;
// 区别种类name属性
String Category = ".txt";
String Category1 = ".txt";
String Category2 = ".txt";
if (filename.contains(Constant.LOGISTICS))
{
type = name + "信息上传:";
} else if (filename.contains(Constant.SUCCESS))
{
type = name + "信息上传:";
} else if (filename.contains(Constant.STAMPP))
{
type = "信息上传:";
} else if (filename.contains(Constant.STAMPV))
{
type = "信息上传:";
} else if (filename.contains(Constant.LOGISTICS))
{
type = name + "物流信息上传:";
} else if (filename.contains(Constant.SUCCESS))
{
type = name + "保单信息上传:";
} else if (filename.contains(Constant.REPORT))
{
type = "清单生产信息上传:";
} if ("...".equals(name.trim()))
{
Category = Constant.SUCCESS;
Category1 = Constant.LOGISTICS;
Category2 = Constant.REPORT;
Localfile = Localfilegx;
} else if ("..".equals(name.trim()))
{
Category = Constant.STAMPP;
Category1 = Constant.STAMPV;
Category2 = Constant.STAMPP;
Localfile = Localfilegx; } else
{
Category = ".txt";
Category1 = ".txt";
Category2 = ".txt";
Localfile = Localfiletx;
}
if (filename.contains(".txt")
&& (filename.contains(Category)
|| filename.contains(Category1) || filename
.contains(Category2)))
{
// 获取当前读取的txt中有多少条记录
int txtRecord = TxTtoFtpaAtion.FilenumberTxt(filepath);
// System.out.println(txtRecord);
// 获取当前txt中应导出记录数
int txtnumber = TxTtoFtpaAtion.GetQuantity(filename);
if (filename.contains(Constant.STAMPP)
|| filename.contains(Constant.STAMPV)
|| filename.contains(Constant.LOGISTICS)
|| filename.contains(Constant.SUCCESS))
{
if (txtRecord == txtnumber)
{
// FTP上传方法
flag = FileTool.upLoadFromProduction(FTPIP,
port, username, password, ftppath,
filename, filepath);
// 判断文件是否存在,不存在就创建
java.io.File myFilePath = new java.io.File(
filepathok);
if (!myFilePath.exists())
{
try
{
myFilePath.createNewFile();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } // FTP上传方法
if (flag)
{
log.log4j(type + filename + "上传成功,共上传"
+ txtnumber + "条记录");
flag = FileTool.upLoadFromProduction(FTPIP,
port, username, password, ftppath,
filenameok, filepathok);
TxTtoFtpaAtion.copyFile(filepath, Localfile
+ "\\" + filename);
log.log4j(type + filename + "备份成功");
TxTtoFtpaAtion.deleteFile(filepath);
log.log4j(type + filename + "原始数据删除成功");
System.out.println(type + filepath + ":文件处理成功"); if (flag)
{
log.log4j(type + filepathok
+ "效验文件上传成功");
TxTtoFtpaAtion.copyFile(filepathok,
Localfile + "\\" + filenameok);
log.log4j(type + filepathok + "备份成功");
TxTtoFtpaAtion.deleteFile(filepathok);
log.log4j(type + filepathok + "备份成功");
System.out.println(type + filepathok + ":文件处理成功");
}
} else
{
log.log4j("FTP上传失败,请检查网络或配置是否正确");
} } else
{
log.log4j(type + filename
+ "上传失败,文件内容数据与回传数据已一致,可能数据丢失,请排查");
TxTtoFtpaAtion.copyFile(filepath, ftpLocalerror
+ "\\" + filename);
TxTtoFtpaAtion.deleteFile(filepath);
TxTtoFtpaAtion.copyFile(filepathok, ftpLocal
+ "\\" + filenameok);
TxTtoFtpaAtion.deleteFile(filepathok); log.log4j(type + filename + "文件已移至错误目录");
log.log4j(type + filename + "文件已删除");
log.log4j(type + filenameok + "文件已移至错误目录");
log.log4j(type + filenameok + "文件已删除");
System.out.println(type + filename +"文件异常,请排查处理,数据不一致");
} } else
{
// FTP上传方法
flag = FileTool.upLoadFromProduction(FTPIP, port,
username, password, ftppath, filename,
filepath);
// FTP上传方法
if (flag)
{
flag = FileTool.upLoadFromProduction(FTPIP,
port, username, password, ftppath,
filenameok, filepathok);
TxTtoFtpaAtion.copyFile(filepath, Localfile
+ "\\" + filename);
TxTtoFtpaAtion.deleteFile(filepath);
TxTtoFtpaAtion.copyFile(filepathok, Localfile
+ "\\" + filenameok);
TxTtoFtpaAtion.deleteFile(filepathok);
log.log4j(filename + "上传成功");
log.log4j(filepathok + "上传成功");
System.out.println(filepath+"文件处理成功");
System.out.println(filepathok+"文件处理成功");
} else
{
log.log4j("网络异常,请协助处理");
}
}
}
}
}
}
} catch (DocumentException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
GUI工具,你应该看了就明白了.