shell - Java执行Linux指令为文件赋予权限无效
伊谢尔伦
伊谢尔伦 2017-04-18 10:33:41
0
1
736
public class Demo {
    public static void main(String[] args) {
        String path = File.separator + "home" + File.separator + "admin" + File.separator + "zlib_decompress"; // zlib_decompress 路径
        String template = "chmod 777 %s";
        String command = String.format(template, path);
        String[] cmd = new String[] { "/bin/sh", "-c", "chmod 777 %s" };
        try {
            Runtime.getRuntime().exec(cmd); // chmod 777 /home/admin/zlib_decompress
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但直接执行chmod 777 /home/admin/zlib_decompress却可以,使用这种方式执行其他指令都可以但换成chmod就不行了是否涉及到其他权限问题呢?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全員に返信(1)
Ty80

String[] cmd = new String[] { "/bin/sh", "-c", "chmod 777 %s" };
上記のコマンド%sフォーマットされていないようですString[] cmd = new String[] { "/bin/sh", "-c", "chmod 777 %s" };
上面的命令%s好像没有被format

是不是应该修改成如下的方式
String[] cmd = new String[] { "/bin/sh", "-c", command };

次のように変更する必要があります
String[] cmd = new String[] { "/bin/sh", "-c", command };
🎜
いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!