SaveFileForDisk.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.kingdee.eas.custom.sendmessage.utils;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
  5. import com.kingdee.eas.base.attachment.AttachmentFactory;
  6. import com.kingdee.eas.base.attachment.AttachmentInfo;
  7. import com.kingdee.eas.base.attachment.AttachmentManagerFacadeFactory;
  8. import com.kingdee.eas.base.attachment.IAttachmentManagerFacade;
  9. import com.kingdee.eas.common.EASBizException;
  10. import com.kingdee.eas.rpts.ctrlreport.osf.OSFExecutor;
  11. import com.kingdee.shr.certificationservice.HRCertificationBillInfo;
  12. import com.kingdee.util.StringUtils;
  13. import javax.servlet.ServletOutputStream;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.UUID;
  21. /**
  22. * 获取附件并保存到磁盘
  23. */
  24. public class SaveFileForDisk {
  25. private static String HOME_URL = "https://shr-test.gooeto.com:6888/";
  26. /**
  27. * 获取pdf对应的信息
  28. * elecProofInfo 为证明单据
  29. * @param ctx
  30. * @param elecProofInfo
  31. * @return
  32. * @throws BOSException
  33. * @throws EASBizException
  34. * @throws IOException
  35. */
  36. public static Map<String,String> getPDFInfo(Context ctx, HRCertificationBillInfo elecProofInfo ) throws BOSException, EASBizException, IOException {
  37. Map<String,String> resultMap = new HashMap();
  38. //获取证明号
  39. String signNo = StringUtils.cnulls(elecProofInfo.getSignNO());
  40. //签署完成后,调用osf服务,预览证明
  41. String billId = StringUtils.cnulls(elecProofInfo.getId());
  42. //调用osf服务
  43. HashMap param = new HashMap<String,String>();
  44. param.put("contractNo", signNo);
  45. param.put("billId", billId);
  46. param.put("isDefault", "1");
  47. Map<String, Object> result = (Map<String, Object>) OSFExecutor.executeOSF(ctx, param, "empPreViewECertificationV2Service");
  48. boolean isSucc = (Boolean)result.get("isSuccess");
  49. String returnUrl = StringUtils.cnulls( result.get("returnUrl") );
  50. //如果成功则把证件放入磁盘;
  51. if(isSucc) {
  52. String folderName = createFolderName();
  53. String fileName = getPDFToDisk(ctx, returnUrl, folderName);
  54. //发送消息
  55. String urlPath = HOME_URL+"?sole="+folderName+"&fname="+fileName;
  56. //文件夹名
  57. resultMap.put("dirName",folderName);
  58. //文件名
  59. resultMap.put("fileName",fileName);
  60. //url路径
  61. resultMap.put("urlPath",urlPath);
  62. }
  63. return resultMap;
  64. }
  65. public static String getPDFToDisk(Context ctx, String attachId, String dirName) throws IOException {
  66. String home = System.getProperty("EAS_HOME"); // 获取 EAS 根目录
  67. String fileDir = home+"/server/deploy/easweb.ear/shr_web.war/tmp/electronicAttachment/"+ dirName;
  68. fileDir = fileDir.replace("\\", "/"); //
  69. ServletOutputStream outputStream = null;
  70. OutputStream fileOutputStream = null; // 创建文件输出流
  71. try {
  72. AttachmentInfo ai = AttachmentFactory.getLocalInstance(ctx).getAttachmentInfo(new ObjectUuidPK(attachId));
  73. String fileName = ai.getName();
  74. String simpleName = ai.getSimpleName();
  75. IAttachmentManagerFacade iAttachmentManagerFacade = AttachmentManagerFacadeFactory.getLocalInstance(ctx);
  76. byte[] content = iAttachmentManagerFacade.downLoad(attachId);
  77. // 确保输出目录存在
  78. File directory = new File(fileDir);
  79. if (!directory.exists()) {
  80. directory.mkdirs(); // 创建目录
  81. }
  82. // 指定文件的完整路径
  83. File file = new File(directory, simpleName);
  84. fileOutputStream = new FileOutputStream(file); // 创建文件输出流
  85. fileOutputStream.write(content); // 将内容写入文件
  86. fileOutputStream.flush();
  87. return simpleName;
  88. } catch (EASBizException | BOSException e) {
  89. e.printStackTrace();
  90. } finally {
  91. try {
  92. if (fileOutputStream != null) {
  93. fileOutputStream.close(); // 关闭文件输出流
  94. }
  95. if (outputStream != null) {
  96. outputStream.close(); // 关闭 Servlet 输出流
  97. }
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. return "";
  103. }
  104. public static String createFolderName () {
  105. //使用uuid生成一个8位数的随机字符串作为文件夹名
  106. return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
  107. }
  108. }