You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Andy Chu <an...@gmail.com> on 2005/08/24 04:04:38 UTC

How to locate a file in a self-defined service ?

Dear All,

In my self-defined service, an IEngineSerivce, I need to access files under 
${CONTEXT}/myfiles. Would you please tell me how to access them with a 
FileInputStream ?

Thank you very much.

Best regard,
Andy

-- 
我是天空裡的一片雲
偶爾投影在妳的波心

----偶然.徐志摩

Re: How to locate a file in a self-defined service ?

Posted by Kent Tong <ke...@cpttm.org.mo>.
XYZ <x.y.zhao.88 <at> gmail.com> writes:

> 
> Try using assets... (you can inject it or pass it as a parameter)
> 
>    ...
>     <at> Asset("context:/myfile")
>    public abstract IAsset getFile();
> 
>    ...
>    {
>       BufferedReader reader = new BufferedReader(new InputStreamReader(
>                      getFile().getResourceAsStream(cycle)));
>       ...

He's trying to do it in a custom service, so he can't just
use @Asset to inject a context asset. In addition, it's
an overkill to use a context asset because he doesn't need
i18n support and doesn't use it in an Image or Shell. If
he is happy to access the file through HTTP as in the case
of an asset, then a simpler way is:

  URL url = new URL(cycle.getAbsoluteURL("/myfile"));
  InputStream s = url.openStream();

--
Author of an e-Book for learning Tapestry (http://www.agileskills2.org/EWDT)


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


Re: How to locate a file in a self-defined service ?

Posted by XYZ <x....@gmail.com>.
Try using assets... (you can inject it or pass it as a parameter)

   ...
   @Asset("context:/myfile")
   public abstract IAsset getFile();

   ...
   {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
                     getFile().getResourceAsStream(cycle)));
      ...

On 20050825, at 220250, Andy Chu wrote:

> Eclipse shows me this method is deprecated. Is any more elegant way
> available ?
>
> On 8/25/05, Kent Tong <ke...@cpttm.org.mo> wrote:
>
>>
>> Andy Chu <andychu <at> gmail.com <http://gmail.com>> writes:
>>
>>
>>>
>>> Dear All,
>>>
>>> In my self-defined service, an IEngineSerivce, I need to access  
>>> files
>>>
>> under
>>
>>> ${CONTEXT}/myfiles. Would you please tell me how to access them  
>>> with a
>>> FileInputStream ?
>>>
>>
>> Try:
>> String path =
>> cycle.getRequestContext().getServlet().getServletContext().
>> getRealPath("/myfiles/foo");
>> FileInputStream stream = new FileInputStream(path);
>>
>> --
>> Author of e-book for learning Tapestry (www.agileskills2.org/ 
>> EWDT<http://www.agileskills2.org/EWDT>
>> )
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user- 
>> help@jakarta.apache.org
>>
>>
>>
>
>
> -- 
> 我是天空裡的一片雲
> 偶爾投影在妳的波心
>
> ----偶然.徐志摩
>


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


Re: How to locate a file in a self-defined service ?

Posted by Andy Chu <an...@gmail.com>.
Eclipse shows me this method is deprecated. Is any more elegant way 
available ?

On 8/25/05, Kent Tong <ke...@cpttm.org.mo> wrote:
> 
> Andy Chu <andychu <at> gmail.com <http://gmail.com>> writes:
> 
> >
> > Dear All,
> >
> > In my self-defined service, an IEngineSerivce, I need to access files 
> under
> > ${CONTEXT}/myfiles. Would you please tell me how to access them with a
> > FileInputStream ?
> 
> Try:
> String path =
> cycle.getRequestContext().getServlet().getServletContext().
> getRealPath("/myfiles/foo");
> FileInputStream stream = new FileInputStream(path);
> 
> --
> Author of e-book for learning Tapestry (www.agileskills2.org/EWDT<http://www.agileskills2.org/EWDT>
> )
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


-- 
我是天空裡的一片雲
偶爾投影在妳的波心

----偶然.徐志摩

Re: How to locate a file in a self-defined service ?

Posted by Kent Tong <ke...@cpttm.org.mo>.
Andy Chu <andychu <at> gmail.com> writes:

> 
> Dear All,
> 
> In my self-defined service, an IEngineSerivce, I need to access files under 
> ${CONTEXT}/myfiles. Would you please tell me how to access them with a 
> FileInputStream ?

Try: 
String path = 
cycle.getRequestContext().getServlet().getServletContext().
getRealPath("/myfiles/foo");
FileInputStream stream = new FileInputStream(path);

--
Author of e-book for learning Tapestry (www.agileskills2.org/EWDT)


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