The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

this returns me a location but I am being told I do not have permission to access this location in this manner. Instead, I am suppose to code it like this (or at least this is how I am understanding what they are telling me..

Code:

String filePath = "/" + "tmp" + "/" + saveFile;

The method they described to me does not seem like it will work because it is more of a relative path instead of providing the whole path.

My question to anyone who can help is, is there another way to provide the location to a directory instead of using getServletContext().getRealPath( "/" )? Is there a relative path way from a servlet?

Here is ultimately what I am trying to do.

Code:

// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream( getServletContext().getRealPath( "/" ) + "tmp" + "/" + saveFile );

My question to anyone who can help is, is there another way to provide the location to a directory instead of using getServletContext().getRealPath( "/" )?

Not that I know of.

However the way they suggested it (which is relative) will be relative from wherever the JVM process is called. So I'm not sure how the host has it set up but if it's a shared JVM I'm not really sure how that's supposed to help you. But in terms of testing you should try this.

Code:

File file = new File("");
System.out.println(file.getAbsolutePath());

Of course if you're using log4j or some other logging API that would be better, but basically you can use this to test where it would generate the file. You can also do like "file.canWrite()" to see if it's writable there.

Sorry for the long delay on my response. Not surprisingly, I had more server issues. I made the code change you advised with the new file to try and determine the path. I installed the code and had to wait until 1:00 am for a server restart. The change did not take effect. So I thought maybe I had done something wrong, so I made a 2nd change to verify. This did not work either. So for the 26th time, I called the help desk and they acknowledge that there was a server issue. I tell you this just to explain why there is a delay.

Would you have any idea why this code executes completely without throwing an exception but it does not write the file out? In the logs I see the 1 2 3 4 that I am logging.

I know there are open source ways to uploading files but I have been told countless times by the hosting company that they do things that are not supported in a shared hosting environment. That is why I am trying to code this without using them.