简体   繁体   English

无法通过Java Web应用程序在Wildfly服务器中创建目录

[英]Can't create directory in the Wildfly server from the java web application

I want to create a directory structure inside the wild-fly server at the run time of my web java web application. 我想在我的Web Java Web应用程序运行时在Wild-fly服务器内部创建目录结构。 Here is the sample code inside my controller. 这是控制器内部的示例代码。

        String directoryName = null;
        try {
            directoryName = "/parent_folder/subfolder/";
            File directory = new File(directoryName);
            if (! directory.exists()){
                directory.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

This code not firing any exceptions. 此代码不会引发任何异常。 But it doesn't create the folders too. 但是它也不会创建文件夹。 I debugg with this code. 我调试此代码。 It shows code running successfully without having any exceptions. 它显示代码成功运行,没有任何异常。 Additionally my wild-fly server is running on a Ubuntu machine. 另外,我的服务器是在Ubuntu机器上运行的。


I changed my code as follows . 我如下更改了代码。 but the problem is same. 但是问题是一样的。

   String directoryName = null;
            try {
                directoryName = "/parent_folder/sub-folder/";
                String filePath = new File("").getAbsolutePath();
                filePath.concat(directoryName);
                File directory = new File(filePath);
                if (! directory.exists()){
                    directory.mkdirs();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

Have a look at the API docs for Java SE . 看看Java SE的API文档。 mkdirs returns a boolean if creation was successful. 如果创建成功,则mkdirs返回一个boolean

Hence, you must change it to: 因此,您必须将其更改为:

String directoryName = null;
            try {
                directoryName = "/parent_folder/sub-folder/";
                String filePath = new File("").getAbsolutePath();
                filePath.concat(directoryName);
                File directory = new File(filePath);
                if (! directory.exists()){
                    boolean created = directory.mkdirs();
                    if (!created) {
                      //TODO: add your code here
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在NetBeans中创建Java Web应用程序 - Can't create Java Web Application in NetBeans 如何将Java Web应用程序部署到远程Wildfly服务器? - How to deploy the java web-application to a remote wildfly server? 如何在不同 java 版本的 Wildfly 服务器上运行两个 web 应用程序? - How to run two web application on wildfly server with different java version? 将Web应用程序从JBoss 7.1.1 + Java 6迁移到Wildfly 9.0.2 + Java 8 - Migrating web application from JBoss 7.1.1 + Java 6 to Wildfly 9.0.2 + Java 8 Java Web应用程序无法连接远程Tomcat 8服务器上的数据库 - Java Web Application can't connect database on remote Tomcat 8 Server Wildfly服务器上的Java Spring项目无法访问RESTful服务 - Java Spring Project on a Wildfly Server can't access RESTful Services Java Web应用程序错误-找不到WEB-INF / classes目录中的类 - Java Web Application Error - Can't find classes that are located in WEB-INF/classes directory 如何为 java web 应用程序创建开发服务器 - How to create a development server for a java web application 无法在SD卡上创建应用程序目录 - Can't create application directory on SD card 无法在目录中创建文件 - Java - Can't create file in directory - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM