Support Article
Read an image file using a file listener
SA-3840
Summary
User needs to configure a file listener to read an image and attache it to a newly created work object
Queries are as follows:
Can the user get the properties needed from the LogServiceFile page,
How do we read the image stream and encode it in base64 encoding so that i can set it to the pyAttachStream property of the Data-WorkAttach-File class page.
User has a code that reads this stream using methods form commons-codec-1.5.jar but this is not available in 6.1sp2 and there is a security concern so he has to use any of the jars existing in prpc6.1 sp2.
So what code should he use to read the file stream and encode it to base 64 format and set the clipboard property pyAttachStream.
Resolution
Below are the steps to read the file and stores the base64 encoded stream to a string propety.
1. Use any page for "Page name" field on Service tab of Service File rule form(For ex. fileListenerPage of your work class).
2. Use “File at a time” for Processing Mode and “text only” for Data Mode fields on Method tab. Set Character Encoding to UTF-8.
3. Map the file stream to any text clipboard property(For example fileListenerPage .test).
4. In the Service Activity add a Java step and use below code.
try
{
java.io.ByteArrayInputStream inByteStream = new java.io.ByteArrayInputStream(tools.findPage("fileListenerPage").getString("test").getBytes(java.nio.charset.StandardCharsets.UTF_8));
//new java.io.ByteArrayInputStream(buffer);
java.io.InputStream inStream = new java.io.BufferedInputStream(inByteStream);
java.io.ByteArrayOutputStream outByteStream = new java.io.ByteArrayOutputStream();
java.io.OutputStream outStream = new java.io.BufferedOutputStream(outByteStream);
java.io.OutputStream encStream = Base64Util.encode(outStream);
java.io.DataOutputStream doStream = new java.io.DataOutputStream(encStream);
byte[] buffer2 = new byte[4096];
int len;
while(( len = inStream.read(buffer2)) != -1)
{
doStream.write(buffer2, 0, len);
}
inStream.close();
doStream.flush();
outStream.flush();
strFileData = outByteStream.toString(); // This sets the strem to a string property.
doStream.close();
outStream.close();
// ClipboardPage cpNFAttach = tools.findPage("AFilePage", false);
// cpNFAttach.putString("pyAttachStream", strFileData);
// close the fos and delete the file
fos.close();
// fileName.delete();
}
catch (Exception e)
{
}
Published January 31, 2016 - Updated October 8, 2020
Have a question? Get answers now.
Visit the Collaboration Center to ask questions, engage in discussions, share ideas, and help others.