第三方系统的NC对接,实现凭证自动导入功能

 

// 获取Servlet连接并设置请求的方法
  String url = "http://172.16.99.31:8003/service/XChangeServlet?account=0003&receiver=80000002";
  URL realURL = new URL(url);
  HttpURLConnection connection = (HttpURLConnection)realURL.openConnection();
  connection.setDoOutput(true);
  connection.setRequestProperty("Content-type", "text/xml");
  connection.setRequestMethod("POST");

  
  File file = new File("E:\\用友\\数据文件目录\\会计凭证.xml");
  BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
  BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
  int length;
  byte[] buffer = new byte[1000];
  while ((length = input.read(buffer, 0, 1000)) != -1) {
     out.write(buffer, 0, length);
  }
     input.close();
     out.close();

  
  
  // 从连接的输入流中取得回执信息
  /***************从输入流取得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======================");
  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:\\用友\\回执目录\\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");
  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属性");
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值