NC凭证多个XML导入(同个文件夹)

dave_o 发布于 2016/08/15 17:08
阅读 203
收藏 0
String url = "http://172.88.1.61:83/service/XChangeServlet?account=0007&receiver=SWJ";
 URL realURL = new URL(url);
 HttpURLConnection connection = (HttpURLConnection)realURL.openConnection();
 connection.setDoOutput(true);
 connection.setRequestProperty("Content-type", "text/xml");
 connection.setRequestMethod("POST");
 response.setContentType("text/xml;charset=UTF-8");


 File file1 = new File("E:\\用友\\");
//创建相关文件夹,如果存在则不创建
 /*File fileDis = new File("E:\\用友\\数据文件目录\\");
 File resDis = new File("E:\\用友\\回执目录\\");
 File errDis = new File("E:\\用友\\失败回执转移目录\\");
 if(!fileDis.exists())
  fileDis.mkdirs();
 if(!resDis.exists())
  resDis.mkdirs();
 if(!errDis.exists())
  errDis.mkdirs();*/
 File resDis = new File("E:\\用友1\\回执目录\\");
 if(!resDis.exists())
  resDis.mkdirs();
 try {
 
//遍历多个XML
showAllFiles(file1);
} catch (Exception e2) {
e2.printStackTrace();
}
 BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
 File[] files = file1.listFiles();
 for(File file:files){
 System.out.println(file);
 
 BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
 
 int length;
 byte[] buffer = new byte[1025];
 while ((length = input.read(buffer, 0, 1025)) != -1) {
    out.write(buffer, 0, length);
 }
 
    input.close();
    out.flush();
    out.close();
   //out.flush();
 // 从连接的输入流中取得回执信息
 /***************从输入流取得Doc***************/
 InputStream inputStream = connection.getInputStream();
  
 InputStreamReader isr = new InputStreamReader(inputStream);
 BufferedReader bufreader = new BufferedReader(isr);
 String xmlString = "";
 int c;
 System.out.println("==================Beging====================");
 while ((c = bufreader.read()) != -1) {
  System.out.print((char) c);
  xmlString += (char) c;
 }
 input.close();
 System.out.println("===================End======================");
try {
Document resDoc = DocumentHelper.parseText(xmlString);
// 对回执结果的后续处理
 /************document转化为xml*************/
TransformerFactory tFactory = TransformerFactory.newInstance();  
Transformer transformer = tFactory.newTransformer();
DocumentSource source = new DocumentSource(resDoc);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//设置文档的换行与缩进
transformer.setOutputProperty(OutputKeys.INDENT, "YES");
  
  
//设置日期格式
 SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
 String resFile = "E:\\用友1\\回执目录\\BkMsg_销售单_"+fmt.format(new Date())+".xml";
 StreamResult result = new StreamResult(new File(resFile));
 transformer.transform(source,result);
 System.out.println("======生成回执文件成功=======");
  
  
 /**************jdom解析XML*****************/
 org.jdom.input.SAXBuilder saxReader = new SAXBuilder();
 org.jdom.Document document1 = saxReader.build(new File(resFile));
 org.jdom.Element root = document1.getRootElement();
//获取根元素,得到导入用友是否成功successful的值,值为Y:成功 N:失败
 String resSuc = root.getAttributeValue("successful");
 
@SuppressWarnings("unchecked")
List<org.jdom.Element> list = root.getChildren();
 for(org.jdom.Element e:list){
  System.out.println("-------------------------");
  System.out.println("filename---> "+e.getChildText("filename"));
  System.out.println("resultcode---> "+e.getChildText("resultcode"));
  System.out.println("resultdescription---> "+e.getChildText("resultdescription"));
  System.out.println("content--> "+e.getChildText("content"));
  System.out.println("--------------------------------------");
 }
//后面对回执结果做判断,然后改变导入状态就行了
 if(null != resSuc){
   if(resSuc.equals("N")){
       System.out.println("导入失败"); 
       
   }else if(resSuc.equals("Y")){
       System.out.println("导入成功");
       
     //接下来的代码,修改状态̬
   }else{
       System.out.println("出现未知错误");    
   }
}else{
   System.out.println("未找到successful属性");
}
} catch (Exception e1) {
e1.printStackTrace();
}
finally {
//关闭 BufferedInputStream
if (input != null)
input.close();
 }
}
加载中
返回顶部
顶部