Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   specifying path for file to be read by servlet with Tomcat (http://www.velocityreviews.com/forums/t131134-specifying-path-for-file-to-be-read-by-servlet-with-tomcat.html)

Yang Xiao 02-08-2004 12:41 AM

specifying path for file to be read by servlet with Tomcat
 
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

Chris Smith 02-08-2004 03:06 AM

Re: specifying path for file to be read by servlet with Tomcat
 
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

Andrew Thompson 02-08-2004 05:34 AM

Re: specifying path for file to be read by servlet with Tomcat
 
Yang Xiao wrote:
> Hi everybody,
> I'm trying to read a txt file to be used by a servlet


You might investigate..
request.getRealPath("Topics.txt");

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology



hiwa 02-08-2004 09:55 AM

Re: specifying path for file to be read by servlet with Tomcat
 
yang2002_99@yahoo.com (Yang Xiao) wrote in message news:<55c6e631.0402071641.34f00eb3@posting.google. com>...
> 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.

czahrobsky 05-30-2010 10:14 PM

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[i].toLowerCase().indexOf(fileName.toLowerCase()) >= 0) {
fullPath = folder + "/" + fileName;
return fullPath;
}
else if (children[i].indexOf("..") < 0) {
String subSearch = searchFolder(folder + "/" + children[i], fileName);
if (subSearch.toLowerCase().indexOf(fileName.toLowerC ase()) >= 0) {
return subSearch;
}
}
}
}
}
return "File not found!";
}
}

</code>


All times are GMT. The time now is 02:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.