Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > specifying path for file to be read by servlet with Tomcat

Reply
Thread Tools

specifying path for file to be read by servlet with Tomcat

 
 
Yang Xiao
Guest
Posts: n/a
 
      02-08-2004
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
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      02-08-2004
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      02-08-2004
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


 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      02-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Yang Xiao) wrote in message news:<(E-Mail Removed). 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.
 
Reply With Quote
 
czahrobsky czahrobsky is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-30-2010
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tomcat ssl servlet to servlet, private CA edavid3001@gmail.com Java 0 10-06-2005 09:03 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat returning 404 error when using additional path (/servlet/myServlet/extra/path/info) Mupota Muchelemba Java 1 02-04-2004 12:43 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM



Advertisments