我的要求是使用通用 PDF,我必须为电子邮件附加静态 PDF,我可以毫无问题地附加通用 PDF,但它在从目录中获取静态 PDF 时给我带来了问题,我已经尝试了几种方法,请您提供帮助....
下面是错误和与之相关的代码。
Error :java.io.FileNotFoundException: /mnt/DGB/Correspondence/Systems/PROD_DOCS/How_to_access_member_information.pdf (No such file or directory)
代码:
try {
File pdfFile = new File("//mnt/DGB/Correspondence/Systems/PROD_DOCS/How_to_access_member_information.pdf");
byte[] bytesArray = new byte[(int) pdfFile.length()];
FileInputStream fis = new FileInputStream(pdfFile);
fis.read(bytesArray); //read file into bytes[]
fis.close();
String registerId = notificationEngineService.registerFileOnNe("application/pdf", "How_to_access_member_information.pdf", bytesArray);
System.out.println("registerId 1=============================== " + registerId);
notificationEngineService.sendRegisteredAttViaNe(registerId, emailBody, dispInfo);
} catch (Exception e) {
System.out.println("Exception 10============================================================");
e.printStackTrace();
}
try {
File pdfFile = new File("\\dcpcifs01\DGB\Correspondence\Systems\PROD_DOCS\How_to_access_member_information.pdf");
byte[] bytesArray = new byte[(int) pdfFile.length()];
FileInputStream fis = new FileInputStream(pdfFile);
fis.read(bytesArray); //read file into bytes[]
fis.close();
String registerId = notificationEngineService.registerFileOnNe("application/pdf", "How_to_access_member_information.pdf", bytesArray);
System.out.println("registerId 2=============================== " + registerId);
notificationEngineService.sendRegisteredAttViaNe(registerId, emailBody, dispInfo);
} catch (Exception e) {
System.out.println("Exception 10============================================================");
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
throw new GroupRiskSystemException(ExceptionCode.COMPASS_ERROR.name());
}
return "";
}
private void sendEmail(MbsMembers memberObject) {
try {
System.out.println(" ======================Start0=================================== ");
za.co.discoverygrouprisk.common.jaxb.email.AttachmentType attachmentType = new za.co.discoverygrouprisk.common.jaxb.email.AttachmentType();
attachmentType.setMember(new MemberType());
attachmentType.setCamundaProcessId("0");
attachmentType.setFileName("How_to_access_member_information.pdf");
attachmentType.setChildBusinessKey(0l);
attachmentType.setNeID(0l);
DGRMultiAttachmentEmailDetailV01 emailDetail = new DGRMultiAttachmentEmailDetailV01();
SchemeDataType schemeDataType = new SchemeDataType();
SchemeType schemeType = new SchemeType();
SchemeNumberType schemeNumberType = new SchemeNumberType();
schemeNumberType.setValue(01);
schemeType.setSchemeNumber(schemeNumberType);
schemeDataType.setScheme(schemeType);
emailDetail.setSchemeData(schemeDataType);
EmailDataType emailDataType = new EmailDataType();
EmailType emailType = new EmailType();
emailType.setSubject("How to access member information");
emailType.setFromAddress("groupinfo@discovery.co.za");
emailType.setToAddress(memberObject.getEmailAddress());
emailDataType.setEmail(emailType);
emailDetail.setEmailData(emailDataType);
AttachmentDataType attachmentDataType = new AttachmentDataType();
// attachmentDataType.setLocation("//mnt/DGB/Correspondence/Systems/PROD_DOCS/");
attachmentDataType.setLocation("\\dcpcifs01\DGB\Correspondence\Systems\PROD_DOCS\");
//mnt/DGB/Correspondence/2020/QA/MEMBER_REQUIREMENT_LETTER
attachmentDataType.setParentBusinessKey(01);
attachmentDataType.getAttachment().add(attachmentType);
emailDetail.setAttachmentData(attachmentDataType);
EmailDataSource adHocDS = new AdHocEmailDataSource(emailDetail);
String emailBody = createEmailBody(memberObject);
StandardEmailTemplate template = new StandardEmailTemplate(emailBody);
Email email = new StandardEmail(adHocDS, template);
email.createEmail();
email.sendEmail();
System.out.println(" ======================End0=================================== ");
} catch (Exception e) {
System.out.println(" ======================Exception0=================================== ");
e.printStackTrace();
}
}
在下面使用:
File pdfFile = new File("/mnt/DGB/Correspondence/Systems/PROD_DOCS/How_to_access_member_information.pdf");
添加以下内容:
在用户主目录下创建一个目录说:/home/user_name/java-pdf
。 然后尝试以下代码一次,以检查您的代码是否能够访问该文件:
File homedir = new File(System.getProperty("user.home"));
File pdfFile = new File(homedir, "java-pdf/How_to_access_member_information.pdf");
上面的代码对我来说运行良好。