113c163c99046a749fca81112f1e1ac4a165d189.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package cn.com.goldenwater.dcproj.utils;
  2. import cn.com.goldenwater.dcproj.model.GwComFile;
  3. import org.apache.axis.client.Call;
  4. import org.apache.axis.client.Service;
  5. import org.dom4j.Document;
  6. import org.dom4j.DocumentException;
  7. import org.dom4j.Element;
  8. import org.dom4j.io.SAXReader;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import sun.misc.BASE64Encoder;
  12. import javax.xml.namespace.QName;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.UnsupportedEncodingException;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. /**
  22. * 用于处理调用 wsdl接口
  23. */
  24. public class HttpWSDLUtils {
  25. private static final Logger logger = LoggerFactory.getLogger(HttpWSDLUtils.class);
  26. /**
  27. * wsdl 接口地址
  28. */
  29. private static final String END_POINT = "http://10.135.6.4:8080/WaterCenter/webservice/ConvergenceService?wsdl";
  30. private static final String TARGET_NAMESPACE = "http://tempuri.org/";
  31. private static final String USER_LOGIN_METHOD = "LoginByAccount" ;
  32. private static final String DATA_PUSH_METHOD = "pushXml" ;
  33. private static final String ACCOUNT_NAME = "sljgpt" ;
  34. private static final String ACCOUNT_PWD = "Sl@jg905%#" ;
  35. private static final String WEB_FLAG = "WEB198" ;
  36. public static final String CONTENT_CHARSET = "UTF-8";
  37. public static String getUserToken(){
  38. try {
  39. Service service = new Service();
  40. Call call = (Call)service.createCall();
  41. //设置地址
  42. call.setTargetEndpointAddress(new java.net.URL(END_POINT));
  43. //设置要调用哪个方法
  44. call.setOperationName(new QName(TARGET_NAMESPACE, USER_LOGIN_METHOD));// 设置操作的名称。
  45. //设置参数名 :参数名 ,参数类型:String, 参数模式:'IN' or 'OUT'
  46. call.addParameter("userid",
  47. org.apache.axis.encoding.XMLType.XSD_STRING,
  48. javax.xml.rpc.ParameterMode.IN);// 接口的参数
  49. call.addParameter("password",
  50. org.apache.axis.encoding.XMLType.XSD_STRING,
  51. javax.xml.rpc.ParameterMode.IN);// 接口的参数
  52. //返回类型
  53. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
  54. //调用方法并传递参数
  55. String result = String.valueOf(call.invoke(new Object[]{ACCOUNT_NAME,ACCOUNT_PWD}));
  56. logger.error("line 61 LoginByAccount result: " + result);
  57. return result;
  58. } catch (Exception e) {
  59. logger.error("身份验证失败 ",e);
  60. System.err.println(e.toString());
  61. return "身份验证失败";
  62. }
  63. }
  64. /**
  65. * 根据 身份验证串 发送数据信息xml
  66. * @author lxf
  67. * @date 2023/12/6 11:14
  68. */
  69. public static String pushData(String guid, String xmlStr ){
  70. try {
  71. if(null == xmlStr){
  72. return "没有数据需要推送 line 88";
  73. }
  74. System.out.println("HttpWSDLUtils pushData 90 guid : " + guid);
  75. logger.info("HttpWSDLUtils pushData 91 guid : " + guid);
  76. Service service = new Service();
  77. Call call = (Call)service.createCall();
  78. //设置地址
  79. call.setTargetEndpointAddress(new java.net.URL(END_POINT));
  80. //设置要调用哪个方法
  81. call.setOperationName(new QName(TARGET_NAMESPACE, DATA_PUSH_METHOD));// 设置操作的名称。
  82. //设置参数名 :参数名 ,参数类型:String, 参数模式:'IN' or 'OUT'
  83. call.addParameter("guid",
  84. org.apache.axis.encoding.XMLType.XSD_STRING,
  85. javax.xml.rpc.ParameterMode.IN);// 接口的参数
  86. call.addParameter("catalogid",
  87. org.apache.axis.encoding.XMLType.XSD_STRING,
  88. javax.xml.rpc.ParameterMode.IN);// 接口的参数
  89. call.addParameter("xmlstr",
  90. org.apache.axis.encoding.XMLType.XSD_STRING,
  91. javax.xml.rpc.ParameterMode.IN);// 接口的参数
  92. //返回类型
  93. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
  94. //调用方法并传递参数
  95. String result = String.valueOf(call.invoke(new Object[]{guid,WEB_FLAG,xmlStr}));
  96. System.out.println("HttpWSDLUtils pushData 112 result is " + result);
  97. logger.info("HttpWSDLUtils pushData 113 result is " + result);
  98. InputStream is = new ByteArrayInputStream(result.getBytes("UTF-8"));
  99. SAXReader reader = new SAXReader();
  100. Document doc = reader.read(is);
  101. Element root = doc.getRootElement();
  102. Iterator<Element> rootIterator = root.elementIterator();
  103. while (rootIterator.hasNext()){
  104. Element element= rootIterator.next();
  105. System.out.println("HttpWSDLUtils pushData 121 result is " + element.asXML());
  106. String nodeName = element.getName();
  107. System.out.println("node name: " + nodeName);
  108. System.out.println("node data: " + element.getData());
  109. if("flag".equals(nodeName)){
  110. String text = element.getText();
  111. if("true".equals(text)){
  112. result = "推送成功";
  113. }else {
  114. result = "推送失败";
  115. }
  116. System.out.println("HttpWSDLUtils pushData 132 result: " +text);
  117. logger.info("HttpWSDLUtils pushData 133 result: " +text);
  118. }
  119. }
  120. is.close();
  121. return result;
  122. } catch (Exception e) {
  123. System.err.println(e.toString());
  124. logger.info("HttpWSDLUtils pushData 141 Exception: " +e.getMessage());
  125. return "推送数据失败";
  126. }
  127. }
  128. /**
  129. * 把数据库的数据转换化为xml报文 增加
  130. * @author lxf
  131. * @date 2023/12/6 11:25
  132. */
  133. public static String getAddXmlStr(List<GwComFile> comFileList) {
  134. if(null == comFileList || comFileList.isEmpty()){
  135. return null;
  136. }
  137. StringBuffer xmlSB = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  138. xmlSB.append("<table>");
  139. for(GwComFile comFile : comFileList){
  140. xmlSB.append("<row type=\"add\">");
  141. // 设置字段
  142. xmlSB.append("<id name=\"主键\" isattachment=\"false\"><![CDATA[");
  143. xmlSB.append(comFile.getId());
  144. xmlSB.append("]]></id>");
  145. xmlSB.append("<file_title name=\"文件标题\" isattachment=\"true\"><![CDATA[");
  146. xmlSB.append(comFile.getFilePath().replace("\\","/").substring(7));
  147. xmlSB.append("]]></file_title>");
  148. xmlSB.append("<key_word name=\"关键词\" isattachment=\"false\"><![CDATA[");
  149. xmlSB.append(comFile.getKeyWord());
  150. xmlSB.append("]]></key_word>");
  151. xmlSB.append("<file_type name=\"音频视频文本图片等\" isattachment=\"false\"><![CDATA[");
  152. xmlSB.append(comFile.getFileType());
  153. xmlSB.append("]]></file_type>");
  154. xmlSB.append("<abs name=\"摘要\" isattachment=\"false\"><![CDATA[");
  155. xmlSB.append(comFile.getAbs());
  156. xmlSB.append("]]></abs>");
  157. xmlSB.append("<file_name name=\"原始文件名(包含扩展名)\" isattachment=\"false\"><![CDATA[");
  158. xmlSB.append(comFile.getFileName());
  159. xmlSB.append("]]></file_name>");
  160. xmlSB.append("<file_size name=\"文件大小\" isattachment=\"false\"><![CDATA[");
  161. xmlSB.append(comFile.getFileSize());
  162. xmlSB.append("]]></file_size>");
  163. xmlSB.append("<file_ext name=\"文件扩展名\" isattachment=\"false\"><![CDATA[");
  164. xmlSB.append(comFile.getFileExt());
  165. xmlSB.append("]]></file_ext>");
  166. xmlSB.append("<file_path name=\"文件路径\" isattachment=\"false\"><![CDATA[");
  167. xmlSB.append(comFile.getFilePath());
  168. xmlSB.append("]]></file_path>");
  169. xmlSB.append("<thumbnail_url name=\"缩略图路径\" isattachment=\"false\"><![CDATA[");
  170. xmlSB.append(comFile.getThumbnailUrl());
  171. xmlSB.append("]]></thumbnail_url>");
  172. xmlSB.append("<biz_type name=\"业务类型\" isattachment=\"false\"><![CDATA[");
  173. xmlSB.append(comFile.getBizType());
  174. xmlSB.append("]]></biz_type>");
  175. xmlSB.append("<biz_id name=\"业务id\" isattachment=\"false\"><![CDATA[");
  176. xmlSB.append(comFile.getBizId());
  177. xmlSB.append("]]></biz_id>");
  178. xmlSB.append("<longitude name=\"移动app时有用\" isattachment=\"false\"><![CDATA[");
  179. xmlSB.append(comFile.getLongitude());
  180. xmlSB.append("]]></longitude>");
  181. xmlSB.append("<latitude name=\"移动app时有用\" isattachment=\"false\"><![CDATA[");
  182. xmlSB.append(comFile.getLatitude());
  183. xmlSB.append("]]></latitude>");
  184. xmlSB.append("<create_by name=\"创建人\" isattachment=\"false\"><![CDATA[");
  185. xmlSB.append(comFile.getCreateBy());
  186. xmlSB.append("]]></create_by>");
  187. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  188. xmlSB.append("<create_date name=\"创建日期\" isattachment=\"false\"><![CDATA[");
  189. xmlSB.append(Date2Str(comFile.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
  190. xmlSB.append("]]></create_date>");
  191. xmlSB.append("<update_by name=\"最后修改人\" isattachment=\"false\"><![CDATA[");
  192. xmlSB.append(comFile.getUpdateBy());
  193. xmlSB.append("]]></update_by>");
  194. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  195. xmlSB.append("<update_date name=\"最后修改日期\" isattachment=\"false\"><![CDATA[");
  196. xmlSB.append(Date2Str(comFile.getUpdateDate(),"yyyy-MM-dd HH:mm:ss"));
  197. xmlSB.append("]]></update_date>");
  198. xmlSB.append("<sn name=\"序号\" isattachment=\"false\"><![CDATA[");
  199. xmlSB.append(comFile.getSn());
  200. xmlSB.append("]]></sn>");
  201. xmlSB.append("<down_num name=\"下载次数\" isattachment=\"false\"><![CDATA[");
  202. xmlSB.append(comFile.getDownNum());
  203. xmlSB.append("]]></down_num>");
  204. xmlSB.append("<top name=\"是否置顶\" isattachment=\"false\"><![CDATA[");
  205. xmlSB.append(comFile.getTopValue());
  206. xmlSB.append("]]></top>");
  207. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  208. xmlSB.append("<top_date name=\"置顶日期\" isattachment=\"false\"><![CDATA[");
  209. xmlSB.append(Date2Str(comFile.getTopDate(),"yyyy-MM-dd HH:mm:ss"));
  210. xmlSB.append("]]></top_date>");
  211. xmlSB.append("<filename name=\"附件名称\" isattachment=\"false\"><![CDATA[");
  212. xmlSB.append(comFile.getFileName());
  213. xmlSB.append("]]></filename>");
  214. xmlSB.append("</row>");
  215. }
  216. xmlSB.append("</table>");
  217. return xmlSB.toString();
  218. }
  219. /**
  220. * 把数据库的数据转换化为xml报文 删除
  221. * @author lxf
  222. * @date 2023/12/6 11:25
  223. */
  224. public static String getDeleteXmlStr(List<GwComFile> comFileList) {
  225. if(null == comFileList || comFileList.isEmpty()){
  226. return null;
  227. }
  228. StringBuffer xmlSB = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  229. xmlSB.append("<table>");
  230. for(GwComFile comFile : comFileList){
  231. xmlSB.append("<row type=\"delete\">");
  232. // 设置字段
  233. xmlSB.append("<id name=\"主键\" isattachment=\"false\"><![CDATA[");
  234. xmlSB.append(comFile.getId());
  235. xmlSB.append("]]></id>");
  236. xmlSB.append("<file_title name=\"文件标题\" isattachment=\"true\"><![CDATA[");
  237. xmlSB.append(comFile.getFilePath().replace("\\","/").substring(7));
  238. xmlSB.append("]]></file_title>");
  239. xmlSB.append("<key_word name=\"关键词\" isattachment=\"false\"><![CDATA[");
  240. xmlSB.append(comFile.getKeyWord());
  241. xmlSB.append("]]></key_word>");
  242. xmlSB.append("<file_type name=\"音频视频文本图片等\" isattachment=\"false\"><![CDATA[");
  243. xmlSB.append(comFile.getFileType());
  244. xmlSB.append("]]></file_type>");
  245. xmlSB.append("<abs name=\"摘要\" isattachment=\"false\"><![CDATA[");
  246. xmlSB.append(comFile.getAbs());
  247. xmlSB.append("]]></abs>");
  248. xmlSB.append("<file_name name=\"原始文件名(包含扩展名)\" isattachment=\"false\"><![CDATA[");
  249. xmlSB.append(comFile.getFileName());
  250. xmlSB.append("]]></file_name>");
  251. xmlSB.append("<file_size name=\"文件大小\" isattachment=\"false\"><![CDATA[");
  252. xmlSB.append(comFile.getFileSize());
  253. xmlSB.append("]]></file_size>");
  254. xmlSB.append("<file_ext name=\"文件扩展名\" isattachment=\"false\"><![CDATA[");
  255. xmlSB.append(comFile.getFileExt());
  256. xmlSB.append("]]></file_ext>");
  257. xmlSB.append("<file_path name=\"文件路径\" isattachment=\"false\"><![CDATA[");
  258. xmlSB.append(comFile.getFilePath());
  259. xmlSB.append("]]></file_path>");
  260. xmlSB.append("<thumbnail_url name=\"缩略图路径\" isattachment=\"false\"><![CDATA[");
  261. xmlSB.append(comFile.getThumbnailUrl());
  262. xmlSB.append("]]></thumbnail_url>");
  263. xmlSB.append("<biz_type name=\"业务类型\" isattachment=\"false\"><![CDATA[");
  264. xmlSB.append(comFile.getBizType());
  265. xmlSB.append("]]></biz_type>");
  266. xmlSB.append("<biz_id name=\"业务id\" isattachment=\"false\"><![CDATA[");
  267. xmlSB.append(comFile.getBizId());
  268. xmlSB.append("]]></biz_id>");
  269. xmlSB.append("<longitude name=\"移动app时有用\" isattachment=\"false\"><![CDATA[");
  270. xmlSB.append(comFile.getLongitude());
  271. xmlSB.append("]]></longitude>");
  272. xmlSB.append("<latitude name=\"移动app时有用\" isattachment=\"false\"><![CDATA[");
  273. xmlSB.append(comFile.getLatitude());
  274. xmlSB.append("]]></latitude>");
  275. xmlSB.append("<create_by name=\"创建人\" isattachment=\"false\"><![CDATA[");
  276. xmlSB.append(comFile.getCreateBy());
  277. xmlSB.append("]]></create_by>");
  278. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  279. xmlSB.append("<create_date name=\"创建日期\" isattachment=\"false\"><![CDATA[");
  280. xmlSB.append(Date2Str(comFile.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
  281. xmlSB.append("]]></create_date>");
  282. xmlSB.append("<update_by name=\"最后修改人\" isattachment=\"false\"><![CDATA[");
  283. xmlSB.append(comFile.getUpdateBy());
  284. xmlSB.append("]]></update_by>");
  285. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  286. xmlSB.append("<update_date name=\"最后修改日期\" isattachment=\"false\"><![CDATA[");
  287. xmlSB.append(Date2Str(comFile.getUpdateDate(),"yyyy-MM-dd HH:mm:ss"));
  288. xmlSB.append("]]></update_date>");
  289. xmlSB.append("<sn name=\"序号\" isattachment=\"false\"><![CDATA[");
  290. xmlSB.append(comFile.getSn());
  291. xmlSB.append("]]></sn>");
  292. xmlSB.append("<down_num name=\"下载次数\" isattachment=\"false\"><![CDATA[");
  293. xmlSB.append(comFile.getDownNum());
  294. xmlSB.append("]]></down_num>");
  295. xmlSB.append("<top name=\"是否置顶\" isattachment=\"false\"><![CDATA[");
  296. xmlSB.append(comFile.getTopValue());
  297. xmlSB.append("]]></top>");
  298. // 请填写时间格式为:【yyyy/MM/dd HH:mm:ss】的数据
  299. xmlSB.append("<top_date name=\"置顶日期\" isattachment=\"false\"><![CDATA[");
  300. xmlSB.append(Date2Str(comFile.getTopDate(),"yyyy-MM-dd HH:mm:ss"));
  301. xmlSB.append("]]></top_date>");
  302. xmlSB.append("<filename name=\"附件名称\" isattachment=\"false\"><![CDATA[");
  303. xmlSB.append(comFile.getFileName());
  304. xmlSB.append("]]></filename>");
  305. xmlSB.append("</row>");
  306. }
  307. xmlSB.append("</table>");
  308. return xmlSB.toString();
  309. }
  310. public static String Date2Str(Date date, String pattern) {
  311. if (date == null) {
  312. date = new Date() ;
  313. }
  314. SimpleDateFormat time = new SimpleDateFormat(pattern);
  315. time.setTimeZone(DateUtils.zone);
  316. return time.format(date);
  317. }
  318. public static String base64Encode(String source){
  319. if(StringUtils.isEmpty(source)){
  320. return source;
  321. }
  322. sun.misc.BASE64Encoder base64Encoder = new BASE64Encoder();
  323. try {
  324. String encoderStr = base64Encoder.encode(source.getBytes(CONTENT_CHARSET)) ;
  325. return encoderStr;
  326. } catch (UnsupportedEncodingException e) {
  327. logger.error("BASE64Encoder 加密异常",e);
  328. return null ;
  329. }
  330. }
  331. public static void main(String[] args) {
  332. String xmlStr= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><flag>false</flag><msg>guid已过期</msg></Response>";
  333. try {
  334. InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
  335. SAXReader reader = new SAXReader();
  336. Document doc = reader.read(is);
  337. Element root = doc.getRootElement();
  338. Iterator<Element> rootIterator = root.elementIterator();
  339. while (rootIterator.hasNext()){
  340. Element element2= rootIterator.next();
  341. System.out.println("HttpWSDLUtils pushData 118 result is " + element2.asXML());
  342. String nodeName = element2.getName();
  343. System.out.println("node name: " + nodeName);
  344. if("flag".equals(nodeName)){
  345. element2.getData();
  346. }
  347. System.out.println("node data: " + element2.getData());
  348. }
  349. is.close();
  350. } catch ( UnsupportedEncodingException e) {
  351. throw new RuntimeException(e);
  352. }catch (DocumentException e) {
  353. throw new RuntimeException(e);
  354. } catch (IOException e) {
  355. throw new RuntimeException(e);
  356. }
  357. }
  358. }