Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How do I encode a csv file to base64?

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

Labels (3)
1 Solution

Accepted Solutions
TRF
Champion II

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.

View solution in original post

4 Replies
TRF
Champion II

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.

Anonymous
Not applicable
Author

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.

TRF
Champion II

You need to adapt the example to your use case.
Forget Document_ID.
In your case you may retrieve the filename from tFileInputRaw using "(String)globalMap.get("tFileInputRaw_1.FILENAME_PATH")".
Now you just need to replace the path or the filename.
For example, replace ".csv" by "_base64.csv" to have a new file arr the same place than the original one.
Anonymous
Not applicable
Author

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.