• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serving binaries to clients from a packaged application

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you locating these resources?
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String path = request.getContextPath() + "/main/Manual.pdf";
File file = new File(path);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure?
getContextPath only returns the application name.
Did you mean getRealPath?
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I mean getContextPath()
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic