Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a folder within web server under /public_html/ in Java

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 ?

like image 421
Sadanand Avatar asked Mar 17 '26 03:03

Sadanand


1 Answers

To point to public web directory use

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

and append your path to the path (above resolved)

like image 195
jmj Avatar answered Mar 19 '26 23:03

jmj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!