AttachmentUtils.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.kingdee.eas.custom.recuritment.utils;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.ctrl.swing.StringUtils;
  5. import com.kingdee.bos.dao.IObjectPK;
  6. import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
  7. import com.kingdee.bos.metadata.entity.EntityViewInfo;
  8. import com.kingdee.bos.metadata.entity.FilterInfo;
  9. import com.kingdee.bos.metadata.entity.FilterItemCollection;
  10. import com.kingdee.bos.metadata.entity.FilterItemInfo;
  11. import com.kingdee.bos.metadata.query.util.CompareType;
  12. import com.kingdee.bos.util.BOSUuid;
  13. import com.kingdee.eas.base.attachment.*;
  14. import com.kingdee.eas.common.EASBizException;
  15. import com.kingdee.shr.attachment.*;
  16. import com.kingdee.shr.attachment.AttachmentTypeEnum;
  17. import com.kingdee.shr.recuritment.ResumeBaseRecInfo;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.HttpURLConnection;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. /**
  28. * @author qingwu
  29. * @date 2025/7/3
  30. * @apiNote
  31. */
  32. public class AttachmentUtils {
  33. /**
  34. * 删除附件信息
  35. *
  36. * @param ctx
  37. * @param sql 条件
  38. * @throws BOSException
  39. * @throws EASBizException
  40. */
  41. public static void deleteAttachment(Context ctx, String sql) throws BOSException, EASBizException {
  42. //查询附件扩展表
  43. ISHRAttachmentExt ishrAttachmentExt = SHRAttachmentExtFactory.getLocalInstance(ctx);
  44. SHRAttachmentExtCollection shrAttachmentExtCollection = ishrAttachmentExt.getSHRAttachmentExtCollection(sql);
  45. Set delAtt = new HashSet();
  46. for (int i = 0; i < shrAttachmentExtCollection.size(); i++) {
  47. SHRAttachmentExtInfo shrAttachmentExtInfo = shrAttachmentExtCollection.get(i);
  48. AttachmentInfo attachment = shrAttachmentExtInfo.getAttachment();
  49. ishrAttachmentExt.delete(new ObjectUuidPK(shrAttachmentExtInfo.getId()));
  50. delAtt.add(attachment.getId());
  51. }
  52. if (delAtt.size() > 0) {
  53. //删除附件主表数据
  54. FilterInfo delAttFilter = new FilterInfo();
  55. delAttFilter.getFilterItems().add(new FilterItemInfo("id", delAtt, CompareType.INCLUDE));
  56. IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
  57. iAttachment.delete(delAttFilter);
  58. //删除附件扩展表和关联关系表数据
  59. FilterInfo dleExFilter = new FilterInfo();
  60. dleExFilter.getFilterItems().add(new FilterItemInfo("attachment.id", delAtt, CompareType.INCLUDE));
  61. BoAttchAssoFactory.getLocalInstance(ctx).delete(dleExFilter);
  62. }
  63. }
  64. /***
  65. * 保存附件
  66. * @param filename
  67. * @param data
  68. * @throws BOSException
  69. * @throws EASBizException
  70. */
  71. public static void insertAttachment(Context ctx, String filename, ResumeBaseRecInfo resumeInfo, byte[] data, String oldfileName) {
  72. try {
  73. //if (data.length > 20971520) {
  74. // throw new BOSException("保存附件失败,附件大小超过20MB!");
  75. //}
  76. //if (StringUtils.isEmpty(filename) || null == resumeInfo.getId() || data.length <= 0) {
  77. // throw new BOSException("保存附件失败,参数格式不正确!");
  78. //}
  79. String boId = resumeInfo.getId().toString();
  80. // String boId = "njRU7qn9RyKlGA2FG+hdCMXc5i4=";
  81. SHRAttachmentExtInfo attchExt = new SHRAttachmentExtInfo();
  82. AttachmentInfo ai = new AttachmentInfo();
  83. ai.setName(filename.substring(0, filename.lastIndexOf('.')));
  84. ai.setSimpleName(filename.substring(filename.lastIndexOf(".") + 1));
  85. ai.setDescription("");
  86. ai.setFile(data);
  87. ai.setIsShared(false);
  88. ai.setSharedDesc("否");
  89. ai.setAttachID("" + System.currentTimeMillis());
  90. ai.setType(getFileType(filename));
  91. AttachmentFactory.getLocalInstance(ctx).addnew(ai);
  92. attchExt.setAttachment(ai);
  93. attchExt.setName(filename);
  94. attchExt.setPropertyName("null0");
  95. attchExt.setType(AttachmentTypeEnum.FORM);
  96. // attchExt.setBunding(userId + "#" + uipk);
  97. attchExt.setBoID(boId);
  98. attchExt.setState(AttachmentState.SAVE);
  99. attchExt.setSimpleName(oldfileName);
  100. SHRAttachmentExtFactory.getLocalInstance(ctx).addnew(attchExt);
  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. BoAttchAssoFactory.getLocalInstance(ctx).addnew(boAttchAssoInfo);
  107. } catch (EASBizException e) {
  108. e.printStackTrace();
  109. } catch (BOSException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. private static String getFileType(String fullname) {
  114. String extname = fullname.substring(fullname.lastIndexOf(".") + 1, fullname.length());
  115. if (!"doc".equalsIgnoreCase(extname) && !"docx".equalsIgnoreCase(extname)) {
  116. if (!"xls".equalsIgnoreCase(extname) && !"xlsx".equalsIgnoreCase(extname) && !"xlsm".equalsIgnoreCase(extname) && !"xlsb".equalsIgnoreCase(extname)) {
  117. if (!"ppt".equalsIgnoreCase(extname) && !"pptx".equalsIgnoreCase(extname) && !"pptm".equalsIgnoreCase(extname)) {
  118. return "txt".equalsIgnoreCase(extname) ? "TEXT 文本文件" : "未知文件类型(." + extname + ")";
  119. } else {
  120. return "Microsoft PowerPoint 幻灯片";
  121. }
  122. } else {
  123. return "Microsoft Excel 表格";
  124. }
  125. } else {
  126. return "Microsoft Word 文档";
  127. }
  128. }
  129. /***
  130. * 根据文件下载地址读取字节流
  131. * @param urlStr
  132. * @return
  133. * @throws IOException
  134. */
  135. public static byte[] getBytesByNetURL(String urlStr) throws IOException {
  136. // RestTemplate restTemplate = new RestTemplate();
  137. // ResponseEntity<byte[]> responseEntity = restTemplate.exchange(urlStr, HttpMethod.GET, null, byte[].class);
  138. // byte[] fileContent = responseEntity.getBody();
  139. URL url = new URL(urlStr);
  140. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  141. // //设置超时时间
  142. conn.setConnectTimeout(5 * 1000);
  143. // //通过输入流获取图片数据
  144. InputStream in = conn.getInputStream();
  145. // //得到图片的二进制数据
  146. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  147. byte[] buffer = new byte[1024];
  148. int len;
  149. while ((len = in.read(buffer)) != -1) {
  150. outputStream.write(buffer, 0, len);
  151. }
  152. in.close();
  153. return outputStream.toByteArray();
  154. // return fileContent;
  155. }
  156. /**
  157. * 从路径中提取文件名
  158. */
  159. public static String extractFileNameFromPath(String path) {
  160. return path.substring(path.lastIndexOf('/') + 1);
  161. }
  162. /**
  163. * 提取文件扩展名
  164. */
  165. public static String extractFileExtension(String fileName) {
  166. return fileName.substring(fileName.lastIndexOf('.') + 1);
  167. }
  168. }