You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by harl3kin <fu...@gmx.de> on 2006/10/13 20:07:55 UTC

load files on tomcat

hello alltogether,

i have a problem with accessing files deployed on tomcat. I want to access
an image which is stored in a "graphics" folder right below the "WEB-INF"
folder. I read that the tomcat-context has its start path on the "classes"
folder. So what i did to retrieve the image was:

Image image = Toolkit.getDefaultToolkit().getImage("/../graphics/toon.gif");

When I debugged I recongnized that the image width and height is -1, so i
figure I didn't load the image correctly. Do you have any suggestions what I
can do to make this work?

I appreciate your time,
Thorsten
-- 
View this message in context: http://www.nabble.com/load-files-on-tomcat-tf2438873.html#a6800963
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by Michael Courcy <mi...@gmail.com>.
Or if your resource are strored in a package try
theObjectOf MyPackage.getClass().getResourceAsStream("image.gif");

Often used when sevlet inside a jar should serve content in the jar as well.


David Smith a écrit :
> If you are trying to read from a servlet, it's:
>
> getServletContext().getResource( "/WEB-INF/graphics/test.gif" )
> getServletContext().getResourceAsStream( "/WEB-INF/graphics/test.gif" ) ;
>
> The first one retrieves a URL object that can be used to open an input 
> stream.  The second returns an input stream you can use directly.
>
> --David
>
> harl3kin wrote:
>
>> Hey again,
>>
>> thanks for your replies. I just want to read the resource file and I 
>> tried
>> the getResource() method, it can't find the resource though. When I 
>> get the
>> absolute path of the context, I receive the file structure where the 
>> tomcat
>> is installed. But does tomcat internally have the same file 
>> structure? (i.e.
>> local tomcat installed on C:\progs\tomcat - ist it correct that tomcat
>> internally tries to retrieve the wanted file from C:\progs\tomcat\... ?)
>> I really tried a lot. I thought the problems arise because I have a
>> windows-tomcat the file structure uses the wrong slashes (so I tried 
>> using
>> "\\" instead of "/" - did not work). So to make it really clear:
>> 1. i have a local tomcat installed on windows
>> 2. i have an image deployed on that tomcat on
>> .../webapps/test/WEB-INF/graphics/test.gif
>>
>> question: how can a deployed class on the server correctly read the 
>> image that resides
>> on the same server?
>>
>> thanks again for your help and patience,
>> regards,
>> thorsten
>>
>>
>>
>>
>>
>> David Smith-2 wrote:
>>  
>>
>>> This is per the spec which explicitly states that the method 
>>> getRealPath() will return null if the webapp is executed from an 
>>> unexpanded .war file.  I believe it's even in the docs for the 
>>> servlet api.  If however your effort is in just reading the 
>>> resource, there are other methods which work regardless of being in 
>>> a .war file or not -- getResource( path ) and getResourceAsStream( 
>>> path ).
>>>
>>>
>>> --David
>>>
>>> Mark Miller wrote:
>>>   
>>>>> ,
>>>>> you could use ServletContext.getRealPath( ).  That will return the
>>>>> absolute path to the file, but only if the webapp isn't packed up 
>>>>> as .war.
>>>>>
>>>>> --David
>>>>>
>>>>>
>>>>>       
>>>> Why is this the case? Doesn't Tomcat extract to war to a 
>>>> webapp/warname
>>>> folder when it first accesses it?
>>>>
>>>> - Mark
>>>>
>>>>     
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>>
>>>   
>>
>>  
>>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by David Smith <dn...@cornell.edu>.
If you are trying to read from a servlet, it's:

getServletContext().getResource( "/WEB-INF/graphics/test.gif" )
getServletContext().getResourceAsStream( "/WEB-INF/graphics/test.gif" ) ;

The first one retrieves a URL object that can be used to open an input 
stream.  The second returns an input stream you can use directly.

--David

harl3kin wrote:

>Hey again,
>
>thanks for your replies. I just want to read the resource file and I tried
>the getResource() method, it can't find the resource though. When I get the
>absolute path of the context, I receive the file structure where the tomcat
>is installed. But does tomcat internally have the same file structure? (i.e.
>local tomcat installed on C:\progs\tomcat - ist it correct that tomcat
>internally tries to retrieve the wanted file from C:\progs\tomcat\... ?)
>I really tried a lot. I thought the problems arise because I have a
>windows-tomcat the file structure uses the wrong slashes (so I tried using
>"\\" instead of "/" - did not work). 
>So to make it really clear:
>1. i have a local tomcat installed on windows
>2. i have an image deployed on that tomcat on
>.../webapps/test/WEB-INF/graphics/test.gif
>
>question: 
>how can a deployed class on the server correctly read the image that resides
>on the same server?
>
>thanks again for your help and patience,
>regards,
>thorsten
>
>
>
>
>
>David Smith-2 wrote:
>  
>
>>This is per the spec which explicitly states that the method 
>>getRealPath() will return null if the webapp is executed from an 
>>unexpanded .war file.  I believe it's even in the docs for the servlet 
>>api.  If however your effort is in just reading the resource, there are 
>>other methods which work regardless of being in a .war file or not -- 
>>getResource( path ) and getResourceAsStream( path ).
>>
>>
>>--David
>>
>>Mark Miller wrote:
>>    
>>
>>>>,
>>>>you could use ServletContext.getRealPath( ).  That will return the
>>>>absolute path to the file, but only if the webapp isn't packed up as 
>>>>.war.
>>>>
>>>>--David
>>>>
>>>>
>>>>        
>>>>
>>>Why is this the case? Doesn't Tomcat extract to war to a webapp/warname
>>>folder when it first accesses it?
>>>
>>>- Mark
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To start a new topic, e-mail: users@tomcat.apache.org
>>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
>>    
>>
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by harl3kin <fu...@gmx.de>.
Hey again,

