Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi I'm relatively new to talend and I want to encode a file of .csv format to base64. I am able to convert the string inside the .csv file but want to encode the entire file. Something like this in the link https://www.freeformatter.com/base64-encoder.html
Hi,
Place this piece of Java code in a tJavaRow (in this example, the filename is in the current row):
// Get file from Document_ID + Name
String filename = input_row.Document_ID+"_"+input_row.Name;
//File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+(String)globalMap.get("ftpFilename"));
File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+filename);
FileInputStream documentInFile = null;
try {
// Read file
documentInFile = new FileInputStream(file);
byte documentData[] = new byte[(int) file.length()];
documentInFile.read(documentData);
// Convert bytes array to Base64 string to fill Body field
output_row.Body = new String(Base64.encodeBase64(documentData));
System.out.println("+++ File converted "+filename);
} catch (FileNotFoundException e) {
output_row.conversionCode = "FILE_NOT_FOUND";
output_row.conversionMessage = "File not found "+filename;
System.out.println("*** File not found "+filename+"\n"+e);
} catch (IOException ioe) {
output_row.conversionCode = "CONVERSION_ERROR";
output_row.conversionMessage = "Error converting file "+filename;
System.out.println("*** Error converting file "+filename+"\n"+ioe);
} finally {
try {
// Close and delete file after conversion
if (documentInFile != null) {
documentInFile.close();
//file.delete();
}
} catch (IOException e) {
output_row.conversionCode = "DELETE_ERROR";
output_row.conversionMessage = "Error deleting file "+filename;
System.out.println("*** Error deleting file "+filename+"\n"+e);
e.printStackTrace();
}
}
This is a travial solution, but it works.
Hope this helps.
Note: the conversion result goes to a field of the output row (Body). You just have to write the filed content to the desired file.
Hi,
Place this piece of Java code in a tJavaRow (in this example, the filename is in the current row):
// Get file from Document_ID + Name
String filename = input_row.Document_ID+"_"+input_row.Name;
//File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+(String)globalMap.get("ftpFilename"));
File file = new File((String)globalMap.get("ftpLocalDirectory")+"/"+filename);
FileInputStream documentInFile = null;
try {
// Read file
documentInFile = new FileInputStream(file);
byte documentData[] = new byte[(int) file.length()];
documentInFile.read(documentData);
// Convert bytes array to Base64 string to fill Body field
output_row.Body = new String(Base64.encodeBase64(documentData));
System.out.println("+++ File converted "+filename);
} catch (FileNotFoundException e) {
output_row.conversionCode = "FILE_NOT_FOUND";
output_row.conversionMessage = "File not found "+filename;
System.out.println("*** File not found "+filename+"\n"+e);
} catch (IOException ioe) {
output_row.conversionCode = "CONVERSION_ERROR";
output_row.conversionMessage = "Error converting file "+filename;
System.out.println("*** Error converting file "+filename+"\n"+ioe);
} finally {
try {
// Close and delete file after conversion
if (documentInFile != null) {
documentInFile.close();
//file.delete();
}
} catch (IOException e) {
output_row.conversionCode = "DELETE_ERROR";
output_row.conversionMessage = "Error deleting file "+filename;
System.out.println("*** Error deleting file "+filename+"\n"+e);
e.printStackTrace();
}
}
This is a travial solution, but it works.
Hope this helps.
Note: the conversion result goes to a field of the output row (Body). You just have to write the filed content to the desired file.
How can I get Document_ID and Name? Am using tFileInputRaw to connect to the tJavaRow. I can only see content from tFileInputRaw. Thank you very much for the repsonse.
Thank you sir,
It worked, I was trying to figure out how to add import statements in tJavaRow component. Now it's all good and working.