【问题标题】:Java IOException: Permission denied on linux while running as ownerJava IOException:以所有者身份运行时在 Linux 上的权限被拒绝
【发布时间】:2012-07-29 01:41:42
【问题描述】:

我需要创建一些目录和文件,它们都应该具有 0600 权限。
当我从 NetBeans Debug 运行时: 创建目录后,当我尝试在那里存储一些文件时,我收到IOException 并显示“Permission Denied”消息,而目录和文件是使用相同的应用程序同时使用相同的用户创建的,所以我认为 0600(所有者读/写)应该工作。
并且在运行 Jar 文件时,chmod 根本不起作用! 我的代码是:

if(!Dest.exists()){
   boolean res=dirs.mkdirs();
   if(res){
      Runtime.getRuntime().exec("chmod -R 600 '"+dirs.getAbsolutePath()+"'");                
    }
}
File Destination=new File(Dest, source.getName());
documentManager.copyFile(source, Destination);

而copyFile是:

public static void copyFile(File sourceFile, File destFile) throws FileNotFoundException,IOException {
    if(!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    }
    finally {
        if(source != null) {
            source.close();
        }
        if(destination != null) {
            Runtime.getRuntime().exec("chmod 600 '"+destFile.getAbsolutePath()+"'");
            destination.close();
        }
    }
}

有什么问题?

谢谢

【问题讨论】:

  • 请发布您的堆栈跟踪。大概自己分析吧。异常是从哪里抛出的?

标签: java linux io file-permissions chmod


【解决方案1】:

该目录需要设置可执行位,以便您将文件写入其中。在目录上尝试 chmod +x。

mkdir tmp2323
chmod a-x tmp2323
touch tmp2323/test
touch: cannot touch `tmp2323/test': Permission denied

【讨论】:

  • 非常感谢;现在另一个问题:为什么当我从 Jar 文件运行应用程序时 chmod 不起作用? (ls -l 为所有创建的文件和目录提供“drwxrwxr-x”!)
  • 目录需要设置可执行位才能在目录上执行任何活动
【解决方案2】:

所有人:

dirs.setWritable(true, false);

仅限所有者:

dirs.setWritable(true, true);

dirs.setWritable(true);

【讨论】:

    【解决方案3】:

    如果您想在家中创建文件,只需运行chmod 777 /home。否则将/home 更改为不同的目录。然后您的简单代码将能够创建文件。

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多