[resolved] Is possible to download a file through tRESTResponse

Hi,
I need to build a job using tRESTRequest and tRESTResponse where it will download a pdf file from a server and store it im my server, then the tRESTResponse should show the file.
I tried somithing like this :
tRESTRequest -> tFileFetch -> tRESTResponse
I'm getting the file and storing it where I want, but I'm not able to fill the tRESTResponse with the pdf file.

Does someone know how to do it?
Thanks,
Gabriel
1 ACCEPTED SOLUTION

Accepted Solutions
Four Stars

Re: [resolved] Is possible to download a file through tRESTResponse

Hi,
Following is the routine which converts input data to byte[]
package routines;public class MyRoutineDemo {
public static byte[] ByteArrayFromFile(String filepath) {
try{
java.io.File file=new java.io.File(filepath);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
int fileLength = (int) file.length();
byte[] incoming_file_data = new byte; // allocate byte array of right size
fis.read(incoming_file_data, 0, fileLength ); // read into byte array
fis.close();
return incoming_file_data;
}catch(Exception err){
err.printStackTrace();return null;
}
}
}
Refer to following link for further details
https://community.talend.com/t5/Design-and-Development/Inserting-BLOB-or-CLOB-data-into-a-database/t...
Thanks
Vaibhav
2 REPLIES

Re: [resolved] Is possible to download a file through tRESTResponse

In the case of someone's interest, here is the solution:
tRESTRequest
||
\/
tFileFetch (here I get the file and store it in a local folder)
||
\/
tJavaRow (here is the important part: I transform the file that I got in tFileFetch in a byte[] using java code)
||
\/
tMap (here is another important part: I map the byte[] from tJavaRow's output to the input of the tRESTResponse's body, wich is also a byte[])
||
\/
tRESTResponse (I set the Return Body Type as byte[] and set the following header:
"Content-Type" = "application/pdf"
"Content-Disposition" = "attachment; filename=filename.pdf"
"Content-Description" = "File Transfer"
)

I hope this can help someone,
Cheers
Four Stars

Re: [resolved] Is possible to download a file through tRESTResponse

Hi,
Following is the routine which converts input data to byte[]
package routines;public class MyRoutineDemo {
public static byte[] ByteArrayFromFile(String filepath) {
try{
java.io.File file=new java.io.File(filepath);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
int fileLength = (int) file.length();
byte[] incoming_file_data = new byte; // allocate byte array of right size
fis.read(incoming_file_data, 0, fileLength ); // read into byte array
fis.close();
return incoming_file_data;
}catch(Exception err){
err.printStackTrace();return null;
}
}
}
Refer to following link for further details
https://community.talend.com/t5/Design-and-Development/Inserting-BLOB-or-CLOB-data-into-a-database/t...
Thanks
Vaibhav