EAITool.java
5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.huaheng.api.U8.tools;
import java.io.*;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.huaheng.api.U8.domain.Ufinterface;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
public class EAITool {
// 以下字段的含义,详见《U8开发之EAI接口.docx》中EAI接口规范->数据交换格式->请求消息格式
public static final String ROOTTAG = "storeout"; // sql
public static final String SENDER = "001"; // 发送者
public static final String RECEIVER = "u8"; // 接收者
public static final String PROC_ADD = "add"; // 处理——添加
public static final String PROC_DEL = "delete"; // 处理——删除
public static final String PROC_EDIT = "edit"; // 处理——编辑
public static final String PROC_LIST = "query"; // 处理——查询所有
public static final String CODEEXCHANGED = "N"; // 导入的代码未进行转码
public static final String EXPORTNEEDEXCH = "N"; // 导出的代码不需要转码
public static final String VERSION = "1.0"; // 版本
public static String sendXML(String urlString, String xmlString) throws Exception {
// 发送xml
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
// 将接口调用超时时间改为20秒
con.setConnectTimeout(40000);
con.setReadTimeout(40000);
StringBuilder sb = new StringBuilder();
String responseXml = null;
ApiLog log = null;
try {
con.setConnectTimeout(4000000);
con.setReadTimeout(4000000);
con.setDoInput(true);
con.setDoOutput(true);
con.setAllowUserInteraction(false);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept-Charset", "UTF-8");
// 初始化日志记录
log = ApiLogAspect.initApiLog(con, xmlString, QuantityConstant.WAREHOUSECODE);
// 发送Request消息
OutputStream out = con.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.write(xmlString.getBytes("UTF-8"));
// 获取Response消息
InputStream in = con.getInputStream();
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(reader);
String s = null;
while ((s = br.readLine()) != null) {
sb.append(s);
}
} catch (Exception e) {
ApiLogAspect.setApiLogException(log, e);
e.printStackTrace();
} finally {
responseXml = sb.toString();
ApiLogAspect.finishApiLog(log, con, responseXml);
}
return responseXml;
}
public static void setErpDefine(Ufinterface.Storeout.Body.Entry entry, String fieldName, String batch) {
Class<?> clazz = entry.getClass();
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(entry, batch);
System.out.println(entry.getDefine32());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
/**
* 获取xml指定参数内容
* @param xml
* @param param
* @return
*/
public static String getXmlParamContent(String xml, String param) {
try {
// 创建DocumentBuilderFactory实例
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建DocumentBuilder实例
DocumentBuilder builder = factory.newDocumentBuilder();
// 使用StringReader将XML字符串转换为InputSource
InputSource inputSource = new InputSource(new StringReader(xml));
// 解析XML字符串,得到Document对象
Document document = builder.parse(inputSource);
// 获取根元素
Element rootElement = document.getDocumentElement();
// 获取所有param元素
NodeList paramNodes = rootElement.getElementsByTagName(param);
// 遍历param元素,提取参数
for (int i = 0; i < paramNodes.getLength(); i++) {
Element paramElement = (Element)paramNodes.item(i);
String value = paramElement.getTextContent().trim();
return value;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
}