EKP jd 上传附件(四) soap+xml

一. jd Demo

package com.landray.kmss.gemdale.webservice;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;



import org.apache.http.entity.StringEntity;



public class WebServiceTest {

	private static String url = "http://:8080/sys/webservice/commonWorkflowService?wsdl";
	private static String localUrl = "http://localhost:8080/sys/webservice/commonWorkflowService?wsdl";
	private static String targetNamespace = "http:///";

	public static void main(String[] args) throws IOException {
		ICommonWorkflowService webService = new ICommonWorkflowServiceProxy(url);
		// String fd_id = webService.addReview(createForm());
		// System.out.println(fd_id);
		String result = doPostSoap();
		System.out.println(result);
	}

	public static String doPostSoap() throws IOException,Exception {
		//为服务请求方请求服务时服务器运行时间,格式为 YYYYMMDDHHMMSS
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		String ServiceTime = sf.format(date);		//传入当前时间、
		byte[] bytes = file2bytes("F:\\" + "ceshi2.txt");
		String fileBase64 = Base64.encodeBase64String(bytes);
				
		String soapXml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 
				"<SOAP-ENV:Envelope\n" + 
				"    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 
				"    xmlns:web=\"http://webservice.gemdale.kmss.landray.com/\">\n" + 
				"    <SOAP-ENV:Header/>\n" + 
				"    <SOAP-ENV:Body>\n" + 
				"        <web:addReview>\n" + 
				"            <arg0>\n" + 
				"                <attachmentForms>\n" + 
				"                     <fdKey>"+"fd_file"+"</fdKey>\n" + 
				"                     <fdFileName>"+"ceshi2.txt"+"</fdFileName>\n" + 
//				"                     <fdAttachment>"+"VGhyb3VnaCB0aGUgZGFya2VzdCBhbGxleXMgYW5kIGxvbmVsaWVzdCB2YWxsZXlz"+"</fdAttachment>\n" +
                "      				  <fdAttachment>"+fileBase64+"</fdAttachment>\n" +				
				"                </attachmentForms>\n" + 
				"                <docCreator>admin</docCreator>\n"+
				"                <docStatus>10</docStatus>\n"+
				"                <docSubject>"+ServiceTime+"_add"+"</docSubject>\n"+
				"                <fdTemplateKeyword>SGSY-XQ</fdTemplateKeyword>\n"+
				"            </arg0>\n" + 
				"         </web:addReview>\n" + 
				"    </SOAP-ENV:Body>\n" + 
				"</SOAP-ENV:Envelope>\n" + 
				"";
		
		int socketTimeout = 10000;// 请求超时时间
		int connectTimeout = 10000;// 传输超时时间
		final String soap = "text/xml;charset=UTF-8";
		// 创建HttpClient
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		// 创建Post请求
		HttpPost httpPost = new HttpPost(localUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
				.setConnectTimeout(connectTimeout).build();
		httpPost.setConfig(requestConfig);
		// 设置Post请求报文头部
		httpPost.setHeader("Content-Type", soap);
		httpPost.setHeader("SOAPAction", "");
		// 添加报文内容
		StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
		httpPost.setEntity(data);
		try {
			// 执行请求获取返回报文
			CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				// 打印响应内容
				return EntityUtils.toString(httpEntity, "UTF-8");
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	

	/*
	 * 创建流程文档
	 */
	public static ReviewHandlerParamter createForm() throws IOException {
		ReviewHandlerParamter reviewHandlerParamter = new ReviewHandlerParamter();
		// 本地环境模板
		reviewHandlerParamter.setFdTemplateKeyword("SGSY-XQ");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		reviewHandlerParamter.setDocSubject(df.format(new Date()) + " add 附件上传");
		reviewHandlerParamter.setDocCreator("admin");

		AttachmentForm[] attForms = createAllAtts();
		reviewHandlerParamter.setAttachmentForms(attForms);

		return reviewHandlerParamter;
	}

	/*
	 * 创建附件列表
	 */
	static AttachmentForm[] createAllAtts() throws IOException {

		AttachmentForm[] attForms = new AttachmentForm[3];
		String fileName = "TEST1.rtf";
		AttachmentForm attForm01 = createAtt(fileName);
		attForms[0] = attForm01;
		return attForms;
	}

	/*
	 * 创建附件对象
	 */
	static AttachmentForm createAtt(String fileName) throws IOException {
		AttachmentForm attForm = new AttachmentForm();
		attForm.setFdFileName(fileName); // 设置附件关键字,表单模式下为附件控件的id
		attForm.setFdKey("fd_file");

		byte[] data = file2bytes("F:\\" + "FILE-10-35-21-34-6333872054413443-1623835937850.rtf");
		attForm.setFdAttachment(data);

		return attForm;
	}

	/*
	 * 将文件转换为字节编码
	 */
	static byte[] file2bytes(String fileName) throws IOException {
		InputStream in = new FileInputStream(fileName);
		byte[] data = new byte[in.available()];

		try {
			in.read(data);
		} finally {
			try {
				in.close();
			} catch (IOException ex) {
			}
		}

		return data;
	}

}

 二. ny Demo

package com.landray.kmss.sec.org.util;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.landray.kmss.util.StringUtil;

/**
 * 模块基础工具类
 */
public class SecOrgPushUtil{
	
	/**
	* 同步HttpPost请求发送SOAP格式的消息
	*
	* @param webServiceURL
	*            WebService接口地址
	* @param soapXml
	*            消息体
	* @param soapAction
	*            soapAction
	* @param soapType
	*            soap版本
	* @return
	*/
	static int socketTimeout = 10000;// 请求超时时间
	static int connectTimeout = 10000;// 传输超时时间
	static final String soap = "text/xml;charset=UTF-8";
	public static String doPostSoap(String webServiceURL, String soapXml,String soapAction) {
	   // 创建HttpClient
	   HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
	   CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
	   // 创建Post请求
	   HttpPost httpPost = new HttpPost(webServiceURL);
	   // 设置请求和传输超时时间
	   RequestConfig requestConfig = RequestConfig.custom()
	           .setSocketTimeout(socketTimeout)
	           .setConnectTimeout(connectTimeout).build();
	   httpPost.setConfig(requestConfig);
	   // 设置Post请求报文头部
	   httpPost.setHeader("Content-Type", soap);
	   httpPost.setHeader("SOAPAction", soapAction);
	   // 添加报文内容
	   StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
	   httpPost.setEntity(data);
	   try {
	       // 执行请求获取返回报文
	       CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
	       HttpEntity httpEntity = response.getEntity();
	       if (httpEntity != null) {
	           // 打印响应内容
	           return EntityUtils.toString(httpEntity, "UTF-8");
	       }
	       // 释放资源
	       closeableHttpClient.close();
	   } catch (ClientProtocolException e) {
	       e.printStackTrace();
	   } catch (IOException e) {
	       e.printStackTrace();
	   }
	   return null;
	 }
	
	//截取<Response>(.*)</Response>
	public static JSONObject unResponseXml(String str) {
		JSONObject jsonGroup = new JSONObject();
		String regex = "<Response>(.*)</Response>";
	    Pattern pattern = Pattern.compile(regex);
	    Matcher matcher = pattern.matcher(str);//匹配类
	    while (matcher.find()) {
	        String group = matcher.group(1);
	        group = StringEscapeUtils.unescapeXml(group);
	        if(StringUtil.isNotNull(group)) {
	        	jsonGroup = JSONObject.parseObject(group);
	        }
	    }
		return jsonGroup;
	}
	
	//利用List的contains方法循环遍历,重新排序,只添加一次数据,避免重复:
	private static List<String> removeDuplicate(List<String> list) {
	    List<String> result = new ArrayList<String>(list.size());
	    for (String str : list) {
	        if (!result.contains(str)) {
	            result.add(str);
	        }
	    }
	    list.clear();
	    list.addAll(result);
		return result;
	}
}

  推送接口

	/**
	 * 调用推送接口
	 * @param jsonAll
	 * @param SourceSysID
	 * @param jsonAll 
	 * @param ServiceID 
	 * @param SerialNO 
	 * @param configLink 
	 * @return 
	 */
	public String pushJsonData(JSONObject jsonAll, String SourceSysID, String ServiceID, String SerialNO, String configLink) {
		
		String result = "";
		try {
			//为服务请求方请求服务时服务器运行时间,格式为 YYYYMMDDHHMMSS
	    	Date date = new Date();
	        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
	        String ServiceTime = sf.format(date);		//传入当前时间
		 	
	        String soapXml="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 
	        		"<SOAP-ENV:Envelope\n" + 
	        		"    xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" + 
	        		"    xmlns:impl=\"http://smile.web.com/webservice\">\n" + 
	        		"    <SOAP-ENV:Header/>\n" + 
	        		"    <SOAP-ENV:Body>\n" + 
	        		"        <impl:esbServiceOperation>\n" + 
	        		"            <arg0>\n" + 
	        		"                <Service>\n" + 
	        		"                    <Route>\n" + 
	        		"                        <ServiceID>"+ServiceID+"</ServiceID>\n" + 
	        		"                        <SerialNO>"+SerialNO+"</SerialNO>\n" + 
	        		"                        <ServiceTime>"+ServiceTime+"</ServiceTime>\n" + 
	        		"                        <SourceSysID>"+SourceSysID+"</SourceSysID>\n" + 
	        		"                    </Route>\n" + 
	        		"                    <Data>\n" + 
	        		"                        <Request>"+jsonAll+"</Request>\n" + 
	        		"                        <Control/>\n" + 
	        		"                    </Data>\n" + 
	        		"                </Service>\n" + 
	        		"            </arg0>\n" + 
	        		"        </impl:esbServiceOperation>\n" + 
	        		"    </SOAP-ENV:Body>\n" + 
	        		"</SOAP-ENV:Envelope>\n" + 
	        		"";
	       
	        //result= doPostSoap("http://10.62.16.56:7080/ESBWS/services/RequestInPort", soapXml, "", soap);
	        result= SecOrgPushUtil.doPostSoap(configLink, soapXml, "");
	        //result= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><impl:esbServiceOperationResponse xmlns:impl=\"http://impl.webservice.eis.com/\"><return><Service><Route><ServiceID>10000000000001</ServiceID><SerialNO>202203230100000001</SerialNO><ServiceTime>20220323090756</ServiceTime><SourceSysID>0100</SourceSysID><ServiceResponse><Status>COMPLETE</Status><Code>S000M000</Code><Desc>通信正常完成</Desc></ServiceResponse></Route><Data><Request>{"sourceSysId":"0100","dataType":"40000001","data":{"f_sjzzbm_cq":"1","f_sjzzbm_xz":"1","f_zczb":"475738.99","f_clrq":"1993-08-21","f_zzzjm":"SZNYJT","f_zzyh":[{"f_bzmc":"CNY","f_yhmc":"1","f_zhmc":"zs2022","f_zhlx":"1","f_bzbm":"CNY","f_yhhh":"1234500","f_zhhm":"zs1980"}],"f_sfstdw":"1","f_tgt_sys":"","f_bmxx":[{"f_name":"信息部","f_bmcj":"01","f_code":"nyjt2022","f_bmlx":"1","f_sjbmmc":"1","f_sjbmbm":"1","f_bmjc":"XXB"}],"f_ename":"SZNYJTYSGS","f_sjzzmc_xz":"组织1","f_jyfw":"中国大陆","f_zcbzbm":"CNY","f_zzcj_cq":"1","f_code":"SE0000000","f_dqrq":"2043-08-21","f_ssywbk":"FD","f_zzjc":"深圳能源集团","f_sjzzmc_cq":"组织1","f_zzcj_xz":"01","f_gj":"CHN","f_name":"深圳能源集团股份有限公司","f_id":"17fb44b6bdfc92dcb03cb544be0a732e","f_jwzjbm":"nyjt","f_zcdz":"深圳市福田区福田街道金田路2026号能源大厦北塔楼9层、29-31层、34-41层","f_qyfr":"王平洋","f_zcbzmc":"人民币元","f_sf":"2","f_zzlx":"1","f_jthjcgbl":"20.0","f_zzxz":"1","f_tyshxydm":"91440300192241158P","f_zzzt":"1"},"msgId":"202203230100000001"}</Request><Response>{"msg":"","code":"0","data":[{"f_code":"SE0000000","f_id":"17fb44b6bdfc92dcb03cb544be0a732e","f_status":"0"}]}</Response></Data></Service></return></impl:esbServiceOperationResponse></soapenv:Body></soapenv:Envelope>";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

 

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><ns1:addReviewResponse xmlns:ns1="http://webservice.gemdale.kmss.landray.com/">
<return>{"processId":"1847b4939130dd0a06ac5e8426ba388f","code":1,"success":true,"message":"流程成功启动"}</return>
</ns1:addReviewResponse>
</soap:Body></soap:Envelope>

  

  

 

 

posted @ 2022-11-07 12:59  CrushGirl  阅读(66)  评论(0编辑  收藏  举报