Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Need help with getResource() problem under weblogic 6.1

瀏覽次數:39 次
跳到第一則未讀訊息

John Hilgedick

未讀,
2002年11月20日 下午6:22:162002/11/20
收件者:
Hey all,

I've got a serious problem that I can't figure out. This should be *really*
simple - but it's alluding me. I have an ear file with a number of servlets
inside it. This ear file also contains a resource that my servlets want to
read from. This resource is located (within the ear file) at:
"/content/structure/a.txt". The file contains raw ascii.

To have my serlvet read from this file, I thought it would be as simple as:

URL content =
servlet.getServletContext().getResource("/content/structure/a.txt");
// pw is an instance of PrintWriter
pw.print(content.getContent());

However, when I execute the above I get the following exception:

java.lang.NullPointerException
at
java.net.URLConnection.stripOffParameters(URLConnection.java:1011)
at java.net.URLConnection.getContentHandler(URLConnection.java:979)
at java.net.URLConnection.getContent(URLConnection.java:581)

OK, so fine - I figure that I'm new to this. The documentation for the URL
class tells me there is another way around this. It says that rather then
calling getContent() directly, I can open a stream on the URL and read from
that, like so:

URL content =
servlet.getServletContext().getResource("/content/structure/scenario2resourc
es.txt");
FileInputStream fis = content.openStream();

but when I do this I get the following different exception:

java.lang.ClassCastException: weblogic.utils.zip.SafeZipFileInputStream^M
at Utility.writeFileToOutput(Utility.java:134)^M

Apparently this thing is trying to return a web-logic-specific stream (which
is incompatible with an FileInputStream) - which I don't want to use or
learn about since I want my stuff to work under other webservers.

Can anyone tell me what I'm doing wrong? This should be simple.

-john


--
John Hilgedick
WisdomTools, Inc.
john.hi...@wisdomtools.com


Wenjin Zhang

未讀,
2002年11月20日 晚上8:03:142002/11/20
收件者:

The "/" in getServletContext().getResource("/content/structure/a.txt") means the
web (WAR) application root, not your EAR root.

The ClassCastException is because that content.openStream() is not FileInputStream
and has nothing to do with Weblogic. You should treat it as normal InputStream.

John Hilgedick

未讀,
2002年11月20日 晚上10:10:372002/11/20
收件者:
Wenjin,

I appreciate your response. I tried treating it as a normal InputStream -
but it didn't make any difference. Here's what I did:

URL content =
servlet.getServletContext().getResource("/content/structure/scenario2resourc
es.txt");

InputStream is = (InputStream)content.getContent();

And here's the exception:

java.lang.NullPointerException^M
at
java.net.URLConnection.stripOffParameters(URLConnection.java:1011)^M
at
java.net.URLConnection.getContentHandler(URLConnection.java:979)^M
at java.net.URLConnection.getContent(URLConnection.java:554)^M
at java.net.URL.getContent(URL.java:807)^M
at Utility.writeFileToOutput(Utility.java:134)^M

You can see that it is definitely breaking somewhere in getContent().

If you have any other ideas, I'd appreciate hearing about them.

-john

Dimitri I. Rakitine

未讀,
2002年11月21日 下午3:19:022002/11/21
收件者:
It looks like URL returned by getResource(...) has null content-type for some
reason.

Does

InputStream in =
servlet.getServletContext().getResourceAsStream("/content/structure/a.txt");

work ?

> -john

--
Dimitri

John Hilgedick

未讀,
2002年11月25日 晚上9:36:252002/11/25
收件者:
That did the trick!

Muchas gracias!

-john

"Dimitri I. Rakitine" <dim...@dima.dhs.org> wrote in message
news:3ddd...@newsgroups.bea.com...

0 則新訊息