sHR附件上傳到阿里OSS實現的一種方式
阿新 • • 發佈:2020-10-29
附件上傳主要涉及資料庫表:
T_BAS_Attachment:附件表
T_BAS_BoAttchAsso:附件和業務單據關係表
T_HR_SHRAttachmentExt:sHR附件表
附件預設是儲存在資料庫的,以位元組流的方式存放在T_BAS_Attachment表的FFile。
sHR標準附件上傳邏輯:點選附件上傳控制元件上傳附件後,會同時在T_BAS_Attachment和T_HR_SHRAttachmentExt寫入資料,T_HR_SHRAttachmentExt表此時的狀態FState為20。點選儲存單據時,會更新T_HR_SHRAttachmentExt的狀態為10,並且往T_BAS_BoAttchAsso寫入資料
如果需要將附件上傳阿里OSS,並且不儲存在資料庫,可修改com.kingdee.shr.attachment.app.SHRAttachmentExtControllerBean的_updateAffterSaveForm方法。在儲存單據的時候將T_BAS_Attachment表FFile的位元組流取到,上傳到阿里OSS,然後將T_BAS_Attachment表FFile清空,將阿里OSS檔案路徑存在T_BAS_Attachment的FRemotePath。下載時可以根據這個路徑去阿里OSS下載。
附件上傳:
1 package com.kingdee.shr.attachment.app;2 3 import com.kingdee.bos.BOSException; 4 import com.kingdee.bos.Context; 5 import com.kingdee.bos.dao.ormapping.ObjectUuidPK; 6 import com.kingdee.bos.metadata.entity.EntityViewInfo; 7 import com.kingdee.bos.metadata.entity.FilterInfo; 8 import com.kingdee.bos.metadata.entity.FilterItemInfo;9 import com.kingdee.bos.util.BOSUuid; 10 import com.kingdee.eas.base.attachment.AttachmentFactory; 11 import com.kingdee.eas.base.attachment.AttachmentInfo; 12 import com.kingdee.eas.base.attachment.BoAttchAssoFactory; 13 import com.kingdee.eas.base.attachment.BoAttchAssoInfo; 14 import com.kingdee.eas.base.attachment.IAttachment; 15 import com.kingdee.eas.base.attachment.IBoAttchAsso; 16 import com.kingdee.eas.base.attachment.common.OSSClientUtil; 17 import com.kingdee.eas.common.EASBizException; 18 import com.kingdee.shr.attachment.AttachmentState; 19 import com.kingdee.shr.attachment.SHRAttachmentExtCollection; 20 import com.kingdee.shr.attachment.SHRAttachmentExtInfo; 21 22 import java.util.Map; 23 import org.apache.log4j.Logger; 24 public class SHRAttachmentExtControllerBean extends AbstractSHRAttachmentExtControllerBean 25 { 26 /** 27 * 28 */ 29 private static final long serialVersionUID = -182628981203112646L; 30 private static Logger logger = Logger.getLogger("com.kingdee.shr.attachment.app.SHRAttachmentExtControllerBean"); 31 32 protected Map _getAttchIDByFileName(Context ctx, String boID, String fileName) throws BOSException, EASBizException { 33 return null; 34 } 35 36 protected void _updateAffterSaveForm(Context ctx, String billId, String ids) throws BOSException, EASBizException { 37 FilterInfo filterInfo = new FilterInfo(); 38 39 filterInfo.getFilterItems().add(new FilterItemInfo("bunding", ids)); 40 filterInfo.getFilterItems().add(new FilterItemInfo("state", Integer.valueOf(20))); 41 EntityViewInfo entityViewInfo = new EntityViewInfo(); 42 entityViewInfo.setFilter(filterInfo); 43 44 IBoAttchAsso boAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx); 45 IAttachment attfactory = AttachmentFactory.getLocalInstance(ctx); 46 SHRAttachmentExtCollection collection = getSHRAttachmentExtCollection(ctx, entityViewInfo); 47 if (collection.size() > 0){ 48 //path:oss存放路徑,shr/單據bostype/單據id/ 49 StringBuffer path = new StringBuffer(); 50 BOSUuid uuid = BOSUuid.read(billId); 51 path.append("shr/"); 52 path.append(uuid.getType().toString()); 53 path.append("/"); 54 path.append(billId); 55 path.append("/"); 56 for (int i = 0; i < collection.size(); ++i) { 57 SHRAttachmentExtInfo extInfo = collection.get(i); 58 AttachmentInfo ai = extInfo.getAttachment(); 59 //獲取附件資料 60 ai = attfactory.getAttachmentInfo(new ObjectUuidPK(ai.getId())); 61 //附件內容 62 byte[] content = ai.getFile(); 63 //上傳oss 64 OSSClientUtil.uploadOSS(ctx,"OSSPARAM",path.toString(),extInfo.getName(), content); 65 //附件在oss的路徑:"shr/"+extInfo.getName()放到附件表的fRemotePath 66 ai.setRemotePath(path+extInfo.getName()); 67 //將附件表的附件內容清空 68 ai.setFile(null); 69 //修改附件資料 70 attfactory.save(ai); 71 72 BoAttchAssoInfo boAttchAssoInfo = new BoAttchAssoInfo(); 73 boAttchAssoInfo.setBoID(billId); 74 boAttchAssoInfo.setAssoBusObjType(BOSUuid.getBOSObjectType(billId, true) + ""); 75 boAttchAssoInfo.setAssoType("Added Accessories"); 76 boAttchAssoInfo.setAttachment(ai); 77 boAttchAsso.addnew(boAttchAssoInfo); 78 extInfo.setState(AttachmentState.SAVE); 79 extInfo.setBoID(billId); 80 save(ctx, extInfo); 81 } 82 } 83 } 84 }
OSSClientUtil是自己寫的阿里OSS幫助類,具體上傳OSS的程式碼可檢視https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11174283.6.940.3a007da2Tg3e3t.
附件下載:附件下載如果指定下載路徑的話,是會下載到伺服器上的,不會下載到客戶端電腦,如果直接讀取流,可能會出現亂碼
com.kingdee.shr.base.syssetting.web.controller.AttachmentUploadController
1 private String writeAttachementFile(HttpServletRequest request, HttpServletResponse response) 2 throws ShrWebBizException, BOSException, EASBizException 3 { 4 String attachId = request.getParameter("id"); 5 logger.error(new StringBuilder().append("++++++ attachment downloadAction : attachId = ").append(attachId).toString()); 6 if (StringUtils.isEmpty(attachId)) { 7 throw new ShrWebBizException("下載失敗,檔案id為空"); 8 } 9 SelectorItemCollection sic = new SelectorItemCollection(); 10 sic.add(new SelectorItemInfo("id")); 11 sic.add(new SelectorItemInfo("name")); 12 sic.add(new SelectorItemInfo("remotePath")); 13 sic.add(new SelectorItemInfo("simpleName")); 14 AttachmentInfo ai = AttachmentFactory.getRemoteInstance().getAttachmentInfo(new ObjectUuidPK(attachId),sic); 15 16 String serverPath = request.getSession().getServletContext().getRealPath(File.separator); 17 String fileDir = new StringBuilder().append("newAttachmentFoler").append(File.separator).append(StringFilter(attachId)).append(File.separator).toString(); 18 String realFileDir = new StringBuilder().append(serverPath).append(fileDir).toString(); 19 File file = new File(realFileDir); 20 if (!(file.exists())) { 21 file.mkdirs(); 22 } 23 String fileName = new StringBuilder().append(ai.getName()).append(".").append(ai.getSimpleName()).toString(); 24 String realFileName = new StringBuilder().append(realFileDir).append(fileName).toString(); 25 System.out.println("realFileName:"+realFileName); 26 System.out.println("fileName:"+fileName); 27 System.out.println("serverPath:"+serverPath); 28 System.out.println("fileDir:"+fileDir); 29 System.out.println("realFileDir:"+realFileDir); 30 //下載到服務端 31 Context ctx = SHRContext.getInstance().getContext(); 32 OSSClientUtil.downloadOSS(ctx,"OSSPARAM",ai.getRemotePath(),realFileName); 33 OutputStream out = null; 34 FileInputStream input = null; 35 try { 36 input = new FileInputStream(realFileName); 37 response.reset(); 38 setResponseHeader(request, response, fileName); 39 if (input != null) { 40 AttachmentDownloadServer downServer = new AttachmentDownloadServer(); 41 byte[] content = downServer.getContent(input); 42 out = response.getOutputStream(); 43 out.write(content); 44 } 45 //刪除下載到服務端的檔案 46 del(realFileDir); 47 48 }catch (IOException e) { 49 }finally{ 50 try{ 51 closeStream(out); 52 input.close(); 53 } catch (ShrWebBizException e) { 54 throw new ShrWebBizException("下載失敗", e); 55 } catch (IOException e) { 56 ExceptionUtil.newEasbizException(e); 57 } 58 } 59 return null; 60 }
這邊使用的方式是先下載到服務端,再去服務端讀取檔案,最後把服務端的這個檔案刪除。
另外,員工資訊列表介面有批量上傳附件。這個需要改動com.kingdee.eas.hr.emp.web.util.PersonAttachmentBatchUploadHelper
1 private static boolean insertPersonAttachment(EmpHRORelationHisCollection empHRORelationHisColl, String fileName, String typeName, byte[] byteArray, Map panelMap, ZipFile zfile) throws BOSException, EASBizException 2 { 3 IAttachment attachment = AttachmentFactory.getRemoteInstance(); 4 IBoAttchAsso attchAsso = BoAttchAssoFactory.getRemoteInstance(); 5 String boID = ""; 6 String onlyone = "true"; 7 String propertyName = "null0"; 8 String description = ""; 9 String uipk = (String)panelMap.get("uipk"); 10 String panelName = (String)panelMap.get("name"); 11 String userId = HRFilterUtils.getCurrentUserId(SHRContext.getInstance().getContext()); 12 13 ISHRAttachmentExt SHRAttchExt = SHRAttachmentExtFactory.getRemoteInstance(); 14 SHRAttachmentExtInfo attchExt = new SHRAttachmentExtInfo(); 15 String personStr = ""; 16 String fileRealName = ""; 17 String mainname = ""; 18 String extname = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()); 19 extname = extname.toLowerCase(); 20 if (fileName.indexOf("##") == -1) { 21 personStr = fileName.substring(0, fileName.lastIndexOf(".")); 22 fileRealName = panelName + "." + extname; 23 mainname = panelName; 24 } else { 25 personStr = fileName.substring(0, fileName.indexOf("##")); 26 fileRealName = fileName.substring(fileName.indexOf("##") + 2); 27 if (fileRealName.lastIndexOf(".") == 0) { 28 fileRealName = panelName + "." + extname; 29 } 30 mainname = fileRealName.substring(0, fileRealName.lastIndexOf(".")); 31 } 32 PersonInfo pInfo = getPersonInfo(personStr); 33 if (pInfo == null) { 34 throw new EmployeeBosBizException(EmployeeBosBizException.EMP_NOT_EXIST_OR_ATTCH_INCON_REQ, new Object[] { personStr }); 35 } 36 37 PersonInfo person = null; 38 int i = 0; for (int size = empHRORelationHisColl.size(); i < size; ++i) { 39 person = empHRORelationHisColl.get(i).getPerson(); 40 if (pInfo.getNumber().equals(person.getNumber())) { 41 break; 42 } 43 person = null; 44 } 45 if (person == null) { 46 throw new EmployeeBizException(EmployeeBizException.NOT_IN_ADMINI_EARE, new Object[] { fileName }); 47 } 48 CoreBaseInfo entityInfo = getCoreBaseInfo(panelMap, pInfo); 49 if (entityInfo == null) { 50 throw new EmployeeBosBizException(EmployeeBosBizException.EMP_PAGE_INFO_NOT_EXIST, new Object[] { personStr, panelName }); 51 } 52 boID = entityInfo.getId().toString(); 53 54 AttachmentInfo ai = new AttachmentInfo(); 55 ai.setName(mainname); 56 ai.setSimpleName(extname); 57 ai.setDescription(description); 58 // ai.setFile(byteArray); 59 ai.setIsShared(false); 60 ai.setSharedDesc(SHRWebResource.getString("com.kingdee.eas.hr.emp.EmployeeBizResource", "label177", SHRContext.getInstance().getContext())); 61 62 //附件上傳到阿里雲 63 //FIle 清空 64 StringBuffer path = new StringBuffer(); 65 String bostype = entityInfo.getId().getType().toString(); 66 path.append("shr/"); 67 path.append(bostype); 68 path.append("/"); 69 path.append(boID); 70 path.append("/"); 71 //上傳oss 72 OSSClientUtil.uploadOSS(null,"OSSPARAM",path.toString(),fileRealName, byteArray); 73 System.out.println(path.toString()); 74 System.out.println(fileRealName); 75 //附件在oss的路徑:"shr/"+extInfo.getName()放到附件表的fRemotePath 76 ai.setRemotePath(path+fileRealName); 77 78 79 80 81 ai.setAttachID("" + System.currentTimeMillis()); 82 ai.setType(getFileType(fileRealName)); 83 84 attchExt.setAttachment(ai); 85 attchExt.setName(fileRealName); 86 attchExt.setPropertyName(propertyName); 87 if (propertyName.startsWith("null")) 88 attchExt.setType(AttachmentTypeEnum.FORM); 89 else { 90 attchExt.setType(AttachmentTypeEnum.PROPERTY); 91 } 92 attchExt.setState(AttachmentState.UNSAVE); 93 attchExt.setBunding(userId + '#' + uipk); 94 attchExt.setBoID(boID); 95 attchExt.setState(AttachmentState.SAVE); 96 97 attachment.addnew(ai); 98 99 if ((Boolean.valueOf(onlyone).booleanValue()) && (!(StringUtils.isEmpty(boID)))) { 100 attchExt.setState(AttachmentState.SAVE); 101 BoAttchAssoInfo boAttchAssoInfo = new BoAttchAssoInfo(); 102 boAttchAssoInfo.setBoID(boID); 103 boAttchAssoInfo.setAssoBusObjType(String.valueOf(BOSUuid.getBOSObjectType(boID, true))); 104 boAttchAssoInfo.setAssoType("Added Accessories"); 105 boAttchAssoInfo.setAttachment(ai); 106 attchAsso.addnew(boAttchAssoInfo); 107 } 108 109 SHRAttchExt.addnew(attchExt); 110 return true; 111 }