public static String readFile(Session session, String sPath, JdbcTemplate jdbcTemplate,SystemService systemService,String sPath_bak, String sPath_broke) {
String flag="1";
Channel channel = null;
Map<String, InputStream > stringHashMap =new HashMap<>();
try {
channel = (Channel) session.openChannel("sftp");
channel.connect(10000000);
ChannelSftp sftp = (ChannelSftp) channel;
try {
sftp.cd(sPath);
} catch (SftpException e) {
sftp.mkdir(sPath);
sftp.cd(sPath);
}
Vector<ChannelSftp.LsEntry> listFiles = sftp.ls(sftp.pwd());
for (ChannelSftp.LsEntry file : listFiles) {
String fileName = file.getFilename();
String[] filename=fileName.split("_");
try {
//读取0001文件
if(filename[0].equals("0001")){
InputStream inputStream = sftp.get(sftp.pwd() + "/" + fileName);
System.out.println(sftp.pwd());
stringHashMap.put(fileName,inputStream);
//解析流读取xml
SAXReader reader = new SAXReader();
Document document =reader.read(inputStream);
Element root = document.getRootElement();
List<Element> list = root.elements() ;
//获得指定节点下面的子节点
Element contactElem = root.element("Body").element("Root").element("HeaderInfo");//首先要知道自己要操作的节点。
List<Element> contactList = contactElem.elements();
String businessno="";
for (Element e:contactList){
if(e.getName().equals("business_no")){
businessno=e.getText();
}
}
String sql="select count(*) from dec_main where client_no='"+businessno+"' and bpm_status='3'";
String num=jdbcTemplate.queryForObject(sql, String.class);
//1.如果存在并且客户已经审核通过了返回0004数据
if(num.equals("1")){
try {
sftp.cd(sPath_broke);
} catch (SftpException e) {
sftp.mkdir(sPath_broke);
sftp.cd(sPath_broke);
}
//xbl 重新返回的0004报文写活
sql="select count(*) from dec_main where client_no='"+businessno+"' and bpm_status='3'";
List <Map<String,Object>> reslist = jdbcTemplate.queryForList(sql);
Map<String,Object> resmap = reslist.get(0);
//创建一个名为DecMessage的节点,因为是第一个创建,所以是根节点,再通过doc创建一个则会报错。
Document doc = DocumentHelper.createDocument();
Element rootg = doc.addElement("Envelope");
Element url = rootg.addElement("Header");
Element loc = url.addElement("PubSub_Command");
loc.setText("Publish");
loc = url.addElement("PubSub_Topic");
loc.setText("/IEM/IEMINFO");
loc = url.addElement("Requester_ID");
loc.setText("IEM");
loc = url.addElement("SID");
loc.setText("14002601016081614000241000024100");
long time = System.currentTimeMillis();
String xmlUrl="D:\\"+time+"createXml08.xml";
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter os = new XMLWriter(new FileWriter(new
File(xmlUrl)), format);
os.write(doc);
// 关闭资源
os.close();
System.out.print("生成XML文件成功");
} catch (IOException e) {
System.out.print("生成XML文件失败");
e.printStackTrace();
}
//读流
File srcFile = new File(xmlUrl);
InputStream inputStream004= new FileInputStream(srcFile);
String file004="0004_"+time;
sftp.put(inputStream004, file004);
}
//2.如果不存在则新增
if(num.equals("0")){
jdbcTemplate.update("delete from dec_main where client_no='"+businessno+"'");
DecMainEntity decmain=new DecMainEntity();
decmain.setClientNo(businessno);
decmain.setCreateDate(new Date());
//xbl 把0001报文里的其他值复写到decmain
List<String> headerreslist = new ArrayList<String>();
List<String> bodyreslist = new ArrayList<String>();
for (Element e:contactList){
//System.out.println(e.getName()+":"+e.getText());
headerreslist.add(e.getName()+":"+e.getText());
}
Element contactElem2 = root.element("Body").element("Root").element("ListInfo");
for(Iterator emp4=contactElem2.elementIterator();emp4.hasNext();){
Element employee5 = (Element) emp4.next();
/*节点名称和节点内容以KEY VALUE的方式保存到JSON对象*/
for(Iterator emp5=employee5.elementIterator();emp5.hasNext();){
Element employee6 = (Element) emp5.next();
//System.out.println(employee6.getName()+":'"+employee6.getText()+"'");
bodyreslist.add(employee6.getName()+":"+employee6.getText());
}
}
try {
systemService.save(setValue(headerreslist,bodyreslist,decmain) );
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//拷贝文件至iem_bak里面
try {
sftp.cd(sPath_bak);
} catch (SftpException e) {
sftp.mkdir(sPath_bak);
sftp.cd(sPath_bak);
}
sftp.put(inputStream, fileName);
//删除文件
delete(sPath,fileName,sftp);;
}
//读取0003文件
if(filename[0].equals("0003")){
InputStream inputStream = sftp.get(sftp.pwd() + "/" + fileName);
System.out.println(sftp.pwd());
stringHashMap.put(fileName,inputStream);
//解析流读取xml
SAXReader reader = new SAXReader();
Document document =reader.read(inputStream);
Element root = document.getRootElement();
List<Element> list = root.elements() ;
//获得指定节点下面的子节点
Element contactElem2 = root.element("Body").element("Root").element("TableInfo");
for(Iterator emp4=contactElem2.elementIterator();emp4.hasNext();){
Element employee5 = (Element) emp4.next();
/*节点名称和节点内容以KEY VALUE的方式保存到JSON对象*/
for(Iterator emp5=employee5.elementIterator();emp5.hasNext();){
Element employee6 = (Element) emp5.next();
System.out.println(employee6.getName()+":"+employee6.getText());
}
}
//拷贝文件至iem_bak里面
try {
sftp.cd(sPath_bak);
} catch (SftpException e) {
sftp.mkdir(sPath_bak);
sftp.cd(sPath_bak);
}
sftp.put(inputStream, fileName);
//删除文件
delete(sPath,fileName,sftp);;
}
} catch (SftpException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}