hi guys
I am using worker in Java and needed to get a file as input (for example a pdf or txt file to attach to an email).
How can I get this file as task input? I received the email address and body of the email as follows:
List<LockedExternalTask> lockedTasks = externalTaskService.fetchAndLock(5, EXTERNAL_WORKER_ID)
.topic("externalTask", 60L * 1000L)
.variables(receiverEmailAddress, emailContent, xxxxxxx)
//xxxxxxx=> What should I put here to get the file and how to use it next?
.execute();
for (LockedExternalTask lockedTask : lockedTasks) {
LOGGER.info("externalTask : {} tasks locked", lockedTasks.size());
// get vars from current locked task
String emailAddress = (String) lockedTask.getVariables().get(receiverEmailAddress);
String content = (String) lockedTask.getVariables().get(emailContent);
// and blah blah blah
}
Hi @Bagher_Naem,
you have to call for the file variable explicitly as it is described here: Send file with HTTP Connector - #3 by marigianna_s.
Hope this helps, Ingo
hi @Ingo_Richtsmeier
I followed the topic and found that the file is given to us as ByteArrayInputStream. i convert it to an array of bytes and attach the file to the email using ByteArrayDataSource and DataHandler.
// create a BodyPart for add attachment to that
BodyPart attachmentFile = new MimeBodyPart();
// create a BodyPart for add html/text content to that
BodyPart text = new MimeBodyPart();
// create Multipart to include all BodyParts
Multipart multipart = new MimeMultipart();
// begin of set attachment file
byte [] fileInByteArray=inputStremToByteArray(file);
String fileType = getMimeType(fileInByteArray);
DataSource source = new ByteArrayDataSource(fileInByteArray, fileType);
// result of getMimeType for a xml file is like : application/xml
attachmentFile.setDataHandler(new DataHandler(source));
attachmentFile.setFileName("attachmentFile" + "." + fileType.split("/")[1]);
// end of set attachment file
// begin of set html/text content
text.setContent(body,
"text/html;charset=UTF-8");
// end of set html/text content
// add all BodyParts into MultiPart
multipart.addBodyPart(text);
multipart.addBodyPart(attachmentFile);
message.setContent(multipart);
thank you.