Results 1 to 3 of 3
- 01-06-2012, 02:43 PM #1
Member
- Join Date
- Dec 2011
- Location
- Brazil
- Posts
- 26
- Rep Power
- 0
Reading file with FileInputStream (Servlet) - How to use relative pathing?
Hello!
The idea:
Basically i'm trying to read a file and print it on the browser.
The problem:
I got it working but i have a problem that the file path being used is the full path on my computer (D:/Sites/GamersLust/web/html/index.html) and i don't think that's gonna sit well on a live host. I'm using the function from the file located at D:/Sites/GamersLust/web/index.jsp, which is the website's root.
Am i using the wrong method here to do this? I've tried setting the path to:
html/index.html
/html/index.html
./html/index.html
../html/index.html
The code:
Any ideas and comments are appreciated!Java Code:public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { // output an HTML page res.setContentType("text/html"); // print some html PrintWriter out = res.getWriter(); // print the file InputStream in = null; try { in = new BufferedInputStream (new FileInputStream("D:/Sites/GamersLust/web/html/index.html") ); int ch; while ((ch = in.read()) !=-1) { out.print((char)ch); } } finally { if (in != null) in.close(); // very important } }
Thanks in advance!
- 01-06-2012, 03:42 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Reading file with FileInputStream (Servlet) - How to use relative pathing?
Do what I do (as I can never remember the path).
That'll tell you pretty quickly what your runtime directory is.Java Code:File f = new File("dummy"); log.debug("file is at " + f.getAbsolutePath()); // Or whatever logging system you have
- 01-06-2012, 04:23 PM #3
Member
- Join Date
- Dec 2011
- Location
- Brazil
- Posts
- 26
- Rep Power
- 0
Re: Reading file with FileInputStream (Servlet) - How to use relative pathing?
Hmmmm, that returned:
file is at C:\Program Files\Apache Software Foundation\Tomcat 7.0\dummy
It's where my server is installed at... Is there a way to get the filepath of the file that's executing the command?
Thanks!
EDIT: Got it, here it is:
Thanks! Solved!Java Code:String path = req.getSession().getServletContext().getRealPath("/");Last edited by KingdomX; 01-06-2012 at 04:38 PM.
Similar Threads
-
Help needed to load relative path of file
By friendz_forever in forum New To JavaReplies: 8Last Post: 08-15-2011, 10:03 AM -
FileNotFoundException, FileInputStream, File
By horndinkle in forum New To JavaReplies: 3Last Post: 06-10-2011, 04:21 PM -
FileInputStream with relative path in a JAR
By Bertieboy7 in forum New To JavaReplies: 3Last Post: 03-31-2011, 04:10 AM -
How to use a relative path for a file?
By kipade in forum SWT / JFaceReplies: 0Last Post: 02-28-2011, 06:37 AM -
Reading file from a class that is called by a servlet
By bikashg in forum New To JavaReplies: 1Last Post: 12-11-2010, 08:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks