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     }

 

posted @ 2016-09-29 13:49  冰西瓜先生  阅读(4007)  评论(2编辑  收藏  举报