0

I'm having trouble creating a folder relative to my webapp somewhere in myApp/WebContent/resources/ folder where to save data.

String folderPath="/WebContent/resources/"+title;

    File folder=new File(folderPath);
    if(!folder.exists())
        System.out.println("Folder created "+folder.mkdir());

the output is always false

dskfdskjgds
  • 341
  • 3
  • 14
  • Are you creating this path :- String folderPath="/WebContent/resources/"+title; – Amit Bhati Aug 25 '15 at 11:43
  • 1
    You should not trying to create such a folder because the servlet container is not required to put your web app in files. – Raedwald Aug 25 '15 at 11:46
  • What @Raedwald said. What will happen to this "data" folder when you deploy a new version of your application? – Steve C Aug 25 '15 at 11:51

1 Answers1

1

You can do something on below lines:-

 String folderPath= request.getServletContext().getRealPath("/");
  File file = new File (folderPath+"title");
  file.mkdir();

Above code will create a folder title within servletcontainer/webappContext.

As @Raedwald has mentioned its not a good practise to create any folder in webappContext.

Refer this link: Best practice to store temporary data for a webapp

Community
  • 1
  • 1
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • so is this a good way to do it ? if i would move my project on a server will this work ? – dskfdskjgds Aug 25 '15 at 11:45
  • Yes it will always work, because its using the relative path. You are not hard-coding anything. – Amit Bhati Aug 25 '15 at 11:47
  • ok. but i don't want to store temporary information. let's say i want to make a photo gallery which provides the user the right to upload pictures which may be later loaded on the website and seen by anyone. is it better to store them in database as bytes ? or just move all my resources outside web content ? – dskfdskjgds Aug 25 '15 at 12:00