Writing Binary Files from Java

I’ve seen lots of discussion here about writing files to the filesystem from webMethods IS. But it seems that these services are typically content with writing string data to the file. I’m trying to put together a service to transfer files from one server to another (I can’t use FTP), and these files will in many cases contain binary data (graphics and such).

Now I can use pub.file:getFile to load the files, but I can’t quite figure out how to write that data back out to the filesystem without munging it into a string and back to bytes. Does anyone have any ideas/experience regarding this problem? TIA.

Use getFile to read as bytes. This will give you the output as an array of bytes. I presume you are using remote invokes to send the data to the remove server. If that is the case then you can just pass the bytes over the network without any problems.

Thanks Rupinder, but that part I’ve got down failry well :-). In fact that’s exactly what I’m doing: a remote invoke to getFile with loadAs=bytes (default). This works beautifully and I can even write the file out to the calling server’s filesystem with a custom Java service - as long as the fileContents is a string.

The difficulty I’m having is that in my Java service the pipeline cursor’s getValue method returns an Object, not a byte. I thought I could simply cast the Object to a byte array for use as input to some form of fileWriter, but it seems that it’s not that easy. I’m no Java pro by any means and I’ve found dozens of different types of fileWriters, outputStreams and the like but I can’t seem to get the right combination to be able to stream/write the data from the pipeline to the file (without destroying it).

You are right. The case should work. Try the following code :

byte bytes = (byte)cursor.getValue(“bytes”);
try
{
FileOutputStream fos = new FileOutputStream(“C:\somefile.gif”);
fos.write(bytes);
fos.close();
}
catch (Exception e)
{
throw new ServiceExcetpion(e);
}

Ah, now we’re on the same page. :slight_smile: But as memory serves, that was one of the first things I tried, and it wouldn’t compile! Why I can’t cast an Object to a byte array I don’t know, but I’ll try that again when I get a chance. In the meantime, I think I may have found a feasible approach with something like:

// position cursor at fileContent, then …
Object data = cursor.getValue();

OjbectOutputStream oos = new ObjectOutputStream (new FileOutputStream (filename));

oos.writeObject(data);
oos.flush;
oos.close;

This compiled successfully, but unfortunately another problem with my server is preventing me from testing it right now. So I’ll have to try it in the morning.

Mike,

One of the things you may want to do is consider using the built in MIME/SMIME services that is part of the webMethods pub.mime package. This will allow you to mix/match content dynamically from both ends. The upside is that the MIME services follow the w3c standard, the downside is more coding on each end. Hope this helps.

ray

Mike,

a byte is an object. So the compiler should not complain about a cast to (byte).

You must have made some other mistake that would prevent it from compiling. I have exactly the same code working. In fact, since most of our servers are remote and we dont have direct access to their filesystems we have the copyRemote service on all of them. And we use the same code that I posted here.

OK, server problem fixed and I’ve had a chance to check out the options. You’re exactly right Rupinder. Thanks! Must have been some other quirk first time around. Also, my last attempt with the ObjectOutptutStream compiled just fine, but wasn’t quite on track at run time.

Ray, your suggestion sounds interesting but I’m unfamiliar with those services (I know MIME sorta/kinda, but haven’t used wM services to do anything in that area). Could you elaborate a bit, or just outline what one would do with these services? Thanks.

BTW - wMUsers rocks!