向用友nc财务接口发送凭证

阅读更多
package com.nantian.nc;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class NCTest2 {
	public static void main(String[] args) {
		String urlStr = "http://192.168.0.68:80/service/XChangeServlet?account=0002&receiver=123";
		XmlBuilder builder = new XmlBuilder("F:/test1.xml");
		String xmlString=doc2string(builder.getDoc());
		System.out.println(xmlString);
		sendPingzheng(urlStr, xmlString);
	}

	public static Document sendPingzheng(String urlStr, String xmlString) {
		byte[] xmlData = xmlString.getBytes("utf-8");

		DataInputStream input = null;
		java.io.ByteArrayOutputStream out = null;
		Document d = null;
		try {
			
			URL url = new URL(urlStr);
			URLConnection urlCon = url.openConnection();
			urlCon.setDoOutput(true);
			urlCon.setDoInput(true);
			urlCon.setUseCaches(false);
			urlCon.setRequestProperty("Content-Type", "text/xml");
			urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length));
			DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
			printout.write(xmlData);
			printout.flush();
			printout.close();
			input = new DataInputStream(urlCon.getInputStream());
			byte[] rResult;
			out = new java.io.ByteArrayOutputStream();
			byte[] bufferByte = new byte[256];
			int l = -1;
			int downloadSize = 0;
			while ((l = input.read(bufferByte)) > -1) {
				downloadSize += l;
				out.write(bufferByte, 0, l);
				out.flush();
			}
			rResult = out.toByteArray();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			d = db.parse(new ByteArrayInputStream(rResult));
			
			String TaskAddr = d.getElementsByTagName("resultdescription").item(0).getFirstChild().getNodeValue();
			String resultcode = d.getElementsByTagName("resultcode").item(0).getFirstChild().getNodeValue();
			System.out.println("resultdescription:" + TaskAddr);
			System.out.println("resultcode"+resultcode);
			System.out.println(doc2string(d));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
				input.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return d;
	}

	// document转换为string
	public static String doc2string(Document doc) {
		StringWriter sw = null;
		try {
			sw = new StringWriter();
			Transformer serializer = TransformerFactory.newInstance().newTransformer();
			serializer.transform(new DOMSource(doc), new StreamResult(sw));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}
}

 xml文件操作类

package com.nantian.nc;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


public class XmlBuilder {
	
	private String path = null;
	private Document doc = null;
	private Element root = null;
	private Logger logger = Logger.getLogger(getClass().getName());

	public XmlBuilder(String path) {
		this.path = path;
		init();
	}

	public void init() {
		buildDocument();
		buildRoot();
	}

	private void buildDocument() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			logger.debug("Construct document builder success.");
			doc = builder.parse(new File(path));
			logger.debug("Build xml document success.");
		} catch (ParserConfigurationException e) {
			logger.error("Construct document builder error:" + e);
		} catch (SAXException e) {
			logger.error("Parse xml file error:" + e);
		} catch (IOException e) {
			logger.error("Read xml file error:" + e);
		}
	}

	
	private void buildRoot() {
		root = doc.getDocumentElement();
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document doc) {
		this.doc = doc;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	
	public Element getRoot() {
		return root;
	}

	public void setRoot(Element root) {
		this.root = root;
	}
}

 

你可能感兴趣的:(向用友nc财务接口发送凭证)