cancel
Showing results for 
Search instead for 
Did you mean: 

FileOutputStream to Write file from URL Stream

Former Member
0 Kudos

We have an incoming payload with an identifier tag for attachments. The value of the attachment node is a URL that will be used to access the actual attachment. Sender Comm Channel is HTTP.

<ATTACHMENTS> <DOCINFO> ... <FILEDESTINATION>FilePath</FILEDESTINATION> ... <WEBURL>http://.../file.pdf</WEBURL> </DOCINFO> </ATTACHMENTS>

We've created a UDF to fetch the attachment (eg. PDF file) from the URL and 'write' it to a specific folder. Logic of the UDF is:


public String GetAttachment(String WebUrl, String FileDestination, Container container){
   try {
	int oneChar;
	URL url = new URL(WebUrl);
	InputStream is = url.openStream();
	File file = new File(FileDestination);
	FileOutputStream fos=new FileOutputStream(file);

	System.out.println("ReadFilefromWeb : Before reading the file...");
	
	while ((oneChar=is.read()) != -1)
	{
		fos.write(oneChar); 
	}
	
	is.close();
	fos.close(); 


         } catch (Exception e) {
	   // TODO: handle exception
	   e.printStackTrace();
         }
          
return WebUrl;
}

Outside PI, the logic works. However, when being used as a UDF in the Message Mapping, the output file can't be found. Testing data included

A. WebUrl - Actual location of attachment

B. FileDestination - (PI Directory) /usr/sap/xi/DVX/Trial --> as seen from tcode AL11

Is this because:

1) The folder path is only a relative path and needs the absolute path for proper location?

2) UDF logic cannot write to a PI directory/folder?

3) instead of UDF, we need a custom bean to be placed in the Comm Channel?

4) None of the above. So, what happened?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

The best option will be to write the Bean or module and call it in the Communication channel.

Regards,

Phani

Former Member
0 Kudos

Thanks! I'll try this option.

Former Member
0 Kudos

But, what would now be the difference between the FileOutputStream logic in Message Mapping as a UDF versus the logic in a custom bean/module?

Former Member
0 Kudos

Maybe it's a security issue.

Can you write from the URL to another stream without any problem?

Martin

Former Member
0 Kudos

I initially tried it outside PI/in my computer. I am able to write the file in my desktop.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>> I initially tried it outside PI/in my computer. I am able to write the file in my desktop.

Looks like during mapping file writing has some security issues. so it is not happening. Talk to Basis team and see where you can create the file..

a) Just for testing purpose, if you have file adapter interfaces and see the folder where they create ... Use that directory path to create and see how that works.

b) Also do this step in your UDF... check it helps or not

// Below line before your read the file....

file.setWritable(true);