0

既存のファイルのパーミッションを変更するのが困難です。私がやりたいことは、xml をマーシャリングしてファイルに書き込むことです。アプリは「root」アカウントで実行されているため、ファイルは root によって所有されます。このファイルの権限を変更して、他のユーザーがこのファイルを SFTP できるようにします。

これは私の最初のコードです:

         File file = new File(Constant.fileName);
         context = JAXBContext.newInstance(RateGroups.class);
         Marshaller marshaller = context.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
         marshaller.marshal(rateGroups, file);
         marshaller.marshal(rateGroups,System.out);

上記のコードを実行すると、ファイルは正常に書き込まれますが、他のアカウントを使用して SFTP を実行できません (許可が拒否されました)。次に、このコードを追加して、ファイルのアクセス許可を変更します。

         file.setWritable(true,false);
         file.setExecutable(true,false);
         file.setReadable(true,false);

パーミッションは変更しましたが、ファイルサイズもゼロ(0)になりました。許可を変更する方法、または可能であれば所有者を変更する方法を教えてください。ありがとうございました。


更新 1

ファイルを書き込んだ後、sftp クラスを呼び出します。

        SFTPFile sftpFile = new SFTPFile();
        error = sftpFile.execute(Constant.rateGroupxml);

これは SFTPFile クラスのコードです:

    public int execute(String fileName){
    JSch jsch = new JSch();
    Session session = null;
    int result = 0;
    String localPath = Constant.path + "/report/" + fileName;
    //try {

    try {
        session = jsch.getSession(Constant.user1, Constant.IP1, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(Constant.pass1);
        session.connect();

        System.out.println("session: " + session.getUserName());
        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.cd(Constant.pathDestination);
        sftpChannel.put(localPath);
        sftpChannel.exit();
        session.disconnect();
        result = 0;
    } catch (SftpException ex) {
        Logger.getLogger(SFTPFile.class.getName()).log(Level.SEVERE, null, ex);
        result = 1;
    } catch (JSchException ex) {
        Logger.getLogger(SFTPFile.class.getName()).log(Level.SEVERE, null, ex);
        result = 2;
    }
    return result;
4

0 に答える 0