1. 程式人生 > >第三方系統的NC對接,實現憑證自動匯入功能

第三方系統的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屬性");
}