关键词:服务集成,附件上传,表单附件,接口开发

O2OA允许用户使用接口来完成对流程、表单以及数据的相关操作。本文介绍如何使用接口来实现表单上传附件的功能。这样便能将流程附件的管理与其他业务进行整合,更方便业务的办理过程。


创建接口

在服务管理平台中创建一个接口,接口代码如下:

/*
* resources.getEntityManagerContainer() // 实体管理容器.
* resources.getContext() //上下文根.
* resources.getOrganization() //组织访问接口.
* requestText //请求内容.
* request //请求对象.
*/



/*
    传入的参数
    假设requestText = {
        "title" : "关于某某某的通知公告(标题,必填)",  //标题
        "from" : "办公室(来文单位,必填)",
        "to" : "qhsnynctrsc(OA的部门人事处ID,必填)",
        "date" : "2020-05-09(收文日期,选填)",
        "no" : "农123(字号,选填)",
        "key" : "nmt(文件加密私钥,选填,为空则认为没加密)",
        "contents" : [ {
            "filepath" : "http://xxxxx.com?file=aaaa.docx",
            "filename" : "aaaa.docx"
        } ],
        "slaves" : [ {
            "filepath" : "http://xxxxx.com?file=slaves.docx",
            "filename" : "slaves.docx"
        } ]
    }
*/
try{
    var result = {

    }

    print( "requestText="+requestText );

    var requestJson = JSON.parse(requestText);
    print( "type of requestJson = " + typeof( requestJson ));

    print( "type of requestJson = " + requestJson );
    if( typeof(requestJson) === "string" ){
        requestJson = JSON.parse(requestJson);
    }

    var workId = "b4cbb9a4-3410-45a4-9c0e-4dad0dcf94b4";  //流程文档的workId

    //上传附件-----------------------------------------------------------begin
    var token = getToken();  

    //print( "contents个数:" + requestJson.contents.length);
    //print( "slaves个数:" + requestJson.slaves.length);
    var contentsSite = "attachment"; //正文附件放置的附件区域
    var slavesSite = "attachment_1"; //普通附件放置的附件区域
    //处理contents
    var conArr = requestJson.contents;
    for(var i=0;i//print( "contents-filepath:" + conArr[i].filepath);
        //print( "contents-filename:" + conArr[i].filename);
        uploadAtt(workId,conArr[i].filepath,conArr[i].filename,token,contentsSite)
    }
    //处理slaves
    var slavesArr = requestJson.slaves;
    for(var j=0;j//print( "slaves-filepath:" + slavesArr[j].filepath);
        //print( "slaves-filename:" + slavesArr[j].filename);
        uploadAtt(workId,slavesArr[j].filepath,slavesArr[j].filename,token,slavesSite)
    }

    //上传附件-----------------------------------------------------------end

    result.state = "NMT0001";
    result.message = "成功";
    result.data = workId;



}catch(e){
    e.printStackTrace();
    result.state = "NMT0002";
    result.message = "失败";
    result.data = e.name + ": " + e.message
}

this.response.setBody(result,"application/json");


/*
    var workid = "b4cbb9a4-3410-45a4-9c0e-4dad0dcf94b4"; //workid:待办id
    var fileUrl = "http://127.0.0.1/x_desktop/js/base.js"; //附件下载链接
    var fileName = "base.js"; //附件名称
    var token = getToken();  
    site:附件放置区域
*/
function uploadAtt(workid,fileUrl,fileName,token,site){ //上传附件

    try {

        print("token=============================="+token);

        //实例化java类
        var LinkedHashMap = Java.type("java.util.LinkedHashMap");
        var HttpClientUtilsUpfile =  Java.type("com.z.custom.HttpClientUtilsUpfile"); 

        var headMap = new LinkedHashMap();
        headMap.put("x-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)");

        var uploadParams = new LinkedHashMap();
        uploadParams.put("fileName", fileName); //附件名称
        uploadParams.put("site", site); //附件区域
        uploadParams.put("extraParam","");


        //上传附件接口,传入文档的workid   
        var strurl = "http://127.0.0.1:20020/x_processplatform_assemble_surface/jaxrs/attachment/upload/work/" + workid;

        /*
        *方法功能:java模拟表单附件上传
        *strurl : 上传附件接口地址
        *fileUrl : 附件下载地址
        *fileName :附件名称
        *uploadParams : 上传附件接口参数
        *headMap : 表单头参数
        */
        HttpClientUtilsUpfile.getInstance().uploadUrlFileImpl(strurl, fileUrl,fileName,"file", uploadParams, headMap);


    } catch (e) {
        e.printStackTrace();
        e.printStackTrace();
        result.state = "NMT0002";
        result.message = "失败";
        result.data = e.name + ": " + e.message
    }

}


