在Spring webmvc应用程序中获取WebContent的相对路径

时间:2015-08-25 11:40:01

标签: java spring file java-ee resources

我无法在myApp / WebContent / resources /文件夹中的某处创建相对于我的webapp的文件夹,以便保存数据。

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

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

输出始终为false

1 个答案:

答案 0 :(得分:1)

您可以在以下行中执行操作: -

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

上面的代码将在servletcontainer / webappContext中创建一个文件夹标题。

正如@Raedwald所说,在webappContext中创建任何文件夹并不是一个好习惯。

请参阅此链接:Best practice to store temporary data for a webapp

相关问题