需求描述:需要通过调用金蝶API接口实现指定单据的附件上传。本文以收料通知单为例,以Java代码示例进行讲解。
tips:阅读本文开始前,希望你是一名开发者同时阅读过:
https://vip.kingdee.com/article/87232573931076096
https://vip.kingdee.com/article/183394
首先我们需要了解金蝶附件上传接口是哪一个,如果不清楚,我们可以通过抓包的方式进行查看,但是如果看过上面的文章,我们可以找到上传接口: 你的服务器地址/FileUpLoadServices/FileService.svc/upload2attachment/?fileName=上传文件名称&fileId=&token=登录返回的token&last=true&dbid=数据中心id
请求体具体就是字节流的方式放入请求体,不同编程语言具体写法不一致,本文以Java为例最后会附上相关代码。
这样我们可以通过查询
SELECT * from T_BAS_FileServerFileInfo where ffilename='你上传的文件名'
可以查看到你上传的文件(使用的是文件服务器存储到服务器地址)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
上传成功了,肯定有一个疑问,这仅仅上传了文件,但是并没有在具体单据显示啊,其实看了第一个tips的能发现,附件是通过T_BAS_Attachment表与实际业务关联起来的。所以下面需要通过保存api将附件信息关联起来。
下面就和保存一样,请求url其实就是保存的。通过分析分析官方的url我们可以得出请求json格式:
{
"parameters": [
"BOS_Attachmen",
{
"NeedReturnFields": [],
"IsAutoSubmitAndAudi": "false",
"Creator": "",
"NeedUpDateFields": [],
"IsVerifyBaseDataField": "false",
"IsEntryBatchFill": "false",
"NumberSearch": "true",
"InterationFlags": "",
"ValidateFlag": "true",
"SubSystemId": "",
"IsDeleteEntry": "false",
"Model": {
"FInterID": "单据的fid",
"FEntryKey": " 单据体标识(如果是单据头,则传一个空格)",
"FEntryInterID": -1 单据体分录主键,单据头传-1,
"FBillNo": "单据体编号",
"FBillType": "你要上传附件表单",
"FAttachmentName": "上传文件的名字",
"FAttachmentSize": "上传文件的大小",
"FExtName": "拓展名",
"FAttachmentDes": "描述",
"FAttachmen": "",
"FFILEID": "通过步骤三返回的fileid",
"FCREATEMEN": " { "FUSERID", 10086}",
"FCreateTime": "上传时间"
}
}
]
}
然后进行保存就可以了通过查询
SELECT * from T_BAS_Attachment where fbillno in('你上传附件的单号')
然后通过前端附件就可以进行查看到上传的文件了
最后附上Java相关代码
public ResultVO upload(String token,String cookie, MultipartFile content, String formId,String fbillno) throws IOException {
System.out.println("cookie"+cookie);
ResultVO rv = new ResultVO();
List<ArrayList>fidList=new ArrayList<>();
String fid=null;
String fileName=null;
String fileId=null;
fileName=content.getOriginalFilename();
String jsonFormat = getExecuteBillQueryJsonFormatNew(formId,"FID", "FBillNo in('"+fbillno+"')");
//json格式化
JSONObject obj = JSON.parseObject(jsonFormat);
rv = executeBillQuery(cookie, obj);
if (rv != null) {
String[] l = JSON.parseObject(rv.getData().toString(), String[].class);
String[] n=JSON.parseObject(l[0], String[].class);
fid=n[0];
}
byte[] bytes=content.getBytes();
InputStream in = new ByteArrayInputStream(bytes);
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String url= "http://你的服务器地址/k3cloud/FileUpLoadServices/FileService.svc/upload2attachment/?filename="+fileName+"&fileid=&last=true&dbid="+kdCloundConfig.getAcctID()+"&token="+token+"";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("data",in);
String result2 = HttpRequest.post(url)
.form(paramMap)//表单内容
.timeout(20000)//超时,毫秒
.execute().body();
System.out.println("result2"+result2);
// System.out.println("FileId"+ JsonPath.read(result2, "$.Upload2AttachmentResult.FileId"));
fileId=JsonPath.read(result2, "$.Upload2AttachmentResult.FileId");
String saveurl = kdCloundConfig.getUrl() + kdCloundConfig.getSave();
//业务数据内码FInterID
//-1 FEntryInterID代码单据头附件,单据体附件需指定具体业务数据单据体内码,
//FBillNo 业务数据编码
//PUR_ReceiveBill 附件关联的业务对象Id
System.out.println("fid"+fid+"fbillno"+fbillno+"formId"+formId+"fileName"+fileName+content.getSize()+content.getOriginalFilename().substring(content.getOriginalFilename().lastIndexOf("."))+dateFormat.format(date));
JSONObject jsonObject = new JSONObject();
jsonObject.put("FUSERID",643384);
String data="{\n" +
"\t\"parameters\": [\n" +
"\t\t\"BOS_Attachment\",\n" +
"\t\t{\n" +
"\t\t\t\"NeedReturnFields\": [],\n" +
"\t\t\t\"IsAutoSubmitAndAudit\": \"false\",\n" +
"\t\t\t\"Creator\": \"\",\n" +
"\t\t\t\"NeedUpDateFields\": [],\n" +
"\t\t\t\"IsVerifyBaseDataField\": \"false\",\n" +
"\t\t\t\"IsEntryBatchFill\": \"false\",\n" +
"\t\t\t\"NumberSearch\": \"true\",\n" +
"\t\t\t\"InterationFlags\": \"\",\n" +
"\t\t\t\"ValidateFlag\": \"true\",\n" +
"\t\t\t\"SubSystemId\": \"\",\n" +
"\t\t\t\"IsDeleteEntry\": \"false\",\n" +
"\t\t\t\"Model\": {\n" +
"\t\t\t\t\"FInterID\": "+fid+",\n" +
"\t\t\t\t\"FEntryKey\": \" \",\n" +
"\t\t\t\t\"FEntryInterID\": -1,\n" +
"\t\t\t\t\"FBillNo\": \""+fbillno+"\",\n" +
"\t\t\t\t\"FBillType\": \""+formId+"\",\n" +
"\t\t\t\t\"FAttachmentName\": \""+fileName+"\",\n" +
"\t\t\t\t\"FAttachmentSize\": "+ (double)(Math.round(content.getSize()/1024))+",\n" +
"\t\t\t\t\"FExtName\": \""+content.getOriginalFilename().substring(content.getOriginalFilename().lastIndexOf("."))+"\",\n" +
"\t\t\t\t\"FAttachmentDes\": \"地磅上传\",\n" +
"\t\t\t\t\"FAttachment\": \"\",\n" +
"\t\t\t\t\"FFILEID\": \""+fileId+"\",\n" +
"\t\t\t\t\"FCREATEMEN\": "+jsonObject.toJSONString()+",\n" +
"\t\t\t\t\"FCreateTime\": \""+dateFormat.format(date)+"\"\n" +
"\t\t\t}\n" +
"\t\t}\n" +
"\t]\n" +
"}";
Map<String, Object> header = new HashMap<>();
header.put("Cookie", cookie);
String result = HttpUtil.httpPost(saveurl, header, data);
rv.setCode(ResultEnumErrorCode.SUCCESS.getCode());
rv.setMessage(ResultEnumErrorCode.SUCCESS.getMsg());
rv.setData(result);
logger.info("upload:formId:{}执行结果:{}", formId, result);
if (ObjectUtil.isEmpty(rv)) {
rv.setCode(ResultEnumErrorCode.SYSTEM_ERROR_B0001.getCode());
rv.setMessage(ResultEnumErrorCode.SYSTEM_ERROR_B0001.getMsg());
logger.error("upload:formId:{}系统执行出错!", formId);
}
return rv;
}
最后本文提供了上传思路,实际业务还需要根据实际情况调整。