function getToken(){ //获取token
    var HttpClientUtilsUpfile =  Java.type("com.z.custom.HttpClientUtilsUpfile");
    //使用token进行Sso登陆
    var path = "http://127.0.0.1:20020/x_organization_assemble_authentication/jaxrs/sso";
    var login_uid = "13379254582"; //用户简称
    var client = "wwx"  //SSO名称
    var sso_key = "987654321"; //SSO密钥

    //获取token
    var token = HttpClientUtilsUpfile.getInstance().getToken(path,client,login_uid,sso_key);
    return token;
}

HttpClientUtilsUpfile类代码

接口中引用了com.z.custom.HttpClientUtilsUpfile中的java代码去实现模拟表单上传附件,HttpClientUtilsUpfile类代码如下:

package com.z.custom;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.x.base.core.project.tools.Crypto;
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.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
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 javax.crypto.CipherInputStream;

public class HttpClientUtilsUpfile {
    public static final int THREAD_POOL_SIZE = 5;

    public interface HttpClientDownLoadProgress {
        public void onProgress(int prgetInstanceogress);
    }

    private static HttpClientUtilsUpfile httpClientDownload;

    private ExecutorService downloadExcutorService;

    private HttpClientUtilsUpfile() {
        downloadExcutorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
    }

    public static HttpClientUtilsUpfile getInstance() {
        if (httpClientDownload == null) {
            httpClientDownload = new HttpClientUtilsUpfile();
        }
        return httpClientDownload;
    }

