File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Serving binaries to clients from a packaged application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serving binaries to clients from a packaged application" Watch "Serving binaries to clients from a packaged application" New topic
Author

Serving binaries to clients from a packaged application

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hi.
I already know how to serve files (PDF, image, Zip...) to clients via a servlet.
This was working when the application was deployed as a dir but when it is packed as an EAR I got FileNotFoundException.
Any work around?
Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How are you locating these resources?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

String path = request.getContextPath() + "/main/Manual.pdf";
File file = new File(path);
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you sure?
getContextPath only returns the application name.
Did you mean getRealPath?
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

No, I mean getContextPath()
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Then I'm surprised that that was ever working (ear file or not).

The getContextPath method doesn't return the filesystem path, which is what java.io.File needs.

In any case, if the file you want to stream to the user is inside your webapp's directory structure, the best way to access it is with context.getResourceAsStream. This method will return a streamed version of your resource regardless of whether it is a file on the operating system or (in your case) a resource bundled in a jar, war, or ear file.

We have an example app that streams images from behind the WEB-INF directory in our CodeBarn. It's in the servlets section, look for SimpleStream.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serving binaries to clients from a packaged application