Results 1 to 3 of 3
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question 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:
    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
            }
        }
    Any ideas and comments are appreciated!

    Thanks in advance!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,854
    Rep Power
    19

    Default Re: Reading file with FileInputStream (Servlet) - How to use relative pathing?

    Do what I do (as I can never remember the path).
    Java Code:
    File f = new File("dummy");
    log.debug("file is at " + f.getAbsolutePath());  // Or whatever logging system you have
    That'll tell you pretty quickly what your runtime directory is.

  3. #3
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default 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:
    Java Code:
            String path = req.getSession().getServletContext().getRealPath("/");
    Thanks! Solved!
    Last edited by KingdomX; 01-06-2012 at 04:38 PM.

Similar Threads

  1. Help needed to load relative path of file
    By friendz_forever in forum New To Java
    Replies: 8
    Last Post: 08-15-2011, 10:03 AM
  2. FileNotFoundException, FileInputStream, File
    By horndinkle in forum New To Java
    Replies: 3
    Last Post: 06-10-2011, 04:21 PM
  3. FileInputStream with relative path in a JAR
    By Bertieboy7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2011, 04:10 AM
  4. How to use a relative path for a file?
    By kipade in forum SWT / JFace
    Replies: 0
    Last Post: 02-28-2011, 06:37 AM
  5. Replies: 1
    Last Post: 12-11-2010, 08:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •