定时任务读取xml文件时,会偶尔出现一个报错testError on line 1 of document :Premature en of file.Nested exception:Premature end of file
一个sftp传输工具读取一个文件夹里的xml文件,由于需要一直跑,所以写了个定时任务,在把jar包部署到linux下运行时,处理很多个之后会出现这个报错,然后重新单独把这个文件放进去的时候又能够正常解析。
这个解析xml文件的方法我按照百度的说法已经改过了,之前是用的SAXReader解析的,后来改的先获取文件内容存入缓存中,但是依然有这个问题,没有什么BOM头,因为是从服务器的一个文件夹里面去读取的xml文件,这个xml文件是只要生成了我就去读取,所以是个1秒读一次
不知道是不是定时任务有问题还是咋的了,已经想了各种办法依旧不行,出错的xml文件单独拿出来再跑依旧可以正常解析,很奇怪,求大神解决,如能解决的可以联系远程也行,必有重谢
报错信息
public class ReadFile {
private static Logger logger = Logger.getLogger(ReadFile.class);
public ReadFile() {
}
/**
* 读取某个文件夹下的所有文件
*/
public static boolean readfile(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
} else if (file.isDirectory()) {
logger.info("读取文件夹成功");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + File.separator
+ filelist[i]);
logger.info("filepath:" + filepath);
if (!readfile.isDirectory()) {
logger.info("path=" + readfile.getPath());
logger.info("absolutepath="
+ readfile.getAbsolutePath());
logger.info("name=" + readfile.getName());
} else if (readfile.isDirectory()) {
readfile(filepath + File.separator + filelist[i]);
}
DataSourceXMLParse.parserXml(readfile.getAbsolutePath());
// 删除xml
ReadFile.delFolder(readfile.getAbsolutePath());
}
}
} catch (FileNotFoundException e) {
logger.info("readfile() Exception:" + e.getMessage());
}
return true;
}
// 删除文件
// param folderPath 文件夹完整绝对路径
public static void delFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
String[] str = folderPath.split("/");
int len = str.length;
String filename = str[len-1];
java.io.File myFilePath = new java.io.File(filePath);
System.out.println("****************** folderPath = "+folderPath);
System.out.println("****************** filePath = "+filePath);
File dest_dir = new File("/home/d5000/test_file/in_back");
File dest = new File("/home/d5000/test_file/in_back/"+filename);
if(!dest_dir.exists()){
dest_dir.mkdirs();
}
myFilePath.renameTo(dest);
// myFilePath.delete(); // 删除文件
} catch (Exception e) {
e.printStackTrace();
System.out.println("****************** "+e.getCause());
}
}
public static void writeToFile(String context, String filePath,
String filename) {
System.out.println("congtext:" + context + " filePath" + filePath + "\n");
FileOutputStream fop = null;
File file;
try {
file = new File(filePath + filename);
if (!file.exists()) {
file.createNewFile();
}
fop = new FileOutputStream(file);
byte[] contentInBytes = context.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
logger.info(e.getMessage());
e.printStackTrace();
}
}
}
public static void main(String[] args) {
logger.info("**********开启启动监控系统图模审核消息文件夹");
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(hour + ":" + minute + ":" + second + "\n");
TimerTask task = new TimerTask() {
int nCountSuc = 0;
@Override
public void run() {
try {
boolean bRet = readfile(ReadProperties
.get("LOCAL_cimsvgpath"));
if (bRet) {
nCountSuc++;
}
//
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(hour + ":" + minute + ":" + second + "\n");
if (hour == 23 && minute == 59 && second==59) {
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd");// 设置日期格式
String filename = df.format(new Date()) + ".txt";
// count写入文件
writeToFile(String.valueOf("收到总数:" + nCountSuc+"条, 发送总数"+nCountSuc),
ReadProperties.get("SEND_AND_RECEIVE_PATH"), filename);
// 初始化
nCountSuc = 0;
}
}
catch (FileNotFoundException ex)
{
logger.info(ex.getMessage());
ex.printStackTrace();
}
catch (IOException ex) {
logger.info(ex.getMessage());
ex.printStackTrace();
}
System.out.println("**********正在读取文件");
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
timer.scheduleAtFixedRate(task, delay, intevalPeriod);
}
}
下面代码是读取xml及一些处理逻辑
public static void parserXml(String contextPath) {
logger.info("**********开始解析XML文件");
// File inputXml = new File(contextPath);
// SAXReader saxReader = new SAXReader();
try {
// Document document = saxReader.read(inputXml);
StringBuffer content = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(contextPath),"GBK"));
String line = null;
while((line = br.readLine()) != null ){
content.append(line+"\n");
}
br.close();
Document document = DocumentHelper.parseText(content.toString());
//根节点
Element root = document.getRootElement();
//遍历
map.clear();
map = listNodes2(root);
listNodes(map);
} catch (Exception e) {
logger.info("test" + e.getMessage());
e.printStackTrace();
}
logger.info("**********解析XML文件完成");
}