3

任何人都可以提出任何想法如何从代码中创建 java web 应用程序的项目结构中的文件夹吗?

4

3 回答 3

2

您正在谈论在 servletcontainer/webappContext.. 中创建一个文件夹。这是如何做到的:

public void doPost(HttpServletRequest req,HttpServletResponse resp){
      String path = req.getServletContext().getRealPath("/");
      File f = new File (path +"myNewFolder");
      f.mkdir();

}
于 2013-10-24T08:37:22.977 回答
1

发现这个:

File f = new File("C:\\Test");
try{
  if(f.mkdir()) { 
    System.out.println("Directory Created");
  } else {
      System.out.println("Directory is not created");
  }
 } catch(Exception e){
  e.printStackTrace();
}

如何在 Java 中创建文件夹?

希望能帮助到你

于 2013-10-24T07:35:02.613 回答
0

你可以这样做:

    String absolute = getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm();
    absolute = absolute.substring(0, absolute.length()-1);
    absolute = absolute.substring(0, absolute.lastIndexOf("/")+1);
    String xmlPath = absolute + "webapp/xml/";       
    String os = System.getProperty("os.name");
    if(os.indexOf("Windows") != -1) {
        xmlPath = xmlPath.replace("/", "\\\\");
        if(xmlPath.indexOf("file:\\\\") != -1) {
        xmlPath = xmlPath.replace("file:\\\\", "");
        }
    } else if(xmlPath.indexOf("file:") != -1) {
        xmlPath = xmlPath.replace("file:", "");
    }

    File f = new File(xmlPath);
    f.mkdir();
于 2013-10-24T08:43:31.933 回答