import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class HttpClientUtil { public static final int THREAD_POOL_SIZE = 5; public interface HttpClientDownLoadProgress { public void onProgress(int progress); } private static HttpClientUtil httpClientDownload; private ExecutorService downloadExcutorService; private HttpClientUtil() { downloadExcutorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); } public static HttpClientUtil getInstance() { if (httpClientDownload == null) { httpClientDownload = new HttpClientUtil(); } return httpClientDownload; } /** * @param url * @param filePath */ public void download(final String url, final String filePath) { downloadExcutorService.execute(new Runnable() { @Override public void run() { httpDownloadFile(url, filePath, null, null); } }); } /** * * @param url * @param filePath * @param progress */ public void download(final String url, final String filePath, final HttpClientDownLoadProgress progress) { downloadExcutorService.execute(new Runnable() { @Override public void run() { httpDownloadFile(url, filePath, progress, null); } }); } /** * @param url * @param filePath */ private void httpDownloadFile(String url, String filePath, HttpClientDownLoadProgress progress, Map<String, String> headMap) { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet(url); setGetHead(httpGet, headMap); CloseableHttpResponse response1 = httpclient.execute(httpGet); try { System.out.println(response1.getStatusLine()); HttpEntity httpEntity = response1.getEntity(); long contentLength = httpEntity.getContentLength(); InputStream is = httpEntity.getContent(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int r = 0; long totalRead = 0; while ((r = is.read(buffer)) > 0) { output.write(buffer, 0, r); totalRead += r; if (progress != null) { progress.onProgress((int) (totalRead * 100 / contentLength)); } } FileOutputStream fos = new FileOutputStream(filePath); output.writeTo(fos); output.flush(); output.close(); fos.close(); EntityUtils.consume(httpEntity); } finally { response1.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * @param url * @return */ public String httpGet(String url) { return httpGet(url, null); } /** * http get * * @param url * @return */ public String httpGet(String url, Map<String, String> headMap) { String responseContent = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response1 = httpclient.execute(httpGet); setGetHead(httpGet, headMap); try { HttpEntity entity = response1.getEntity(); responseContent = getRespString(entity); EntityUtils.consume(entity); } finally { response1.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return responseContent; } public String httpPost(String url, Map<String, String> paramsMap) { return httpPost(url, paramsMap, null); } /** * @param url * @param paramsMap * @return */ public String httpPost(String url, Map<String, String> paramsMap, Map<String, String> headMap) { String responseContent = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); setPostHead(httpPost, headMap); setPostParams(httpPost, paramsMap); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); responseContent = getRespString(entity); EntityUtils.consume(entity); } finally { response.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * @param httpPost * @param headMap */ private void setPostHead(HttpPost httpPost, Map<String, String> headMap) { if (headMap != null && headMap.size() > 0) { Set<String> keySet = headMap.keySet(); for (String key : keySet) { httpPost.addHeader(key, headMap.get(key)); } } } /** * * * @param httpGet * @param headMap */ private void setGetHead(HttpGet httpGet, Map<String, String> headMap) { if (headMap != null && headMap.size() > 0) { Set<String> keySet = headMap.keySet(); for (String key : keySet) { httpGet.addHeader(key, headMap.get(key)); } } } /** * * * @param serverUrl ַ * @param localFilePath * * @param serverFieldName * @param params * @return * @throws Exception */ public String uploadFileImpl(String serverUrl, String localFilePath, String serverFieldName, Map<String, String> params, Map<String, String> paramshead) throws Exception { String respStr = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost(serverUrl); setPostHead(httppost, paramshead); FileBody binFileBody = new FileBody(new File(localFilePath)); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addPart(serverFieldName, binFileBody); setUploadParams(multipartEntityBuilder, params); HttpEntity reqEntity = multipartEntityBuilder.build(); httppost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity resEntity = response.getEntity(); respStr = getRespString(resEntity); EntityUtils.consume(resEntity); } finally { response.close(); } } finally { httpclient.close(); } return respStr; } /** * @param multipartEntityBuilder * @param params */ private void setUploadParams(MultipartEntityBuilder multipartEntityBuilder, Map<String, String> params) { if (params != null && params.size() > 0) { Set<String> keys = params.keySet(); for (String key : keys) { multipartEntityBuilder.addPart(key, new StringBody(params.get(key), ContentType.APPLICATION_JSON)); } } } /** * String * * @param entity * @return * @throws Exception */ private String getRespString(HttpEntity entity) throws Exception { if (entity == null) { return null; } InputStream is = entity.getContent(); StringBuffer strBuf = new StringBuffer(); byte[] buffer = new byte[4096]; int r = 0; while ((r = is.read(buffer)) > 0) { strBuf.append(new String(buffer, 0, r, "UTF-8")); } return strBuf.toString(); } /** * @param httpPost * @param paramsMap * @throws Exception */ private void setPostParams(HttpPost httpPost, Map<String, String> paramsMap) throws Exception { if (paramsMap != null && paramsMap.size() > 0) { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); Set<String> keySet = paramsMap.keySet(); for (String key : keySet) { nvps.add(new BasicNameValuePair(key, paramsMap.get(key))); } httpPost.setEntity(new UrlEncodedFormEntity(nvps)); } } public static String sendPost(String url, String token, String paramsStr) throws IOException, Exception { // POST请求 CloseableHttpClient httpclient = HttpClients.createDefault(); // 请求参数 StringEntity postingString = new StringEntity(paramsStr, "utf-8"); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(postingString); // 请求头设置 httpPost.addHeader("Cookie", "x-token=" + token); httpPost.addHeader("accept", "*/*"); httpPost.addHeader("connection", "Keep-Alive"); httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); httpPost.addHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 执行请求 CloseableHttpResponse execute = httpclient.execute(httpPost); // 处理返回结果 String restStr = IOUtils.toString(execute.getEntity().getContent(), "utf-8"); return restStr; } public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(" POST" + e); e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } public static void main(String[] args) throws IOException, Exception { String token = ""; // 第一步:单点登入 String path = "http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/sso"; // 单点路径 long time = new Date().getTime(); // 当前时间 String login_uid = "kfry"; // 用户id String sso_key = "12345678"; // 加密密码 String xtoken = null; String client = "sso"; // sso配置名称 try { xtoken = Crypto.encrypt(login_uid + "#" + time, sso_key); System.out.println(xtoken); } catch (Exception e1) { e1.printStackTrace(); } String string = "{"token": " + xtoken + ", "client": " + client + "}"; String str = HttpClientUtil.getInstance().sendPost(path, string); JsonParser parser = new JsonParser(); JsonObject object = null; object = (JsonObject) parser.parse(str); JsonObject value = object.get("data").getAsJsonObject(); token = value.get("token").getAsString(); // 第二步:创建流程 String processId = "e4c77038-2964-498f-9d37-1635e26639eb";// 流程id path = "http://127.0.0.1:20020/x_processplatform_assemble_surface/jaxrs/work/process/"+processId;// 流程创建路径 String datagrid = "{"data": [{" + " " + " "col1": { "textfield1": "1" }," + " "col2": { "textfield2": "2" }," + " "col3": { "textfield3": "3" }," + " "col4": { "textfield4": "4" }," + " "col5": { "textfield5": "5" }" + " }," + " {" + " "col1": { "textfield1": "11" }," + " "col2": { "textfield2": "12" }," + " "col3": { "textfield3": "13" }," + " "col4": { "textfield4": "14" }," + " "col5": { "textfield5": "15" }" + " }]}"; // 数据网格数据 JsonParser parser1 = new JsonParser(); JsonObject jsonObj = parser1.parse(datagrid).getAsJsonObject(); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("latest", false); jsonObject.addProperty("title", "演示系统grid测试(2021-02-22)");// 标题 jsonObject.addProperty("identity", "开发人员@932ca9a4-4c80-4a29-9e44-aed85afe21d3@I"); // 创建者身份 JsonObject jsonObjectdata = new JsonObject(); jsonObjectdata.addProperty("subject", "演示系统grid测试2021-02-22"); // 标题 jsonObjectdata.addProperty("calendar", "2020-04-13 12:00:00");// 表单 上的字段名“calendar” jsonObjectdata.addProperty("explain", "演示系统grid测试"); // 表单 上的字段名“explain” jsonObjectdata.add("datagrid", jsonObj); // 表单上的数据网格 jsonObject.add("data", jsonObjectdata); // 创建流程 String strflow = HttpClientUtil.getInstance().sendPost(path, token, jsonObject.toString()); String work = ""; // 获取返回的workid JsonParser parser2 = new JsonParser(); JsonObject object2 = null; object2 = (JsonObject) parser2.parse(strflow); JsonArray value2 = object2.getAsJsonArray("data"); object2 = value2.get(0).getAsJsonObject(); work = object2.get("work").getAsString(); System.out.println("work=" + work); // 上传附件 try { Map<String, String> uploadParams = new LinkedHashMap<String, String>(); uploadParams.put("fileName", "明天.docx"); // 附件名 uploadParams.put("site", "attachment"); // 表单上的附件控件标识 uploadParams.put("extraParam", ""); Map<String, String> headMap = new HashMap<String, String>(); headMap.put("Cookie", "x-token=" + token); // 单点token headMap.put("accept", "*/*"); headMap.put("connection", "Keep-Alive"); headMap.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); String attUrl = "http://127.0.0.1:20020/x_processplatform_assemble_surface/jaxrs/attachment/upload/work/" + work; String filePath = "F:Download明天.docx"; HttpClientUtil.getInstance().uploadFileImpl(attUrl, filePath, "file", uploadParams, headMap); } catch (Exception e) { e.printStackTrace(); } } }
二、功能代码介绍
第一步:单点登入
一、创建SSO配置,请参考组织管理里中的SSO管理
二、通过下面代码获取token.
注意:下面代码中要调整的内容如下:
“path”:单点路径要根据实际服务器ip调整;
“login_uid”:单点用户id;
“sso_key”:加密解密密码;
“client”:sso配置名称;
String token = ""; //第一步:单点登入 String path = "http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/sso"; //单点路径 long time = new Date().getTime(); //当前时间 String login_uid = "kfry"; //用户id String sso_key = "12345678"; //加密密码 String xtoken = null; String client = "sso"; //sso配置名称 try { xtoken = Crypto.encrypt( login_uid + "#" + time, sso_key ); System.out.println(xtoken); } catch (Exception e1) { e1.printStackTrace(); } String string = "{"token": "+xtoken+", "client": " + client + "}"; String str = HttpClientUtil.getInstance().sendPost(path,string); JsonParser parser = new JsonParser(); JsonObject object = null; object =(JsonObject) parser.parse(str); JsonObject value =object.get("data").getAsJsonObject(); token= value.get("token").getAsString();
第二步:创建流程
一、设置x-token
注意:下面代码中要调整的内容如下:
“processId”: 流程id;
“path”:创建流程路径要根据实际服务器ip调整;
“title”:标题;
“identity”:创建者身份;
“subject”:标题;
...... // 第二步:创建流程 String processId = "e4c77038-2964-498f-9d37-1635e26639eb";// 流程id path = "http://127.0.0.1:20020/x_processplatform_assemble_surface/jaxrs/work/process/";// 流程创建路径 String datagrid = "{"data": [{" + " " + " "col1": { "textfield1": "1" }," + " "col2": { "textfield2": "2" }," + " "col3": { "textfield3": "3" }," + " "col4": { "textfield4": "4" }," + " "col5": { "textfield5": "5" }" + " }," + " {" + " "col1": { "textfield1": "11" }," + " "col2": { "textfield2": "12" }," + " "col3": { "textfield3": "13" }," + " "col4": { "textfield4": "14" }," + " "col5": { "textfield5": "15" }" + " }]}"; // 数据网格数据 JsonParser parser1 = new JsonParser(); JsonObject jsonObj = parser1.parse(datagrid).getAsJsonObject(); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("latest", false); jsonObject.addProperty("title", "演示系统grid测试(2021-02-22)");// 标题 jsonObject.addProperty("identity", "开发人员@932ca9a4-4c80-4a29-9e44-aed85afe21d3@I"); // 创建者身份 JsonObject jsonObjectdata = new JsonObject(); jsonObjectdata.addProperty("subject", "演示系统grid测试2021-02-22"); // 标题 jsonObjectdata.addProperty("calendar", "2020-04-13 12:00:00");// 表单 上的字段名“calendar” jsonObjectdata.addProperty("explain", "演示系统grid测试"); // 表单 上的字段名“explain” jsonObjectdata.add("datagrid", jsonObj); // 表单上的数据网格 jsonObject.add("data", jsonObjectdata); // 创建流程 String strflow = HttpClientUtil.getInstance().sendPost(path, token, jsonObject.toString()); String work = ""; // 获取返回的workid JsonParser parser2 = new JsonParser(); JsonObject object2 = null; object2 = (JsonObject) parser2.parse(strflow); JsonArray value2 = object2.getAsJsonArray("data"); object2 = value2.get(0).getAsJsonObject(); work = object2.get("work").getAsString(); ......
第三步:上传附件
一、设置x-token
二、设置workid
注意:下面代码中要调整的内容如下:
“fileName”:附件名
“site”:表单上的附件控件标识
“filePath”:附件路径
“attUrl”:附件接口要根据实际服务器ip调整
// 上传附件 try { Map<String, String> uploadParams = new LinkedHashMap<String, String>(); uploadParams.put("fileName", "明天.docx"); // 附件名 uploadParams.put("site", "attachment"); // 表单上的附件控件标识 uploadParams.put("extraParam", ""); Map<String, String> headMap = new HashMap<String, String>(); headMap.put("Cookie", "x-token=" + token); // 单点token headMap.put("accept", "*/*"); headMap.put("connection", "Keep-Alive"); headMap.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); String attUrl = "http://127.0.0.1:20020/x_processplatform_assemble_surface/jaxrs/attachment/upload/work/" + work; String filePath = "F:Download明天.docx"; HttpClientUtil.getInstance().uploadFileImpl(attUrl, filePath, "file", uploadParams, headMap); } catch (Exception e) { e.printStackTrace(); }
若有收获,就点个赞吧