NC凭证接口

问题的描述:金融行业在系统模块分为财务和业务两个系统。但是财务有时候需要生成凭证,这时候就涉及业务模块了,我方就需要写NC凭证接口。这时候就需要三方交互好,确定规则。简单的说,就是我方发送一个正确的一个XML格式的字符给NC公司,然后NC公司会判断这个XML是不是符合规则,返回一个xml格式结果。
用到的架包:dom4j-1.6.1.jar、jdom.jar
接口的代码

package com.enfo.wd;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.DocumentSource;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;


public class NCPush {
    @SuppressWarnings("rawtypes")
    public String checkNCSendPzFlag(String sendXML) throws Exception {
        String result = "";//returne标识
        try {
            /*********将xml发送到目标服务器*****************/
            //将xml保存在本地文件夹
            String path = "F:\\xml_voucher\\IMP\\NC.xml";
            File file = new File("F:\\xml_voucher\\IMP");
            file.mkdirs();//创建父文件夹
            File f2 = new File(path);
            f2.createNewFile();
            FileOutputStream fos = new FileOutputStream(f2);
            fos.write(sendXML.getBytes("utf-8"));//写入并设置编码格式
            fos.flush();
            fos.close();

            //获取Servlet连接并设置请求的方法
            //NC接口地址
            String url = "http://10.68.3.5:8020/service/XChangeServlet?account=04&groupcode=1";
            URL realURL = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realURL
                    .openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-type", "text/xml");
            connection.setRequestMethod("POST");

            //将Document对象写入连接的输出流中
            BufferedOutputStream out = new BufferedOutputStream(connection
                    .getOutputStream());
            BufferedInputStream input = new BufferedInputStream(
                    new FileInputStream(path));
            int length;
            byte[] buffer = new byte[1000];
            while ((length = input.read(buffer, 0, 1000)) != -1) {
                out.write(buffer, 0, length);
            }
            input.close();
            out.close();

            /***************从连接的输入流中取得回执信息***************/
            //输入流获取返回的xml,写入Document
            InputStream inputStream = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufreader = new BufferedReader(isr);
            String xmlString = "";
            int c;
            while ((c = bufreader.read()) != -1) {
                System.out.print((char) c);
                xmlString += (char) c;
            }
            input.close();
            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";
            File resDis = new File("E:\\用友\\回执目录\\");
            if (!resDis.exists())
                resDis.mkdirs();
            StreamResult results = new StreamResult(new File(resFile));
            transformer.transform(source, results);

            //jdom解析XML
            SAXBuilder builder = new SAXBuilder();
            Document doc = (Document) builder.build(new File(resFile));
            Element foo = (Element) doc.getRootElement();
            List allChildren = (List) foo.getChildren();
            for (int i = 0; i < allChildren.size(); i++) {
                System.out.println(" 发送状态:"
                        + ((Element)allChildren.get(i)).getChild("resultcode").getText());
                System.out.print("测试信息"
                        + ((Element)allChildren.get(i)).getChild("resultdescription").getText());
            }

            if (((Element)allChildren.get(0)).getChild("resultcode").getText().equals("1")) {
                result = "导入成功!";
            } else {
                result = "导入失败:"
                        + ((Element) allChildren.get(0)).getChild(
                                "resultdescription").getText();
            }
        } catch (Exception e) {
            result = "导入失败" + e.getMessage();
            e.printStackTrace();
        }
        return result;
    }


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活中的思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值