1.分布式锁
相关:https://dev.kingdee.com/index/docsNew/d1b4f435-a5f8-4441-83ac-5059bea51815
import kd.bos.dlock.DLock;DLock lock = DLock.create("fi/gl/N20180503-8808", "凭证N20180503-8808");// 一直等待直到获得锁lock.lock();try {// 获取锁信息:// {key: fi/gl/N20180503-8808, desc: 凭证N20180503-8808, owner: 163388da76b0046, waitingLocks: 0, createTime: 2018-05-08 11:35:50.087}System.out.println(DLock.getLockInfo("fi/gl/N20180503-8808"));// 获取所有锁信息// System.out.println(DLock.getAllLockInfo());// ...} finally {//一般放入finallylock.unlock();}
2.DynamicObject 转成 Map对象
Map<String, Object> objectMap = new HashMap();if (dynamicObject != null) {DataEntityPropertyCollection dynamicObjects = dynamicObject.getDataEntityType().getProperties();Iterator var3 = dynamicObjects.iterator();while(var3.hasNext()) {IDataEntityProperty iDataEntityProperty = (IDataEntityProperty)var3.next();String property = iDataEntityProperty.getName().toLowerCase();objectMap.put(property, dynamicObject.get(property));}}return objectMap;
3.查询单据所有字段信息(需要查询两次)
BusinessDataServiceHelper.load(id[], BusinessDataServiceHelper.newDynamicObject(单据标识).getDynamicObjectType());
4.file转base64
package wens.fi.jyt.commmon.utils;import kd.bos.cache.CacheFactory;import kd.bos.cache.tempfile.TempFileCacheDownloadable;import kd.bos.exception.KDBizException;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import java.io.*;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashMap;import java.util.Map;/*** @Author 李涛* @Description file文件转base64* @Date 19:13 2022/5/6* @Param* @return**/public class FileToBase64Utils {/*** 根据文件url获取文件并转换为base64编码** @param srcUrl 文件地址* @param requestMethod 请求方式("GET","POST")* @return 文件base64编码*/public static String netSourceToBase64(String srcUrl,String requestMethod) {ByteArrayOutputStream outPut = new ByteArrayOutputStream();byte[] data = new byte[1024 * 8];try {// 创建URLURL url = new URL(srcUrl);// 创建链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod(requestMethod);conn.setConnectTimeout(10 * 1000);if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {//连接失败/链接失效/文件不存在return null;}InputStream inStream = conn.getInputStream();int len = -1;while (-1 != (len = inStream.read(data))) {outPut.write(data, 0, len);}inStream.close();} catch (IOException e) {e.printStackTrace();}// 对字节数组Base64编码BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(outPut.toByteArray());}/*** 演示读取上传的文件内容,转为Base64* @param fileUrl 已上传到临时目录的文件URL* @return //http://127.0.0.1:8081/ierp/tempfile/download.do?configKey=redis.serversForCache&id=tempfile-7be913cc-6dd9-4b9e-9ddd-6a35962ad8be*/public static String loadFileString(String fileUrl) {TempFileCacheDownloadable downLoad = (TempFileCacheDownloadable) CacheFactory.getCommonCacheFactory().getTempFileCache();int temp = 0;int len = 0;byte[] bt = null;InputStream inStream = null;String base64 = "";try {String[] queryParams = new URL(fileUrl).getQuery().split("&");Map<String, String> downloadFileParams = new HashMap<>();for (String queryParam : queryParams) {String[] p = queryParam.split("=");downloadFileParams.put(p[0], p[1]);}TempFileCacheDownloadable.Content content = downLoad.get(downloadFileParams.get("configKey"), downloadFileParams.get("id"));inStream = content.getInputStream();bt = new byte[content.getLength()];while ((temp = inStream.read()) != -1) {bt[len] = (byte) temp;len++;}BASE64Encoder encoder = new BASE64Encoder();base64 = encoder.encode(bt);} catch (Exception e) {throw new KDBizException(e.getMessage());} finally {try {if (inStream != null) {inStream.close();}} catch (IOException e) {throw new KDBizException(e.getMessage());}}return base64;}public static File getFileFromBase64(String base) {String base64Pic = base;File file = null;Map<String, Object> resultMap = new HashMap<String, Object>();if (base64Pic == null) { // 图像数据为空resultMap.put("resultCode", 0);resultMap.put("msg", "图片为空");} else {BASE64Decoder decoder = new BASE64Decoder();String baseValue = base64Pic.replaceAll(" ", "+");//前台在用Ajax传base64值的时候会把base64中的+换成空格,所以需要替换回来。byte[] b = new byte[0];//去除base64中无用的部分try {b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));} catch (IOException e) {e.printStackTrace();}File file1 = new File("E:\\");if (!file1.exists() && !file1.isDirectory()) {//判断文件路径下的文件夹是否存在,不存在则创建file1.mkdirs();}try {for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {// 调整异常数据b[i] += 256;}}file = new File("E:" + "\\" + System.currentTimeMillis());// 如果要返回file文件这边return就可以了,存到临时文件中OutputStream out = new FileOutputStream(file.getPath());out.write(b);out.flush();out.close();} catch (Exception e) {resultMap.put("resultCode", 0);resultMap.put("msg", "存储异常");}}return file;}}
