wood burning stoves 2.0*
The moose likes Servlets and the fly likes FileInputStream in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "FileInputStream in Servlets" Watch "FileInputStream in Servlets" New topic
Author

FileInputStream in Servlets

vivek parepalli
Greenhorn

Joined: Jul 16, 2004
Posts: 9
Hello,

I am trying to access a txt file from the service method of Servlet code. I am getting a Null Pointer Exception at this line

FileInputStream fis= new FileInputStream(source);

I have placed the source file in all possible locations. But still I am getting the same problem.

What would be the right location to place the source file. My servlet hierarchy looks like this:

+Servlet Folder
+WEB-INF
+CLASSES
-servlet.java&class
+LIB
-web.xml

Hope someone helps me out.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
FileInputStream fis= new FileInputStream(source);

I have placed the source file in all possible locations. But still I am getting the same problem.

The problem is not where the file is but the way you are referring to it. If you are not giving a complete path but using the "filename.typ" only, it will never work reliably. The reason being that with just the filename, the JVM assumes the "current" directory - something you have utterly no control over in the servlet environment.
Either code a complete path or use something like the ServletContext getRealPath method.
Bill


Java Resources at www.wbrogden.com
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Moreover, doing file I/O or database operations in servlet is not a good practice.

Thanks.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

If you have to read from a file, then you have to

Use the getResourceAsStream(String path) method of the ServletContext object. The path must begin with a "/" and is interpreted as relative to the current context root. For eaxmple if you have a file called 'test.txt' in the context root folder of your web-app,



cheers,
ram.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Moreover, doing file I/O or database operations in servlet is not a good practice.

Perhaps the intended statement was related to doing file I/O or database operations inside the servlet class code - for instance the service method. You will find it much easier to test and debug if such operations are in "helper" classes.
Bill

[ November 25, 2005: Message edited by: William Brogden ]
[ November 25, 2005: Message edited by: William Brogden ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileInputStream in Servlets
 
Similar Threads
Loading properties file into Servlet
try without catch
Importing digital certificate programmatically through jsp screen
Regarding importing pkcs12 file into keystore programmatically by uploading thru jsp
File name and extension