    /**
     * 下载文件
     *
     * @param url
     * @param filePath
     */
    public void download(final String url, final String filePath) { downloadExcutorService.execute(new Runnable() {
            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() {
            public void run() {
                httpDownloadFile(url, filePath, progress, null);
            }
        });
    }

    /**
     *下载文件
     *
     * @param url
     * @param filePath
     */
    private void httpDownloadFile(String url, String filePath,
                                  HttpClientDownLoadProgress progress, Map headMap) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            setGetHead(httpGet, headMap);
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            try {
                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();
            }
        }
    }

    /**
     * 发送Get请求
     *
     * @param url
     * @return
     */
    public String httpGet(String url) {
        return httpGet(url, null);
    }

    /**
     * 发送Get请求
     *
     * @param url
     * @return
     */
    public String httpGet(String url, Map headMap) {
        String responseContent = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            setGetHead(httpGet, headMap);
            try {
                System.out.println(response1.getStatusLine());
                HttpEntity entity = response1.getEntity();
                responseContent = getRespString(entity);
                System.out.println("debug:" + responseContent);
                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 paramsMap) {
        return httpPost(url, paramsMap, null);
    }

    /**
     * 发送Post请求
     *
     * @param url
     * @param paramsMap
     * @return
     */
    public String httpPost(String url, Map paramsMap, Map 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 {
                System.out.println(response.getStatusLine());
                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();
            }
        }
        System.out.println("responseContent = " + responseContent);
        return responseContent;
    }

    /**
     * 设置Post请求的HttpHeader
     *
     * @param httpPost
     * @param headMap
     */
    private void setPostHead(HttpPost httpPost, Map headMap) {
        if (headMap != null && headMap.size() > 0) {
            Set keySet = headMap.keySet();
            for (String key : keySet) {
                httpPost.addHeader(key, headMap.get(key));
            }
        }
    }

    /**
     * 设置Get请求的HttpHeader
     *
     * @param httpGet
     * @param headMap
     */
    private void setGetHead(HttpGet httpGet, Map headMap) {
        if (headMap != null && headMap.size() > 0) {
            Set 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 params, Map 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();
            // add the file params
            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();
        }
        System.out.println("resp=" + respStr);
        return respStr;
    }


    /**
     * 上传文件的实现方法
     *
     * @param serverUrl
     * @param urlFilePath
     * @param urlFileName
     * @param serverFieldName
     * @param params
     * @return
     * @throws Exception
     */
    public String uploadUrlFileImpl(String serverUrl, String urlFilePath,String urlFileName,String serverFieldName, Map params, Map 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(urlFilePath));
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
            // add the file params
            BufferedInputStream in = getFileInputStream(urlFilePath);
            InputStreamBody inputStreamBody = new InputStreamBody(in,urlFileName);
            multipartEntityBuilder.addPart(serverFieldName, inputStreamBody);
            setUploadParams(multipartEntityBuilder, params);
            HttpEntity reqEntity = multipartEntityBuilder.build();
            httppost.setEntity(reqEntity);

            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                respStr = getRespString(resEntity);
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        System.out.println("resp=" + respStr);
        return respStr;
    }

    /**
     * 上传文件的实现方法
     * 上传前 需要解密
     * @param serverUrl  上传附件服务
     * @param urlFilePath  附件URL
     * @param urlFileName  附件名称
     * @param serverFieldName
     * @param params  附件参数
     * @param paramshead  文件头
     * @param skey  密钥
     * @param siv  向量
     * @return
     * @throws Exception
     */
    public String uploadByteFileImpl(String serverUrl, String urlFilePath,String urlFileName,String serverFieldName, Map params, Map paramshead,String skey,String siv)
            throws Exception {
        String respStr = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost(serverUrl);
            setPostHead(httppost, paramshead);
            //ileBody binFileBody = new FileBody(new File(urlFilePath));
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
            // add the file params

            BufferedInputStream fis = getFileInputStream(URLDecoder.decode(urlFilePath, "UTF-8" ));;
            //解密
            CipherInputStream cis = AESFile.decryptedFile(fis,skey,siv);

            if(cis != null){
                //InputStream inputByteFile = new ByteArrayInputStream(byteFile);
                //BufferedInputStream in = new BufferedInputStream(inputByteFile);
                InputStreamBody inputStreamBody = new InputStreamBody(cis,urlFileName);
                multipartEntityBuilder.addPart(serverFieldName, inputStreamBody);
                setUploadParams(multipartEntityBuilder, params);
                HttpEntity reqEntity = multipartEntityBuilder.build();
                httppost.setEntity(reqEntity);

                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    System.out.println(response.getStatusLine());
                    HttpEntity resEntity = response.getEntity();
                    respStr = getRespString(resEntity);
                    EntityUtils.consume(resEntity);
                } finally {
                    response.close();
                }
            }else{
                respStr = "解密失败";
            }

        } finally {
            httpclient.close();
        }
        System.out.println("resp=" + respStr);
        return respStr;
    }

    /**
     * 设置上传时的参数
     *
     * @param multipartEntityBuilder
     * @param params
     */
    private void setUploadParams(MultipartEntityBuilder multipartEntityBuilder,
                                 Map params) {
        if (params != null && params.size() > 0) {
            Set keys = params.keySet();
            for (String key : keys) {
                multipartEntityBuilder.addPart(key, new StringBody(params.get(key),ContentType.APPLICATION_JSON));

            }
        }
    }

    /**
     * 获取响应内容
     *
     * @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();
    }
    /**
     * 设置Post请求发送的参数
     *
     * @param httpPost
     * @param paramsMap
     * @throws Exception
     */
    private void setPostParams(HttpPost httpPost, Map paramsMap)
            throws Exception {
        if (paramsMap != null && paramsMap.size() > 0) {
            List nvps = new ArrayList();
            Set keySet = paramsMap.keySet();
            for (String key : keySet) {
                nvps.add(new BasicNameValuePair(key, paramsMap.get(key)));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        }
    }

    /**
     * 发送Post请求
     * @param url
     * @param param
     * @return
     */
    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();
            }
        }
        System.out.println(result);
        return result;
    }

    /**
     * 根据ssoToken获取人员认证后的真实xtoken
     * @param url
     * @param client
     * @param login_uid
     * @param sso_key
     * @return
     */
    public String getToken(String url, String client, String login_uid, String sso_key ) {
        long time = new Date().getTime();
        String xtoken = null;

        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 = getInstance().sendPost( url,string );
        System.out.println("sso response: " + str );
        try {
            JsonElement jsonObj = new JsonParser().parse(str);
            if( jsonObj != null && jsonObj.getAsJsonObject().get("data") != null ){
                JsonObject data = jsonObj.getAsJsonObject().get("data").getAsJsonObject();
                System.out.println("getToken: " + data.get("token"));
                return data.get("token").getAsString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private BufferedInputStream getFileInputStream(String urlPath) {
        BufferedInputStream bin=null;
        URL url;
        try {
            url = new URL(urlPath);
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            httpURLConnection.setConnectTimeout(1000*5);
            httpURLConnection.setRequestMethod("GET");
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
            bin = new BufferedInputStream(httpURLConnection.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bin;
    }

    private InputStream getInputStream(String urlPath) {
        InputStream bin=null;
        URL url;
        try {
            url = new URL(urlPath);
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            httpURLConnection.setConnectTimeout(1000*5);
            httpURLConnection.setRequestMethod("GET");
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
            bin = httpURLConnection.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bin;
    }

    public static void main(String[] args) throws IOException, Exception {

    }
}

推荐文章:

系统配置-流程催办提醒配置
2021-02-25
O2OA平台设计了灵活的消息提醒数据交互方式,开发者可以根据自己的需要,来消费消息提醒数据,也可以将消息提醒数据接入到Kafka消息中间件来实现消息的准实时提醒
系统配置-主要配置文件说明
2021-02-25
O2OA开发平台有比较多的配置文件,目前多数配置已经已经可以直接在WEB端进行修改。本章节主要讲述O2OA信息化系统开发平台支持的配置文件以及各项配置相关的用途
平台维护-流转中工作数据的查询、操作和管理
2021-02-26
O2OA提供平台数据维护应用,用于对平台应用类数据的维护操作,用户可以在应用市场选择安装平台维护应用。本篇主要简单介绍流转中工作数据的查询、操作和管理功能。
会议管理
2021-02-19
O2OA会议管理系统可以对组织的会议室,会议申请进行管理,会议申请也可以关联相关的流程,表单和流程可以由开发者根据企业需求自行定义。
开发知识-O2OA平台启用Eruda进行移动端调试
2021-02-07
//reload--是否重新加载页面。true表示重新加载页面;默认falseo2.debug(reload);
移动办公-创建自定义企业微信应用
2021-02-25
O2OA平台拥有配套的原生开发的安卓和IOS移动APP,可以以自建应用的方式集成到企业微信,同步企业微信的企业通讯录作为本地组织人员架构,并且可以将待办等通知直
Custom应用前端代码编译
2021-03-15
O2OA允许用户对服务进行扩展,可以修改原来的代码,但是我们更推荐用户增加新的war包,jar包,根据O2OA的源码结构新增服务和应用来进行业务的扩展。本文主要
快速入门-流程表单载入基础数据
2021-02-07
表单载入数据以联系电话为例:联系电话字段默认值写脚本:returnthis.session.user.mobile(脚本详解在下方)即可人员基础信息当前用户信息
CRM客户管理使用手册
2021-02-19
O2OA包含一个简单的开源CRM客户信息管理系统,提供客户信息管理,客户线索管理,公海管理,联系人管理以及跟进信息和数据统计功能。O2OA是一款以流程管理为核心
创建组织架构及个人信息
2021-02-19
本篇主要简单讲述初次使用O2OA如何创建人员信息,组织职务信息以及组织成员和组织职务管理的实际意义及使用场景。

results matching ""

    No results matching ""