read an audio file from action class
I am trying to read an audio file(which is in my war file) from my action class( which is in my ear file).. I am using
java.io.File inputFile = new File(filename);
FileInputStream in = new FileInputStream(inputFile);
and then writing it through outStream.write();
But my code is unable to locate the inputFile..How do i point to the audio file (which is in my war) from action class( which is in my ear)
[435 byte] By [
srija] at [2007-11-27 8:33:26]

Get a handle to the WAR and then use an unzip library to read from it?
RATiXa at 2007-7-12 20:29:35 >

i tried thisInputStream is = getClass().getClassLoader().getResourceAsStream("abc.wav");but its not working
srija at 2007-7-12 20:29:35 >

Could you write a small piece of code on how to get the handle of the war and unzip the library ?
srija at 2007-7-12 20:29:35 >

Yes, sorry, that will only read a file that is inside your archive. If your WAR is in your EAR, you should be able to get a handle to your WAR using that chain of methods and then use:
URL warUrl = getClass().getClassLoader().getResource("myfile.war");
//the following code, may not work. if not, append:
//(you will need to catch malformedurlexception)
//warUrl = new URL("jar:" + warUrl.toExternalForm());
JarFile jar = ((JarURLConnection)warUrl.openConnection()).getJarFile();
for (JarEntry entry : jar.entries()) {
//use entry.getName(); to see its name
//use jar.getInputStream(entry); to read from it
}
Note: I did not test any of this.
Message was edited by:
RATiX
RATiXa at 2007-7-12 20:29:35 >

Is there any easier way to do this..My requirement is just to load an audio file in a servlet and then add that to response stream...
srija at 2007-7-12 20:29:35 >

The easier way(s) would be to put the audio file either in the EAR directly or put it outside of your EAR.If this is for a Web application, why not make a directory structure like "resources/audio/" and put the music file in there?
RATiXa at 2007-7-12 20:29:35 >

Here is my application package structure
mainapp.ear
--> abc.jar (this has my servlets and action classes)
> web.war (this has my jsp, html, audio.wav file)
Now i want to load the audio.wav file from my action or servlet class..! How can i do this..i am struggling
srija at 2007-7-12 20:29:35 >

Like I said, why can't you just put the audio file outside of the EAR?
Otherwise, to my knowledge (which admittedly isn't that great with respect to J2EE), you'll need to get a handle on the EAR, open it programmatically, then open the WAR file inside of that, and then search for your .wav file.
RATiXa at 2007-7-12 20:29:35 >

if i put it outside of the ear, how can retrieve it from my action or servlet..Is it using absolute path ?
srija at 2007-7-12 20:29:35 >

No, you should be able to just use a relative path (like your original code... File audioFile = new File("audio.wav");)
RATiXa at 2007-7-12 20:29:36 >

How do i know where i should keep the wav file ? where does the above line pick it from
srija at 2007-7-12 20:29:36 >

From the same directory in which the EAR file resides.
RATiXa at 2007-7-12 20:29:36 >

Stick it under the WEB-INF directory, probably creating another directory there, then use getServletContext().getResourceAsStream("WEB-INF/audio/my.wav");
Excellent. thank you very much.. I Just placed my audio file inside WEB_INF folder instead of putting it outside it.. Thank you very much to Malcolm and RATix both of you guys for your help..!
srija at 2007-7-12 20:29:36 >
