在服务管理平台中创建一个接口,接口代码如下:
/* * 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://host?file=aaaa.docx", "filename" : "aaaa.docx" } ], "slaves" : [ { "filepath" : "http://host?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<conArr.length;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<slavesArr.length;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; }