[英]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.