You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Security Management <li...@secmgmt.com> on 2009/02/06 15:03:20 UTC

Get an InputStream for a file in my webapp

I have an action that returns an image, and the getImageStream method
returns the stream associated with the blob in the database that I'm
pulling.

That works great, and displays/exports the image exactly like I want to.

However, there are cases where there is no image, and I want to display a
"No Image Found" image.

So, the getImageStream is:

public InputStream getImageStream()
        {
            if(this.imageStream != null)
            {
                return this.imageStream;
            }
            else
            {
                return new FileInputStream("no_image.jpg");
            }
        }

My questions is how do I get the path to no_image.jpg?  Here is its location
in the war file.  It is not on the classpath.

-rw-r--r--      4827  29-Oct-2008  19:47:26  images/no_image.jpg

Thanks,
Mike.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Get an InputStream for a file in my webapp

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
Using a FileInputStream like that won't work if the war is packaged.
Take a look at the getResourceAsStream method in the ServletContext
interface instead.

Nils-H

On Fri, Feb 6, 2009 at 3:03 PM, Security Management
<li...@secmgmt.com> wrote:
> I have an action that returns an image, and the getImageStream method
> returns the stream associated with the blob in the database that I'm
> pulling.
>
> That works great, and displays/exports the image exactly like I want to.
>
> However, there are cases where there is no image, and I want to display a
> "No Image Found" image.
>
> So, the getImageStream is:
>
> public InputStream getImageStream()
>        {
>            if(this.imageStream != null)
>            {
>                return this.imageStream;
>            }
>            else
>            {
>                return new FileInputStream("no_image.jpg");
>            }
>        }
>
> My questions is how do I get the path to no_image.jpg?  Here is its location
> in the war file.  It is not on the classpath.
>
> -rw-r--r--      4827  29-Oct-2008  19:47:26  images/no_image.jpg
>
> Thanks,
> Mike.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Get an InputStream for a file in my webapp

Posted by Musachy Barroso <mu...@gmail.com>.
You can implement the ServletContextAware interface in your action,
and Struts will set the ServletContext which has a "getPath" method,
or something like that.

musachy

On Fri, Feb 6, 2009 at 9:03 AM, Security Management
<li...@secmgmt.com> wrote:
> I have an action that returns an image, and the getImageStream method
> returns the stream associated with the blob in the database that I'm
> pulling.
>
> That works great, and displays/exports the image exactly like I want to.
>
> However, there are cases where there is no image, and I want to display a
> "No Image Found" image.
>
> So, the getImageStream is:
>
> public InputStream getImageStream()
>        {
>            if(this.imageStream != null)
>            {
>                return this.imageStream;
>            }
>            else
>            {
>                return new FileInputStream("no_image.jpg");
>            }
>        }
>
> My questions is how do I get the path to no_image.jpg?  Here is its location
> in the war file.  It is not on the classpath.
>
> -rw-r--r--      4827  29-Oct-2008  19:47:26  images/no_image.jpg
>
> Thanks,
> Mike.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org