thanks for your replies. I just want to read the resource file and I tried
the getResource() method, it can't find the resource though. When I get the
absolute path of the context, I receive the file structure where the tomcat
is installed. But does tomcat internally have the same file structure? (i.e.
local tomcat installed on C:\progs\tomcat - ist it correct that tomcat
internally tries to retrieve the wanted file from C:\progs\tomcat\... ?)
I really tried a lot. I thought the problems arise because I have a
windows-tomcat the file structure uses the wrong slashes (so I tried using
"\\" instead of "/" - did not work). 
So to make it really clear:
1. i have a local tomcat installed on windows
2. i have an image deployed on that tomcat on
.../webapps/test/WEB-INF/graphics/test.gif

question: 
how can a deployed class on the server correctly read the image that resides
on the same server?

thanks again for your help and patience,
regards,
thorsten





David Smith-2 wrote:
> 
> This is per the spec which explicitly states that the method 
> getRealPath() will return null if the webapp is executed from an 
> unexpanded .war file.  I believe it's even in the docs for the servlet 
> api.  If however your effort is in just reading the resource, there are 
> other methods which work regardless of being in a .war file or not -- 
> getResource( path ) and getResourceAsStream( path ).
> 
> 
> --David
> 
> Mark Miller wrote:
>>> ,
>>> you could use ServletContext.getRealPath( ).  That will return the
>>> absolute path to the file, but only if the webapp isn't packed up as 
>>> .war.
>>>
>>> --David
>>>
>>>
>> Why is this the case? Doesn't Tomcat extract to war to a webapp/warname
>> folder when it first accesses it?
>>
>> - Mark
>>
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/load-files-on-tomcat-tf2438873.html#a6877475
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by David Smith <dn...@cornell.edu>.
This is per the spec which explicitly states that the method 
getRealPath() will return null if the webapp is executed from an 
unexpanded .war file.  I believe it's even in the docs for the servlet 
api.  If however your effort is in just reading the resource, there are 
other methods which work regardless of being in a .war file or not -- 
getResource( path ) and getResourceAsStream( path ).


--David

Mark Miller wrote:
>> ,
>> you could use ServletContext.getRealPath( ).  That will return the
>> absolute path to the file, but only if the webapp isn't packed up as 
>> .war.
>>
>> --David
>>
>>
> Why is this the case? Doesn't Tomcat extract to war to a webapp/warname
> folder when it first accesses it?
>
> - Mark
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by Mark Miller <ma...@gmail.com>.
> ,
> you could use ServletContext.getRealPath( ).  That will return the
> absolute path to the file, but only if the webapp isn't packed up as .war.
>
> --David
>
>
Why is this the case? Doesn't Tomcat extract to war to a webapp/warname
folder when it first accesses it?

- Mark

Re: load files on tomcat

Posted by David Smith <dn...@cornell.edu>.
The problem is the "current directory" is the startup directory for 
tomcat.  In a webapp that might be running on several different 
installations, the current working directory is a moving target.

Given this is always used from an exploded webapp and not a .war file, 
you could use ServletContext.getRealPath( ).  That will return the 
absolute path to the file, but only if the webapp isn't packed up as .war.

--David

José Roberto Motta Garcia wrote:

>Are u trying to access this image from a class or from a JSP? Please
>correct me people, but the root path to JSP files is
><TOMCAT_HOME>/webapps/<YOUR_APP_HOME>.
>
>JR
>
>harl3kin wrote:
>  
>
>>hello alltogether,
>>
>>i have a problem with accessing files deployed on tomcat. I want to access
>>an image which is stored in a "graphics" folder right below the "WEB-INF"
>>folder. I read that the tomcat-context has its start path on the "classes"
>>folder. So what i did to retrieve the image was:
>>
>>Image image = Toolkit.getDefaultToolkit().getImage("/../graphics/toon.gif");
>>
>>When I debugged I recongnized that the image width and height is -1, so i
>>figure I didn't load the image correctly. Do you have any suggestions what I
>>can do to make this work?
>>
>>I appreciate your time,
>>Thorsten
>>  
>>    
>>
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: load files on tomcat

Posted by José Roberto Motta Garcia <ga...@cptec.inpe.br>.
Are u trying to access this image from a class or from a JSP? Please
correct me people, but the root path to JSP files is
<TOMCAT_HOME>/webapps/<YOUR_APP_HOME>.

JR

harl3kin wrote:
> hello alltogether,
>
> i have a problem with accessing files deployed on tomcat. I want to access
> an image which is stored in a "graphics" folder right below the "WEB-INF"
> folder. I read that the tomcat-context has its start path on the "classes"
> folder. So what i did to retrieve the image was:
>
> Image image = Toolkit.getDefaultToolkit().getImage("/../graphics/toon.gif");
>
> When I debugged I recongnized that the image width and height is -1, so i
> figure I didn't load the image correctly. Do you have any suggestions what I
> can do to make this work?
>
> I appreciate your time,
> Thorsten
>   

-- 
Jose Roberto M. Garcia
Analista de Sistemas - Grupo Banco de Dados
Fone: (12) 3186-8405   Fax: (12) 3101-2835
--
http://www.cptec.inpe.br 
http://www.inpe.br 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org