0

getServletContext().getResourceAsStream()

asked 2006-08-08 14:47:07 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3857986

By: davetam

Hello,

In the java code I need to read in a file as stream.
In the past we can do it like this:

void initialiseSQLStatement(String resourcePath){
try{
log.debug("Trying to open " + resourcePath);
InputStream iStream
= servlet.getServletContext().getResourceAsStream(resourcePath);
int c;
StringBuffer sbSQLString = new StringBuffer();
while ((c = iStream.read()) != -1){
sbSQLString.append((char) c);
}
log.debug("Script = " + sbSQLString);
SQLStatement = sbSQLString.toString();
}catch (IOException e){

}
}


However I dont seem to see a way to do this now as I do not seem to be able to get the Servlet anymore or is there other way you could suggest?

Thanks in advance.

Dave

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2006-08-08 15:13:19 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=3858034

By: davetam

Hi guys I have found it. It is:

InputStream iStream = desktop.getWebApp().getResourceAsStream(resourcePath);


Thanks anyway and zk is brilliant!

link publish delete flag offensive edit

answered 2007-05-23 09:59:49 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4326277

By: nobody1

How can i convert a String to a resourcePath, that i get my InputStream how it's in this post?
http://sourceforge.net/forum/message.php?msg_id=3858034

Example is c:\test.txt
then convert it and
InputStream iStream = desktop.getWebApp().getResourceAsStream(resourcePath);
that would solve my upload problem too.

Thanks
nobody1


link publish delete flag offensive edit

answered 2007-05-23 10:32:26 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4326319

By: zanyking


ServletContext will store in WebApp and you can get all its resources use:

Map attributes = desktop.getWebApp().getAttributes();

and this attributes Object is actually ServletContext's proxy, so any operations of it will reflect to ServletContext .


link publish delete flag offensive edit

answered 2007-05-23 11:42:49 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4326400

By: nobody1

Thanks,
i have taken desktop.getWebApp().getNativeContext() and Mapped it to a ServletContext.
But now i want to have an inputStream with my given Value: c:\test.txt or something else.

ServletContext ctx = (ServletContext) desktop.getWebApp().getNativeContext();
inputStream is = ctx.getResourceAsStream(resourcePath); // Here is my Problem how get Stream from c:\test.txt ?????

How i get the resourcePath????

nobody1

link publish delete flag offensive edit

answered 2007-05-24 01:45:39 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4327775

By: zanyking


ServletContext is a "Servlet" context.

The resource you want to get from ServletContext must have some relations with your web application in the document base. Take a look for ServletContext API
detail:

http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.ht
ml#getResource(java.lang.String)

So, If there's any resource you want to get form server's OS file system. you should write Code Like this:

ServletContext ctx = (ServletContext) desktop.getWebApp().getNativeContext();
String nativeOsFilePath = ctx.getAttribute("systemFilePath");
InputStream fIo = new FileInputStream(fIo);

But this is not recommaded in an web application design, you broken the abstraction over URL.



link publish delete flag offensive edit

answered 2007-05-24 05:43:32 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4327926

By: nobody1

I've looked yesterday for the API, but i allways get a "null Exception" and the file isn't at Server System, it's on the Client and i want to upload it whith help of the Context and the given path, but it has to transform.
The idea is, that i don't need any form-Tag in HTML and/or the encode Type for file.
So i make textbox with ZK-Framework and there is the typ="file" the value of it is the a String which represent the File to Upload from the lokal File-System.

Thanks
nobody1


link publish delete flag offensive edit

answered 2007-05-25 09:57:38 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4330094

By: zanyking



For modern OS and Http Protocol design, there's no "normal & Legal" way for any server to access user's local file system like this without installation & permition. Because It's too dangerous.



link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2006-08-08 14:47:07 +0800

Seen: 1,424 times

Last updated: May 25 '07

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More