使用java将节目内容保存到该位置,以编程方式创建文件夹以及权限

use*_*234 3 java apache file-permissions mkdir

我在我的windows系统中安装了xampp,并在我的linux系统中安装了lampp.我想使用java在"http:// localhost /"位置创建文件夹.我做了以下事情:

dirName = new File("http://localhost/"+name);
if(!dirName.exists()) {
    dirName.mkdir();
}
Run Code Online (Sandbox Code Playgroud)

有可能吗?我的想法是以编程方式将一些文件下载到该位置.下载正在运行,但我如何创建文件夹,以便我可以通过它访问它http://example.com/name.这是跟踪用户相关内容所必需的.我可以访问已经安装了lampp的apache web服务器.如何创建文件夹并将下载保存到该文件夹​​,并以编程方式为文件夹及其中的内容分配权限,以便可以使用wget方法从那里下载保存的内容.

fge*_*fge 6

不要使用FileAPI.对于严肃的文件系统工作,它存在不当行为.

例如,如果目录创建失败,则该.mkdir()方法返回...一个布尔值!没有异常被抛出.

请改用文件.

例如,要创建目录:

// Throws exception on failure
Files.createDirectory(Paths.get("/the/path"), 
      PosixFilePermissions.asFileAttribute(      
         PosixFilePermissions.fromString("rwxr-x---")
      ));
Run Code Online (Sandbox Code Playgroud)


小智 5

使用带有 PosixPermission 的 Java 文件。 [注意- Windows 不支持 PosixPermission]

Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx");
Files.createDirectories(path, PosixFilePermissions.asFileAttribute(perms));
Run Code Online (Sandbox Code Playgroud)


tra*_*ega 2

在 Java 中,您可以通过执行以下操作在系统上的任何可写目录中创建文件:

File file1 = new File("/var/www/newDirectory/");
file1.mkdirs();
Run Code Online (Sandbox Code Playgroud)

然后要在该目录中创建文件,您可以执行以下操作:

File file2 = new File(file1.getAbsolutePath() + "newFile.txt"); // You may need to add a "File.seperator()" after the "file1.getAbsolutePath()" if the trailing "/" isn't included
if (file2.exists() == false) {
    file2.createNewFile();
}
Run Code Online (Sandbox Code Playgroud)

为了确保您的文件可供公众读取,您应该为该文件添加读取权限

file2.setReadable(true, false);
Run Code Online (Sandbox Code Playgroud)

在 Apache 中,您可以设置一个虚拟主机,该主机指向您想要从中提供文件的目录。在 debian linux 上默认是/var/www.