1.分布式锁

相关:https://dev.kingdee.com/index/docsNew/d1b4f435-a5f8-4441-83ac-5059bea51815

  1. import kd.bos.dlock.DLock;
  2. DLock lock = DLock.create("fi/gl/N20180503-8808", "凭证N20180503-8808");
  3. // 一直等待直到获得锁
  4. lock.lock();
  5. try {
  6. // 获取锁信息:
  7. // {key: fi/gl/N20180503-8808, desc: 凭证N20180503-8808, owner: 163388da76b0046, waitingLocks: 0, createTime: 2018-05-08 11:35:50.087}
  8. System.out.println(DLock.getLockInfo("fi/gl/N20180503-8808"));
  9. // 获取所有锁信息
  10. // System.out.println(DLock.getAllLockInfo());
  11. // ...
  12. } finally {
  13. //一般放入finally
  14. lock.unlock();
  15. }

2.DynamicObject 转成 Map对象

  1. Map<String, Object> objectMap = new HashMap();
  2. if (dynamicObject != null) {
  3. DataEntityPropertyCollection dynamicObjects = dynamicObject.getDataEntityType().getProperties();
  4. Iterator var3 = dynamicObjects.iterator();
  5. while(var3.hasNext()) {
  6. IDataEntityProperty iDataEntityProperty = (IDataEntityProperty)var3.next();
  7. String property = iDataEntityProperty.getName().toLowerCase();
  8. objectMap.put(property, dynamicObject.get(property));
  9. }
  10. }
  11. return objectMap;

3.查询单据所有字段信息(需要查询两次)

  1. BusinessDataServiceHelper.load(id[], BusinessDataServiceHelper.newDynamicObject(单据标识).getDynamicObjectType());

4.file转base64

  1. package wens.fi.jyt.commmon.utils;
  2. import kd.bos.cache.CacheFactory;
  3. import kd.bos.cache.tempfile.TempFileCacheDownloadable;
  4. import kd.bos.exception.KDBizException;
  5. import sun.misc.BASE64Decoder;
  6. import sun.misc.BASE64Encoder;
  7. import java.io.*;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * @Author 李涛
  14. * @Description file文件转base64
  15. * @Date 19:13 2022/5/6
  16. * @Param
  17. * @return
  18. **/
  19. public class FileToBase64Utils {
  20. /**
  21. * 根据文件url获取文件并转换为base64编码
  22. *
  23. * @param srcUrl 文件地址
  24. * @param requestMethod 请求方式("GET","POST")
  25. * @return 文件base64编码
  26. */
  27. public static String netSourceToBase64(String srcUrl,String requestMethod) {
  28. ByteArrayOutputStream outPut = new ByteArrayOutputStream();
  29. byte[] data = new byte[1024 * 8];
  30. try {
  31. // 创建URL
  32. URL url = new URL(srcUrl);
  33. // 创建链接
  34. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  35. conn.setRequestMethod(requestMethod);
  36. conn.setConnectTimeout(10 * 1000);
  37. if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
  38. //连接失败/链接失效/文件不存在
  39. return null;
  40. }
  41. InputStream inStream = conn.getInputStream();
  42. int len = -1;
  43. while (-1 != (len = inStream.read(data))) {
  44. outPut.write(data, 0, len);
  45. }
  46. inStream.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. // 对字节数组Base64编码
  51. BASE64Encoder encoder = new BASE64Encoder();
  52. return encoder.encode(outPut.toByteArray());
  53. }
  54. /**
  55. * 演示读取上传的文件内容,转为Base64
  56. * @param fileUrl 已上传到临时目录的文件URL
  57. * @return //http://127.0.0.1:8081/ierp/tempfile/download.do?configKey=redis.serversForCache&id=tempfile-7be913cc-6dd9-4b9e-9ddd-6a35962ad8be
  58. */
  59. public static String loadFileString(String fileUrl) {
  60. TempFileCacheDownloadable downLoad = (TempFileCacheDownloadable) CacheFactory.getCommonCacheFactory().getTempFileCache();
  61. int temp = 0;
  62. int len = 0;
  63. byte[] bt = null;
  64. InputStream inStream = null;
  65. String base64 = "";
  66. try {
  67. String[] queryParams = new URL(fileUrl).getQuery().split("&");
  68. Map<String, String> downloadFileParams = new HashMap<>();
  69. for (String queryParam : queryParams) {
  70. String[] p = queryParam.split("=");
  71. downloadFileParams.put(p[0], p[1]);
  72. }
  73. TempFileCacheDownloadable.Content content = downLoad.get(downloadFileParams.get("configKey"), downloadFileParams.get("id"));
  74. inStream = content.getInputStream();
  75. bt = new byte[content.getLength()];
  76. while ((temp = inStream.read()) != -1) {
  77. bt[len] = (byte) temp;
  78. len++;
  79. }
  80. BASE64Encoder encoder = new BASE64Encoder();
  81. base64 = encoder.encode(bt);
  82. } catch (Exception e) {
  83. throw new KDBizException(e.getMessage());
  84. } finally {
  85. try {
  86. if (inStream != null) {
  87. inStream.close();
  88. }
  89. } catch (IOException e) {
  90. throw new KDBizException(e.getMessage());
  91. }
  92. }
  93. return base64;
  94. }
  95. public static File getFileFromBase64(String base) {
  96. String base64Pic = base;
  97. File file = null;
  98. Map<String, Object> resultMap = new HashMap<String, Object>();
  99. if (base64Pic == null) { // 图像数据为空
  100. resultMap.put("resultCode", 0);
  101. resultMap.put("msg", "图片为空");
  102. } else {
  103. BASE64Decoder decoder = new BASE64Decoder();
  104. String baseValue = base64Pic.replaceAll(" ", "+");//前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。
  105. byte[] b = new byte[0];//去除base64中无用的部分
  106. try {
  107. b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. File file1 = new File("E:\\");
  112. if (!file1.exists() && !file1.isDirectory()) {//判断文件路径下的文件夹是否存在,不存在则创建
  113. file1.mkdirs();
  114. }
  115. try {
  116. for (int i = 0; i < b.length; ++i) {
  117. if (b[i] < 0) {// 调整异常数据
  118. b[i] += 256;
  119. }
  120. }
  121. file = new File("E:" + "\\" + System.currentTimeMillis());
  122. // 如果要返回file文件这边return就可以了,存到临时文件中
  123. OutputStream out = new FileOutputStream(file.getPath());
  124. out.write(b);
  125. out.flush();
  126. out.close();
  127. } catch (Exception e) {
  128. resultMap.put("resultCode", 0);
  129. resultMap.put("msg", "存储异常");
  130. }
  131. }
  132. return file;
  133. }
  134. }