specifying path for file to be read by servlet with Tomcat

Y

Yang Xiao

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
 
C

Chris Smith

Yang said:
(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
 
H

hiwa

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.
 
Joined
May 30, 2010
Messages
1
Reaction score
0
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top