问题描述:
金融行业在系统模块分为财务和业务两个系统,我公司是负责业务模块系统,NC公司负责财务系统。但是财务有时候需要生成凭证,这时候就涉及业务模块了,我方就需要写NC凭证接口。这时候就需要三方交互好,确定规则。简单的说,就是我方发送一个正确的一个XML格式的字符给NC公司,然后NC公司会判断这个XML是不是符合规则,返回一个xml格式结果。好了,不多说,其实就是写一个Java代码,发送xml格式流和获取返回的xml格式的结果处理。
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连接并设置请求的方法
String url = "http://10.68.3.5:8020/service/XChangeServlet?account=04&groupcode=1";//NC接口地址
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();
org.jdom.Document doc = builder.build(new File(resFile));
Element foo = doc.getRootElement();
List allChildren = 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) {
// TODO: handle exception
result = "导入失败" + e.getMessage();
e.printStackTrace();
}
return result;
}