specifying path for file to be read by servlet with Tomcat

Discussion in 'Java' started by Yang Xiao, Feb 8, 2004.

  1. Yang Xiao

    Yang Xiao Guest

    Hi everybody,
    I'm trying to read a txt file to be used by a servlet to display
    posting topics, I'm running Tomcat 5.0 on Win2k.

    (new FileInputStream("C:/JAVA/Topics.txt"));

    This seems to work fine, but not portable, but none of the following
    attempts to use a relative path works,
    (new FileInputStream("Topics.txt")) -- classes/Topics.txt
    (new FileInputStream "../Topics.txt")); -- classes/Topics.txt
    (new FileInputStream "/Topics.txt")); -- ROOT/Topics.txt
    (new FileInputStream "http://localhost/Topics.txt")); --
    ROOT/Topics.txt

    Please help!

    Thanks,
    Yang
     
    Yang Xiao, Feb 8, 2004
    #1
    1. Advertising

  2. Yang Xiao

    Chris Smith Guest

    Yang Xiao wrote:
    > (new FileInputStream("C:/JAVA/Topics.txt"));
    >
    > This seems to work fine, but not portable, but none of the following
    > attempts to use a relative path works,


    See comments on each of your approaches, plus my general comments on
    solving your problem at the end.

    > (new FileInputStream("Topics.txt")) -- classes/Topics.txt


    That name is interpreted by the VM as relative to the current working
    directory. The current working directory is undefined for the servlet
    execution environment, so you should not rely on it. Generally
    speaking, the working directory is often the bin directory for the
    servlet container. It's very rarely set to the classes directory of
    your web app. Such a thing is not required of the servlet container
    implementation, is a good bit of trouble (and in fact impossible for a
    pure Java implementation), and it would encourage non-portable
    programming anyway.

    > (new FileInputStream "../Topics.txt")); -- classes/Topics.txt


    Some thing as above.

    > (new FileInputStream "/Topics.txt")); -- ROOT/Topics.txt


    The servet runs on the server, and files are resolved on the server's
    local filesystem. While your webapp root may be presented as a root
    directory by FTP, it's NOT the root of your server's local filesystem.
    So that won't work.

    > (new FileInputStream "http://localhost/Topics.txt")); --
    > ROOT/Topics.txt


    FileInputStream is used to access files from a filesystem. Java is
    capable of reading from an HTTP URL, but you'll need to do it with
    java.net.URL and java.net.URLConnection. So this is a valid approach,
    but you're trying to use the wrong classes to accomplish it.

    However, the easier method is probably to use ServletContext.getResource
    to get a reference to the data packaged in your web app. Since
    getResource does take a context-relative URL, you can use
    getResource("/Topics.txt") and trust it to work, regardless of the
    location of your web application on the server's local filesystem OR the
    path it's mapped to via the servlet container. That should work well.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 8, 2004
    #2
    1. Advertising

  3. Andrew Thompson, Feb 8, 2004
    #3
  4. Yang Xiao

    hiwa Guest

    (Yang Xiao) wrote in message news:<>...
    > Hi everybody,
    > I'm trying to read a txt file to be used by a servlet to display
    > posting topics, I'm running Tomcat 5.0 on Win2k.
    >
    > (new FileInputStream("C:/JAVA/Topics.txt"));
    >
    > This seems to work fine, but not portable, but none of the following
    > attempts to use a relative path works,
    > (new FileInputStream("Topics.txt")) -- classes/Topics.txt
    > (new FileInputStream "../Topics.txt")); -- classes/Topics.txt
    > (new FileInputStream "/Topics.txt")); -- ROOT/Topics.txt
    > (new FileInputStream "http://localhost/Topics.txt")); --
    > ROOT/Topics.txt
    >
    > Please help!
    >
    > Thanks,
    > Yang


    FileInputStream needs parameters based on OS file system. If you want
    to stick to Web-app/Web-server vocabulary, get the stream from URL.
     
    hiwa, Feb 8, 2004
    #4
  5. Yang Xiao

    czahrobsky

    Joined:
    May 30, 2010
    Messages:
    1
    If you don't want to mess with resources, you can let your app search for the file:

    <code>
    package utils;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.File;

    public class PartLoader {

    public static String Read(String filePath) throws java.io.IOException {
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;
    try {
    String foundFile = FindFile(filePath);
    File file = new File(foundFile);
    reader = new BufferedReader(new FileReader(file));
    String text = null;
    // repeat until all lines is read
    while ((text = reader.readLine()) != null) {
    contents.append(text).append("\n"); }
    } catch (Exception e) {
    contents.append(e.getMessage());
    } finally {
    try {
    if (reader != null) {
    reader.close(); }
    } catch (Exception e) {
    contents.append(e.getMessage());
    e.printStackTrace();
    }
    }
    return contents.toString();
    }

    public static String FindFile(String fileName)
    {
    String path = searchFolder(".", fileName);
    return path;
    }

    private static String searchFolder(String folder, String fileName)
    {
    String fullPath = "";
    File dir = new File(folder);
    if (dir.isDirectory())
    {
    String[] children = dir.list();
    if (children != null) {
    for (int i=0; i<children.length; i++) {
    // Get filename of file or directory
    if (children.toLowerCase().indexOf(fileName.toLowerCase()) >= 0) {
    fullPath = folder + "/" + fileName;
    return fullPath;
    }
    else if (children.indexOf("..") < 0) {
    String subSearch = searchFolder(folder + "/" + children, fileName);
    if (subSearch.toLowerCase().indexOf(fileName.toLowerCase()) >= 0) {
    return subSearch;
    }
    }
    }
    }
    }
    return "File not found!";
    }
    }

    </code>
     
    czahrobsky, May 30, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Clarke
    Replies:
    1
    Views:
    1,922
    Sudsy
    Jan 7, 2004
  2. Mupota Muchelemba
    Replies:
    1
    Views:
    933
    Tony Morris
    Feb 4, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,045
    Jack Jia
    Apr 4, 2004
  4. Doug McCann
    Replies:
    1
    Views:
    1,079
    William Brogden
    Aug 5, 2004
  5. Replies:
    0
    Views:
    868
Loading...

Share This Page