Set Directory Permissions CHMOD with JSCH

前端 未结 4 1400
野性不改
野性不改 2021-01-13 05:24

In Unix, how do I set the directory permissions with JSCH? I am looking to do drwxrwxrwx. Filezilla says the integer for that is 775 but JSCH is not setting the permission

4条回答
  •  暖寄归人
    2021-01-13 06:06

    here is a short and a full example of how to can easyly use Jsch to change a chmod by using the usual way to decrib a CHMOD permission

    ========================================================= short answer : int chmodInt = Integer.parseInt(chmod, 8); channel.chmod(chmodInt, fileLinux);

    ========================================================= Full Example :

    package example;
    
    import java.io.IOException;
    import java.util.Date;
    
    import main.services.ServiceSSH;
    
    import org.junit.Test;
    
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    import com.jcraft.jsch.SftpException;
    
    public class ExampleChmod {
    
        @Test
        public void testchmod() throws JSchException, SftpException, IOException {
            Session session = ServiceSSH.getSession(); // Use your own session Factory
            Date dateStart = new Date();
            chmod("/home/user/launcher.sh", "777", session);
            Date dateEnd = new Date();
            session.disconnect();
            System.out.println(dateEnd.getTime() - dateStart.getTime() + "ms");
        }
    
        public static void chmod(String fileLinux, String chmod, Session session) throws JSchException, SftpException {
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            chmod(fileLinux, chmod, channel);
            channel.disconnect();
    
        }
    
        private static void chmod(String fileLinux, String chmod, ChannelSftp channel) throws SftpException {
            int chmodInt = Integer.parseInt(chmod, 8);
            channel.chmod(chmodInt, fileLinux);
        }
    }
    

提交回复
热议问题