[resolved] Is possible to download a file through tRESTResponse

[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

Accepted Solutions
Highlighted
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

View solution in original post


All Replies
Highlighted

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
Highlighted
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

View solution in original post

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog