transforming absolute path names to relative path names?

I need to create a file happy.txtin location D:/ProjFolder/SrcFolder/However i would like to use http://sitename/projectcontext/SrcFolder/ to create happy.txt I have a basic web application.This java piece is called by servlet.Thank you
[264 byte] By [techyless.techiea] at [2007-11-27 9:34:13]
# 1
I for one do not understand the question.What do you mean by "I want to use [some url]"? Use it for what, and opposed to what?
paulcwa at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 2

> I need to create a file happy.txtin location

> D:/ProjFolder/SrcFolder/

> However i would like to use

> http://sitename/projectcontext/SrcFolder/ to create

> happy.txt

Do you wish to copy the file happy.txt from http://sitename/projectcontext/SrcFolder/ to D:/ProjFolder/SrcFolder/ ?

Can you explain it in more detail.

jdolphina at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 3

I have a servlet that creates a file happy.txt in the file system.

File happy = new File("D:/projectFolder/SrcFolder/happy.txt")

Instead I would like

File happy = new File(http://blah.com/srcFolder/happy.txt");

This way when i send the war file to someone else, it doest say FolderNotFoundException or something like that.

techyless.techiea at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 4
Well, you wouldn't give a full URL with a host name. Think about it; if that worked, then they'd get error messages if they didn't own the hostname.Just use a relative pathname when you create the File object.
paulcwa at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 5
I dont think writing an absolute path is a good solution. You can try putting the file in the webapps directory, if possible in your application, and te directly give the file name only. This can be one of the solution.
jdolphina at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 6

use

getServletContext().getRealPath(String path)

or

getServletContext().getResource(String path)

or

getServletContext().getResourceAsStream(String path)//try this

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/GenericServlet.html#getServletContext()

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html

Hope That Helps

java_2006a at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 7

getServletContext().getRealPath(String path)

or

getServletContext().getResource(String path)

or

getServletContext().getResourceAsStream(String path)//try this

Im sorry im unable to get the path through these methods. The trouble is that getServletContext().getRealPath("/") gives me the location of jboss tmp/deploy directory.

techyless.techiea at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...
# 8

I just wrote a quick function today for transforming an absolutepath into a relative path (using Files). Don't know if this will be helpful, but here's the code

public static String getPathRelativeTo(String path, String relativeTo) {

if (!path.startsWith(relativeTo)) return null;

path = path.substring(relativeTo.length());

if (path.startsWith(File.separator)) path = path.substring(File.separator.length());

return path;

}

tjacobs01a at 2007-7-12 22:57:56 > top of Java-index,Java Essentials,New To Java...