package com.yem.wm.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import kd.bos.context.RequestContext; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.utils.StringUtils; import kd.bos.fileservice.FileItem; import kd.bos.fileservice.FileService; import kd.bos.fileservice.FileServiceFactory; import kd.bos.orm.ORM; import kd.bos.servicehelper.AttachmentServiceHelper; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.user.UserServiceHelper; import kd.bos.url.UrlService; import kd.bos.web.actions.utils.FilePathUtil; /** * @author longh zhouc * @date 2023/12/29 18:04 * 2021年8月20日 下午4:33:59 * @className AttachmentUtil * @description 附件面板工具类 */ public class AttachmentUtil { public static DynamicObject uploadAttachmentPanel(String formId, Object pkId, String attachmentKey, String fileName, String url) { int size = 0; try { size = FileServiceFactory.getAttachmentFileService().getInputStream(url).available(); } catch (IOException e) { e.printStackTrace(); } if(YEM.isNotEmpty(url)) { if(YEM.isEmpty(fileName)) { fileName = url.substring(url.lastIndexOf("/") + 1); } Date today = new Date(); DynamicObject newAtt = BusinessDataServiceHelper.newDynamicObject("bos_attachment"); Long id = ORM.create().genLongId("bos_attachment"); newAtt.set("id", id); newAtt.set("fnumber", "upload_" + id); newAtt.set("FBillType", formId); newAtt.set("FInterID", pkId); newAtt.set("FModifyTime", today); newAtt.set("fcreatetime", today); newAtt.set("FaliasFileName", fileName); newAtt.set("FAttachmentName", fileName); newAtt.set("FFileId", url); newAtt.set("FExtName", fileName.substring(fileName.lastIndexOf(46) + 1)); // newAtt.set("FATTACHMENTSIZE", size); newAtt.set("FCREATEMEN", RequestContext.get().getCurrUserId()); newAtt.set("fattachmentpanel", attachmentKey); newAtt.set("fdescription", ""); SaveServiceHelper.save(new DynamicObject[] {newAtt}); return newAtt; } return null; } /** * 上传附件至附件面板 * * @param appId 应用标识 * @param formId 单据标识 * @param pkId 单据内码 * @param attachmentKey 附件面板标识 * @param fileName 文件名 * @return * @throws IOException */ public static DynamicObject uploadAttachment(String appId, String formId, String pkId, String attachmentKey, String fileName, InputStream fileInputStream){ DynamicObject newAtt = null; Date today = new Date(); DateFormat df = new SimpleDateFormat("yyyyMM"); String NewDateStr = df.format(today); String attachmentPath = "/" + RequestContext.get().getTenantId() + "/" + RequestContext.get().getAccountId() + "/" + NewDateStr + "/" + appId + "/" + formId + "/" + pkId + "/attachments/"; newAtt = BusinessDataServiceHelper.newDynamicObject("bos_attachment"); // File file = new File(filePath); // if (file.isFile()) { @SuppressWarnings("resource") // int size = new FileInputStream(file).available(); FileService fs = FileServiceFactory.getAttachmentFileService(); String path = attachmentPath + fileName; FileItem fi = new FileItem(fileName, path, fileInputStream); fi.setCreateNewFileWhenExists(true); String url = fs.upload(fi); Long id = ORM.create().genLongId("bos_attachment"); if (url != null && !"".equals(url)) { newAtt.set("id", id); newAtt.set("fnumber", id + "upload_" + formId); newAtt.set("FBillType", formId); newAtt.set("FInterID", pkId); newAtt.set("FModifyTime", today); newAtt.set("fcreatetime", today); newAtt.set("FaliasFileName", fileName); newAtt.set("FAttachmentName", fileName); newAtt.set("FFileId", url); newAtt.set("FExtName", fileName != null ? fileName.substring(fileName.lastIndexOf(46) + 1) : ""); newAtt.set("FATTACHMENTSIZE", 15132); newAtt.set("FCREATEMEN", RequestContext.get().getCurrUserId()); newAtt.set("fattachmentpanel", attachmentKey); newAtt.set("fdescription", ""); SaveServiceHelper.save(new DynamicObject[]{newAtt}); } else { new Exception("附件【" + fileName + "】上传失败!"); } return newAtt; } }