3

I am trying to create a folder within web server using Java File handling APIs in my RESTFul web service developed using JERSEY. According to my understanding, when I target "xyz.com" , it by default points out /home/xyz/public_html/ in my server.

So when I try to create a folder as follows

        String appFolderPath = "/xyz.com/appFolder/";
    File userNameFolder = new File(appFolderPath + userName);
    if (!userNameFolder.exists()) {
        folderPath = userNameFolder.mkdir();
    }

The above code fails, I am not getting any exception, and no folder is created. How exactly I suppose to do it ? How to give path for public_html/ folder ?

Another point is, is it not happening because of permission issue ? , I actually tried another way , I manually created /appFolder under public_html/ and give full read write permission to that folder, but still I couldn't create any folder within that using above code.

Please let me know how to achieve it ? Any Sample code ? Also if possible let me know if JERSEY does give me APIs to make it simple ?

Sadanand
  • 1,080
  • 3
  • 13
  • 30

2 Answers2

3

To point to public web directory use

ServletContext context = request.getServletContext();
String path = context.getRealPath("/");

and append your path to the path (above resolved)

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Since I am using jersey and developing web service, to get servlet context we have to add a parameter as follows http://stackoverflow.com/questions/11831042/restful-how-to-get-access-to-httpsession-inside-the-service-class – Sadanand Sep 29 '13 at 02:20
0

According to my understanding, when I target "xyz.com" , it by default points out /home/xyz/public_html/ in my server.

What makes you think so? You're operating files in the code snippet given, so your path means /xyz.com/appFolder/ and nothing more. And it's very likely you're facing a permission issue trying to create folders like this.

As for absense of exception, that's a design feature of Java file API not to throw exceptions on certain failures, but rather return erroneous error codes. Check what is returned by mkdir() call and you'll find false there, as a signal of failure.

Jk1
  • 11,233
  • 9
  • 54
  • 64
  • At least is it correct to refer a folder as "/xyz.com/appFolder/" ? – Sadanand Sep 22 '13 at 06:38
  • yes it is returning false for mkdir(). What might be the possible cause ? is it a permission issue? if that is the case to what folder I have to give read write access? definitely I can not give it for public_html folder. – Sadanand Sep 22 '13 at 06:51
  • First try to use 'mkdirs()' instead of 'mkdir()'. If it won't help, then you're facing a persimmion issue. To find out the folder to grant permission on, execute userNameFolder.getAbsolutePath(). – Jk1 Sep 22 '13 at 07:04
  • I used both way, using mkdirs() and using getAbsolutePath() , getAbsolutePath() returning "xyz.com/appFolder" which is expected. But still it is mkdirs() returning false. – Sadanand Sep 22 '13 at 08:01
  • I am not even getting securityException . – Sadanand Sep 22 '13 at 08:50
  • Resolved the issue : Instead of giving path as " /xyz.com/appFolder/ " , should give full path as "home/XYZ/public_html/appFolder" – Sadanand Sep 28 '13 at 07:24