金蝶EAS,附件管理,附件上传,单据对应附件相关代码

本文介绍金蝶EAS系统中附件的上传、查询、删除等操作方法,涵盖前后端实现细节,包括如何通过API与业务对象关联,以及如何处理字节流。

金蝶EAS,附件上传/查询/删除等操作。

import java.io.File;
import java.io.FileInputStream;
import com.kingdee.bos.Context;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.base.attachment.IAttachment;
import com.kingdee.eas.base.attachment.IBoAttchAsso;
import com.kingdee.eas.base.attachment.AttachmentInfo;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.base.attachment.BoAttchAssoInfo;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.base.attachment.AttachmentFactory;
import com.kingdee.eas.base.attachment.BoAttchAssoFactory;
import com.kingdee.eas.base.attachment.AttachmentCollection;
import com.kingdee.eas.base.attachment.BoAttchAssoCollection;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.base.attachment.common.SimpleAttachmentInfo;
import com.kingdee.eas.base.attachment.common.AttachmentClientManager;
import com.kingdee.eas.base.attachment.common.AttachmentServerManager;
import com.kingdee.eas.base.attachment.common.AttachmentManagerFactory;

/**
 * 文件处理工具类,附件相关处理
 * 部分方法用于前端/后台,使用时请参阅方法注释
 * @author 郭旭
 *
 */
public class FileUtil {
	
	/**
	 * 删除单据对应的所有附件,操作成功时返回ture,用于前端
	 * @param billId 单据主键
	 * 
	 */
	public static boolean delete(String billId) throws BOSException, EASBizException{
		
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return false;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
	    	IObjectPK[] pks = new ObjectUuidPK[coll.size()];
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		pks[i] = new ObjectUuidPK(attachment.getId());
	    		
	    	}
	    	iAttachment.delete(pks); //删除附件
	    	iBoAttchAsso.delete(filter); //删除附件关联
    	
		return true;
		
	}
	
	/**
	 * 删除单据对应的所有附件,操作成功时返回ture,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * 
	 */
	public static boolean delete(Context ctx, String billId) throws BOSException, EASBizException{
		
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return false;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
	    	IObjectPK[] pks = new ObjectUuidPK[coll.size()];
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		pks[i] = new ObjectUuidPK(attachment.getId());
	    		
	    	}
	    	iAttachment.delete(pks); //删除附件
	    	iBoAttchAsso.delete(filter); //删除附件关联
    	
		return true;
		
	}
	
	/**
	 * 附件上传,金蝶EAS标准产品功能(用于前端)
	 * @param billId 单据主键
	 * @param filePath 附件路径(不含文件名)
	 * @param fileName 附件名称(含后缀)
	 * @return 附件ID
	 * 
	 */
	public static String upload(String billId, String filePath, String fileName){
		
		String attachId = null;
		if(!filePath.endsWith("/")) filePath += "/";
		AttachmentClientManager manager = AttachmentManagerFactory.getClientManager();
		try {
			byte[] bytes = getBytes(filePath + fileName); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName.substring(0, fileName.indexOf("."))); //文件名
			simple.setExtName(fileName.substring(fileName.indexOf(".") + 1, fileName.length())); //后缀
			attachId = manager.addNewAttachment(billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return attachId;
		
	}
	
	/**
	 * 附件上传,金蝶EAS标准产品功能(用于后台)
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @param filePath 附件路径(不含文件名)
	 * @param fileName 附件名称(含后缀)
	 * @return 附件ID
	 * 
	 */
	public static String upload(Context ctx, String billId, String filePath, String fileName){
		
		String attachId = null;
		if(!filePath.endsWith("/")) filePath += "/";
		AttachmentServerManager manager = AttachmentManagerFactory.getServerManager(ctx);
		try {
			byte[] bytes = getBytes(filePath + fileName); //获取字节流
			SimpleAttachmentInfo simple = new SimpleAttachmentInfo();
			simple.setContent(bytes);
			simple.setMainName(fileName.substring(0, fileName.indexOf("."))); //文件名
			simple.setExtName(fileName.substring(fileName.indexOf(".") + 1, fileName.length())); //后缀
			attachId = manager.addNewAttachment(ctx, billId, simple);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return attachId;
		
	}
	
	/**
	 * 根据文件路径读取字节流
	 * @param filePath 文件路径
	 * @return byte[]
	 * 
	 */
	public static byte[] getBytes(String filePath) throws Exception {
		
	        File file = new File(filePath);
	        long fileSize = file.length();
	        if (fileSize > Integer.MAX_VALUE) {
	            return null;
	        }
	        FileInputStream in = new FileInputStream(file);
	        byte[] bytes = new byte[(int) fileSize];
	        int offset = 0;
	        int read = 0;
	        while (offset < bytes.length && (read = in.read(bytes, offset, bytes.length - offset)) >= 0) {
	            offset += read;
	        }
	        //确保所有数据均被读取
	        if (offset != bytes.length) {
	        	throw new Exception("Could not completely read file " + file.getName());
	        }
	        in.close();
	        
	        return bytes;
        
    }
	
	/**
	 * 获取单据对应的单一附件,用于前端
	 * @param billId 单据主键
	 * @return 附件对象
	 * 
	 */
	public static AttachmentInfo getAttach(String billId) throws BOSException, EASBizException{
		
		AttachmentInfo attachment = null;
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return attachment;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
		BoAttchAssoInfo bo = coll.get(0); //附件关联对象
    		attachment = bo.getAttachment(); //附件对象
		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()));
    	
		return attachment;
		
	}
	
	/**
	 * 获取单据对应的单一附件,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @return 附件对象
	 * 
	 */
	public static AttachmentInfo getAttach(Context ctx, String billId) throws BOSException, EASBizException{
		
		AttachmentInfo attachment = null;
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return attachment;
	    	}
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
		BoAttchAssoInfo bo = coll.get(0); //附件关联对象
		
	    	attachment = bo.getAttachment(); //附件对象
	    	SelectorItemCollection selectors = new SelectorItemCollection();
	    	selectors.add(new SelectorItemInfo("id"));
	    	selectors.add(new SelectorItemInfo("number"));
	    	selectors.add(new SelectorItemInfo("name"));
	    	selectors.add(new SelectorItemInfo("file"));
		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()), selectors);
    	
		return attachment;
		
	}
	
	/**
	 * 获取附件的文件名,包含名称和后缀
	 * @param attach 附件对象
	 * @return 文件名
	 * 
	 */
	public static String getAttach(AttachmentInfo attach){
		
		String fileName = null;
		fileName = attach.getName() + "." + attach.getSimpleName();
		
		return fileName;
		
	}
	
	/**
	 * 获取单据对应的所有附件,用于前台
	 * @param billId 单据主键
	 * @return 附件对象集合
	 * 
	 */
	public static AttachmentCollection getAttaches(String billId) throws BOSException, EASBizException{
		
		AttachmentCollection attaches = null; //附件集合
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getRemoteInstance(); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return attaches;
	    	}
	    	attaches = new AttachmentCollection();
	    	IAttachment iAttachment = AttachmentFactory.getRemoteInstance();
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()));
	    		attaches.add(attachment);
	    		
	    	}
    	
		return attaches;
		
	}
	
	/**
	 * 获取单据对应的所有附件,用于后台
	 * @param ctx 上下文
	 * @param billId 单据主键
	 * @return 附件对象集合
	 * 
	 */
	public static AttachmentCollection getAttaches(Context ctx, String billId) throws BOSException, EASBizException{
		
		AttachmentCollection attaches = null; //附件集合
		IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); //附件与业务对象关联接口
	    	EntityViewInfo view = new EntityViewInfo();
	    	FilterInfo filter = new FilterInfo();
	    	filter.getFilterItems().add(new FilterItemInfo("boID", billId));
	    	view.setFilter(filter);
	    	BoAttchAssoCollection coll = iBoAttchAsso.getBoAttchAssoCollection(view); //查询所关联附件
	    	if(Verify.isNull(coll)){
	    		return attaches;
	    	}
	    	attaches = new AttachmentCollection();
	    	IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
	    	for(int i = 0; i < coll.size(); i++){
	    		
	    		BoAttchAssoInfo bo = coll.get(i); //附件关联对象
	        	AttachmentInfo attachment = bo.getAttachment(); //附件对象
	    		attachment = iAttachment.getAttachmentInfo(new ObjectUuidPK(attachment.getId()));
	    		attaches.add(attachment);
	    		
	    	}
    	
		return attaches;
		
	}
	
}

 

