生活随笔
收集整理的這篇文章主要介紹了
NC凭证接口
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題的描述:金融行業(yè)在系統(tǒng)模塊分為財(cái)務(wù)和業(yè)務(wù)兩個(gè)系統(tǒng)。但是財(cái)務(wù)有時(shí)候需要生成憑證,這時(shí)候就涉及業(yè)務(wù)模塊了,我方就需要寫NC憑證接口。這時(shí)候就需要三方交互好,確定規(guī)則。簡(jiǎn)單的說,就是我方發(fā)送一個(gè)正確的一個(gè)XML格式的字符給NC公司,然后NC公司會(huì)判斷這個(gè)XML是不是符合規(guī)則,返回一個(gè)xml格式結(jié)果。
用到的架包:dom4j-1.6.1.jar、jdom.jar
接口的代碼:
package
com.enfo.wdimport 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.Listimport javax
.xml.transform.OutputKeys
import javax
.xml.transform.Transformer
import javax
.xml.transform.TransformerFactory
import javax
.xml.transform.stream.StreamResultimport org
.dom4j
.Document
import org
.dom4j
.DocumentHelper
import org
.dom4j
.io.DocumentSource
import org
.jdom.Element
import org
.jdom.input.SAXBuilderpublic class NCPush {@SuppressWarnings(
"rawtypes")public String checkNCSendPzFlag(String sendXML) throws Exception {String result =
""try {//將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連接并設(shè)置請(qǐng)求的方法//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對(duì)象寫入連接的輸出流中BufferedOutputStream
out = new BufferedOutputStream(connection
.getOutputStream())BufferedInputStream input = new BufferedInputStream(new FileInputStream(path))int lengthbyte[] buffer = new byte[
1000]while ((length = input
.read(buffer,
0,
1000)) != -
1) {
out.write(buffer,
0, length)}input
.close()
out.close()//輸入流獲取返回的xml,寫入DocumentInputStream inputStream = connection
.getInputStream()InputStreamReader isr = new InputStreamReader(inputStream,
"utf-8")BufferedReader bufreader = new BufferedReader(isr)String xmlString =
""int cwhile ((c = bufreader
.read()) != -
1) {System
.out.print((char) c)xmlString += (char) c}input
.close()Document resDoc = DocumentHelper
.parseText(xmlString)//document轉(zhuǎn)化為xml,并保存TransformerFactory tFactory = TransformerFactory
.newInstance()Transformer transformer = tFactory
.newTransformer()DocumentSource source = new DocumentSource(resDoc)transformer
.setOutputProperty(OutputKeys
.ENCODING,
"UTF-8")//設(shè)置文檔的換行與縮進(jìn)transformer
.setOutputProperty(OutputKeys
.INDENT,
"YES")//設(shè)置日期格式SimpleDateFormat fmt = new SimpleDateFormat(
"yyyyMMddHHmmss")String resFile =
"E:\\用友\\回執(zhí)目錄\\BkMsg_會(huì)計(jì)憑證_"+ fmt
.format(new Date()) +
".xml"File resDis = new File(
"E:\\用友\\回執(zhí)目錄\\")if (!resDis
.exists())resDis
.mkdirs()StreamResult results = new StreamResult(new File(resFile))transformer
.transform(source, results)//jdom解析XMLSAXBuilder builder = new SAXBuilder()Document doc = (Document) builder
.build(new File(resFile))Element foo = (Element) doc
.getRootElement()List allChildren = (List) foo
.getChildren()for (int i =
0System
.out.println(
" 發(fā)送狀態(tài):"+ ((Element)allChildren
.get(i))
.getChild(
"resultcode")
.getText())System
.out.print(
"測(cè)試信息"+ ((Element)allChildren
.get(i))
.getChild(
"resultdescription")
.getText())}if (((Element)allChildren
.get(
0))
.getChild(
"resultcode")
.getText()
.equals(
"1")) {result =
"導(dǎo)入成功!"} else {result =
"導(dǎo)入失敗:"+ ((Element) allChildren
.get(
0))
.getChild(
"resultdescription")
.getText()}} catch (Exception e) {result =
"導(dǎo)入失敗" + e
.getMessage()e
.printStackTrace()}return result}}
總結(jié)
以上是生活随笔為你收集整理的NC凭证接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。