Sample Code for Sending Email with Attachments:
Screen shot in sample activity implementation:
Sample java code for reading the Attachment:
ClipboardPage page_Attachment = null,
page_EmbedAttachment = null,
page_AttachmentPage = tools.findPage("AttachmentPage");
ClipboardProperty prop_pyAttachment = null;
prop_pyAttachment = page_AttachmentPage.getProperty(".pyAttachments(<append>)");
page_EmbedAttachment = prop_pyAttachment.getPageValue();
java.io.DataInputStream dis = null;
String strFileName = "C://EmployeeDetails1.xls";
String strTheFileName="";
java.util.HashMap hmFileInfo = new java.util.HashMap();
java.io.File objFile = new java.io.File(strFileName);
byte buffer [] = null;
java.lang.StringBuilder strFileData = null;
try {
String sCurrentLine;
dis = new java.io.DataInputStream(new java.io.FileInputStream(objFile));
int bufferSize = 2048001;
int fileSize = dis.available();
System.out.println("fileSize"+fileSize);
strFileData = new StringBuilder((int) (fileSize * 1.4));
int noOfIterations = fileSize / bufferSize;
for (int j = 0; j < noOfIterations; j++) {
buffer= new byte[bufferSize];
dis.read(buffer, 0, bufferSize);
//bytes.toString()
System.out.println("Buffer Size "+buffer);
strFileData.append(Base64Util.encodeToString(buffer)); //to encode to Base64
//strFileData.append(buffer);
}
int remaining = fileSize % bufferSize;
buffer = new byte[remaining];
dis.read(buffer, 0, remaining);
strFileData.append(Base64Util.encodeToString(buffer));
//strFileData.append(buffer);
//strFileData.append(Base64Util.encodeToString(strFileData));
dis.close();
if (strFileData == null)
{
//throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");
}
strTheFileName = objFile.getName();
String strFileExtension = "";
int nIdx = strTheFileName.lastIndexOf(".");
if (nIdx > -1){
strFileExtension = strTheFileName.substring(nIdx + 1);
}
String decodedFileName = StringUtils.decodeCrossOSString(strTheFileName);
System.out.println("Extension "+strFileExtension+","+strTheFileName+",file date"+strFileData);
page_EmbedAttachment.putString(".pyReference", "");
page_EmbedAttachment.putString(".pyData", strFileData.toString());
page_EmbedAttachment.putString(".pyName",decodedFileName);
page_EmbedAttachment.putString(".pyDecode","true");
oLog.infoForced("string"+strFileData.toString());
System.out.println(strFileData);
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)dis.close();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
}
Screen shot in sample activity implementation:
Sample java code for reading the Attachment:
ClipboardPage page_Attachment = null,
page_EmbedAttachment = null,
page_AttachmentPage = tools.findPage("AttachmentPage");
ClipboardProperty prop_pyAttachment = null;
prop_pyAttachment = page_AttachmentPage.getProperty(".pyAttachments(<append>)");
page_EmbedAttachment = prop_pyAttachment.getPageValue();
java.io.DataInputStream dis = null;
String strFileName = "C://EmployeeDetails1.xls";
String strTheFileName="";
java.util.HashMap hmFileInfo = new java.util.HashMap();
java.io.File objFile = new java.io.File(strFileName);
byte buffer [] = null;
java.lang.StringBuilder strFileData = null;
try {
String sCurrentLine;
dis = new java.io.DataInputStream(new java.io.FileInputStream(objFile));
int bufferSize = 2048001;
int fileSize = dis.available();
System.out.println("fileSize"+fileSize);
strFileData = new StringBuilder((int) (fileSize * 1.4));
int noOfIterations = fileSize / bufferSize;
for (int j = 0; j < noOfIterations; j++) {
buffer= new byte[bufferSize];
dis.read(buffer, 0, bufferSize);
//bytes.toString()
System.out.println("Buffer Size "+buffer);
strFileData.append(Base64Util.encodeToString(buffer)); //to encode to Base64
//strFileData.append(buffer);
}
int remaining = fileSize % bufferSize;
buffer = new byte[remaining];
dis.read(buffer, 0, remaining);
strFileData.append(Base64Util.encodeToString(buffer));
//strFileData.append(buffer);
//strFileData.append(Base64Util.encodeToString(strFileData));
dis.close();
if (strFileData == null)
{
//throw new PRRuntimeException("Can't continue with file upload. Couldn't encode the file to Base64 so that we can store it on the database");
}
strTheFileName = objFile.getName();
String strFileExtension = "";
int nIdx = strTheFileName.lastIndexOf(".");
if (nIdx > -1){
strFileExtension = strTheFileName.substring(nIdx + 1);
}
String decodedFileName = StringUtils.decodeCrossOSString(strTheFileName);
System.out.println("Extension "+strFileExtension+","+strTheFileName+",file date"+strFileData);
page_EmbedAttachment.putString(".pyReference", "");
page_EmbedAttachment.putString(".pyData", strFileData.toString());
page_EmbedAttachment.putString(".pyName",decodedFileName);
page_EmbedAttachment.putString(".pyDecode","true");
oLog.infoForced("string"+strFileData.toString());
System.out.println(strFileData);
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)dis.close();
} catch (java.io.IOException ex) {
ex.printStackTrace();
}
}
Thanks, that helped. Let's add some suggestions, if the tracer reports exceptions on the file (like "java.io.FileNotFoundException: the system cannot find the file specified"), specify path and file separately or check the path to the file (a. add the parameter to the activity "File", of type string; b. retrieve the parameter with the instruction "String filePath = tools.getParamValue("File");"; c. create the File object with the instruction "java.io.File objFile = new java.io.File(filePath);"; d. get the path of the file with the statement "String path = objFile.getPath();"; e. add some printouts to check the paths, like: "oLog .infoForced("Absolute path: " + new java.io.File("..").getAbsolutePath());" and "oLog.infoForced("Current path: " + path);")
ReplyDelete