### 金蝶EAS系统中处理费用报销打印附件的方法 在金蝶EAS系统的开发环境中,如果要自定义或调整费用报销单据的打印功能及其附带的数据源,则应当考虑对`customPrintDataEntities`方法进行修改[^2]。此方法允许开发者根据业务需求定制化打印内容,确保最终输出满足特定的要求。 对于具体实现步骤而言,在该函数内部可以加入逻辑来指定哪些字段以及它们对应的值会被传递给打印机;同时也可以在此处设置额外参数控制页面布局或是样式表应用情况等细节方面的工作。此外,为了使这些改动生效并能被正确识别出来,还需保证所作的一切变动均遵循官方API文档中的指导原则与最佳实践建议。 当涉及到实际操作层面时,假设现在有一个场景是要向现有的标准报表模板里新增加一栏用于展示员工提交申请时上传相关证明材料链接列表: ```javascript // 假设这是位于某个扩展类下的 customPrintDataEntities 函数片段 function customPrintDataEntities() { var attachments = []; // 初始化一个数组用来存储文件路径 // 遍历获取到的所有关联记录对象(这里简化表示) for (var i = 0; i < expenseReport.attachments.length; ++i) { let attachmentUrl = getAttachmentDownloadLink(expenseReport.attachments[i].id); attachments.push(attachmentUrl); } // 将收集好的 URL 列表作为新属性附加至待渲染实体集合当中去 this.printingContext.customProperties["Attachments"] = attachments.join(", "); } ``` 上述代码展示了怎样通过遍历开销报告内的每一个已挂载资源项,并调用辅助工具函数 `getAttachmentDownloadLink()` 来取得其下载地址的方式构建起一份可供查阅的实际连接串列。最后一步则是把这些信息注入到了即将参与排版过程的对象实例之中以便后续呈现于纸面之上。 值得注意的是,以上仅提供了一种可能的技术方案框架供参考学习之用,针对不同版本号之间存在的差异性以及其他潜在影响因素,请务必参照产品手册说明仔细核验后再行尝试部署实施。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值