This content has been marked as final. Show 10 replies
A quote from the API doc
The path must begin with a "/" and is interpreted as relative to the current context root.You can't use that method to get a file, unless that file exists under the context root of the servlet, use a normal FileInputStream and replace "\" with either "/" or "\\".
Edit: Or better yet, place it under your context root (if you can), then you can use that method and you only need to reference the portion of the path inside the context.
the problem again
I have web application user can upload images from their computer
in my web application I want to write this file into a tem folder in my application
for that I use uploadservlet
in the servlet I become the path of the uploaded image
to write it in the temp folder
example user did upload the image
String path="C:\\Documents and Settings\\images\\myImage.jpg"; InputStream ins = this.getClass().getResourceAsStream(path); but ins = null; InputStream ins = this.getClass().getResourceAsStream("/"+myImage.jpg); but ins = null; what shoud be the problrm
can only be used for something in the "context root" (when accessed through a ServletContext) or something in the classpath when accessed through Class.
To open a file for reading that is not located in one of these two places do
As I said in my first post.
InputStream ins = new FileInputStream(path);
Edit: also, if this is for a file upload, your servlet will not be doing any kind of reading from the file system to begin with. You should be receiving your input from the request. Take look at the following document: