NC憑證接口(Java發送流和處理返回結果)


問題描述:

  金融行業在系統模塊分為財務和業務兩個系統,我公司是負責業務模塊系統,NC公司負責財務系統。但是財務有時候需要生成憑證,這時候就涉及業務模塊了,我方就需要寫NC憑證接口。這時候就需要三方交互好,確定規則。簡單的說,就是我方發送一個正確的一個XML格式的字符給NC公司,然后NC公司會判斷這個XML是不是符合規則,返回一個xml格式結果。好了,不多說,其實就是寫一個Java代碼,發送xml格式流和獲取返回的xml格式的結果處理。

 

 1     public String checkNCSendPzFlag(String sendXML) throws Exception {
 2         String result = "";//returne標識
 3         try {
 4             /*********將xml發送到目標服務器*****************/
 5             //將xml保存在本地文件夾
 6             String path = "F:\\xml_voucher\\IMP\\NC.xml";
 7             File file = new File("F:\\xml_voucher\\IMP");
 8             file.mkdirs();//創建父文件夾
 9             File f2 = new File(path);
10             f2.createNewFile();
11             FileOutputStream fos = new FileOutputStream(f2);
12             fos.write(sendXML.getBytes("utf-8"));//寫入並設置編碼格式
13             fos.flush();
14             fos.close();
15 
16             //獲取Servlet連接並設置請求的方法
17             String url = "http://10.68.3.5:8020/service/XChangeServlet?account=04&groupcode=1";//NC接口地址
18             URL realURL = new URL(url);
19             HttpURLConnection connection = (HttpURLConnection) realURL
20                     .openConnection();
21             connection.setDoOutput(true);
22             connection.setRequestProperty("Content-type", "text/xml");
23             connection.setRequestMethod("POST");
24             
25             //將Document對象寫入連接的輸出流中
26             BufferedOutputStream out = new BufferedOutputStream(connection
27                     .getOutputStream());
28             BufferedInputStream input = new BufferedInputStream(
29                     new FileInputStream(path));
30             int length;
31             byte[] buffer = new byte[1000];
32             while ((length = input.read(buffer, 0, 1000)) != -1) {
33                 out.write(buffer, 0, length);
34             }
35             input.close();
36             out.close();
37 
38             /***************從連接的輸入流中取得回執信息***************/
39             //輸入流獲取返回的xml,寫入Document
40             InputStream inputStream = connection.getInputStream();
41             InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
42             BufferedReader bufreader = new BufferedReader(isr);
43             String xmlString = "";
44             int c;
45             while ((c = bufreader.read()) != -1) {
46                 System.out.print((char) c);
47                 xmlString += (char) c;
48             }
49             input.close();
50             Document resDoc = DocumentHelper.parseText(xmlString);
51 
52             /************對回執結果的后續處理…************/
53             //document轉化為xml,並保存
54             TransformerFactory tFactory = TransformerFactory.newInstance();
55             Transformer transformer = tFactory.newTransformer();
56             DocumentSource source = new DocumentSource(resDoc);
57             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
58             //設置文檔的換行與縮進
59             transformer.setOutputProperty(OutputKeys.INDENT, "YES");
60             //設置日期格式
61             SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
62             String resFile = "E:\\用友\\回執目錄\\BkMsg_會計憑證_"
63                     + fmt.format(new Date()) + ".xml";
64             File resDis = new File("E:\\用友\\回執目錄\\");
65             if (!resDis.exists())
66                 resDis.mkdirs();
67             StreamResult results = new StreamResult(new File(resFile));
68             transformer.transform(source, results);
69 
70             //jdom解析XML
71             SAXBuilder builder = new SAXBuilder();
72             org.jdom.Document doc = builder.build(new File(resFile));
73             Element foo = doc.getRootElement();
74             List allChildren = foo.getChildren();
75             for (int i = 0; i < allChildren.size(); i++) {
76                 System.out.println(" 發送狀態:"
77                         + ((Element) allChildren.get(i)).getChild("resultcode").getText());
78                 System.out.print("測試信息"
79                         + ((Element) allChildren.get(i)).getChild("resultdescription").getText());
80             }
81 
82             if (((Element) allChildren.get(0)).getChild("resultcode").getText()
83                     .equals("1")) {
84                 result = "導入成功!";
85             } else {
86                 result = "導入失敗:"
87                         + ((Element) allChildren.get(0)).getChild(
88                                 "resultdescription").getText();
89             }
90         } catch (Exception e) {
91             // TODO: handle exception
92             result = "導入失敗" + e.getMessage();
93             e.printStackTrace();
94         }
95         return result;
96     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM