File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes passing image files stored in hard drive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "passing image files stored in hard drive" Watch "passing image files stored in hard drive" New topic

passing image files stored in hard drive

Ted Addis

Joined: Nov 19, 2008
Posts: 11
I have image files stored in hard drive with their address stored as a field in database.

say for example, I would have CUSTOMER_ID would have "C:\IMAGE_CUSTOMERID.jpg" as its image address stored in as a varchar2 field and I want to send the actual image IMAGE_CUSTOMER_ID.jpg inside a web service.

How would you advice sending such files inside a web service. I read about sending it as binary, but can you post a sample example?

Thank you in advance,
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
In general, sending files via web services is not recommended - specially the media files because of their potential size. Alternatives to sending the file via web service would be to make the file available via ftp or similar and send the filename/location via web service. Another option is to use JMS to send the file (but again, depending on the size this may or may not be a good idea but it definitely works better than web service).

In the past people also sent files via base64 encoding. This encoding essentially turns an image into text, which can be embedded inside a SOAP message. You can find plenty of code samples on Google to encode an image into text and decode it back to image. Again, this may make the soap message way too big and isn't a preferred method anymore.

If you must send the file via web service, preferred way is to use MTOM/XOP. Find more information here, - Coming soon!
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Look into SAAJ - the SOAP with Attachments API for Java. You might start here at the Glassfish project.

Peer Reynders

Joined: Aug 19, 2005
Posts: 2933
The problem with web service attachments is that lack of supported standards. While MTOM/XOP is currently the preferred standard it is only supported by the most recent SOAP web service stacks. Earlier clients will only support a variety of even less well supported standards like DIME (not well supported under Java) and SOAP with Attachments (SwA) (not supported under .NET).

Given that you want to expose standard images it makes sense that you expose them as resources:


The second one streams the image for customer 12345678 while the third one streams the image for customer 87654321. These images can be displayed in a browser or easily received programmatically.

Frameworks like Jersey (JAX-RS; JSR-311) and Restlet are used to build resources like these.

The Jersey Sample Applications

The samples run on Glassfish - however you should be able to run Jersey on Tomcat. Restlet even has connectors to run on web servers like Grizzly or Apache MINA which perform better than servlet-based web servers (Benchmark shows Restlet scalability).
[ December 11, 2008: Message edited by: Peer Reynders ]
I agree. Here's the link:
subject: passing image files stored in hard drive
jQuery in Action, 3rd edition