【问题标题】:JAVA: get the creation date of a remote file via sftpJAVA:通过 sftp 获取远程文件的创建日期
【发布时间】:2020-11-11 17:53:27
【问题描述】:

我想获取远程文件的创建日期,但我没有找到提供此选项的方法。 我只能得到最后修改日期,但我想得到这个文件的创建日期:

这是我的代码:

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class mytest {


    public static void main(String args[]) throws ParseException {

        //===============================================================
        String hostname = "10.10.11.19";
        String username = "root";
        String password = "passwword";
        String remoteFile = "/opt/test_pyt/teeeeeeest.txt"
        String copyTo = "/home/desk/Desktop";
        JSch jsch = new JSch();
        Session session = null;
        System.out.println("Trying to connect.....");
        try {
            session = jsch.getSession(username, hostname, 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftpChannel = (ChannelSftp) channel;
            //get date of file=========================================   
            String lastModif = sftpChannel.lstat(remoteFile).getMtimeString();

            //I want to get creation date =============================
            creation_date= ???????????;

          //==============================================
                sftpChannel.get(remoteFile, copyTo);
                sftpChannel.exit();
                session.disconnect();


        } catch (JSchException e) {
        } catch (SftpException e) {
        }

    }
}

谢谢

【问题讨论】:

  • 并非所有文件系统都会记录文件的创建时间。可能没有价值。

标签: java jsch


【解决方案1】:

是的,你可以用 jsch 做到这一点:

Vector vec = channel.ls("File Name");
// Assumption only no duplicate file names on server
if (vec != null && vec.size() == 1) {
        LsEntry details = (LsEntry) vec.get(0);
        SftpATTRS attrs = details.getAttrs();

        int t = attrs.getMTime();
        java.utl.Date modTime = new Date(t * 1000L);

}

【讨论】:

  • 不适用于windos ssh
【解决方案2】:

SFTP standard 不包含获取文件创建日期的方法。你运气不好,对不起。

【讨论】:

  • 可能启动一个shell,ls -l文件并将输出读入日期。
  • @PeterMmm Unix 文件系统通常不会记录文件创建时间。 mtime 是文件内容最后一次改变的时间,ctime 是inode 数据最后一次改变的时间。
【解决方案3】:

通过 sshChannel 运行 linux 命令stat --printf="%y\n" ${file} 解决了这个问题。

第一个运行命令并返回系统输出的通用方法:

String runCommand(String command) {
        Session session = connect(details); // change to your connection here
        ChannelExec execChannel = null;
        try {
            execChannel = (ChannelExec) session.openChannel("exec");
            execChannel.setCommand(command);
            InputStreamReader stream = new InputStreamReader( execChannel.getInputStream() );
            execChannel.setErrStream( System.err, true );
            execChannel.connect();
            StringBuilder output = new StringBuilder();
            char[] buffer = new char[128];
            int read;
            while ( ( read = stream.read( buffer, 0, buffer.length ) ) >= 0 ) {
                output.append( buffer, 0, read );
            }
            stream.close();
            new Await()
                    .waitUntil(execChannel, Channel::isClosed, 10000); //it's my implementation; you can just wait in loop until it's closed
            execChannel.disconnect();
            return output.toString();
        } catch (JSchException | IOException e) {
            handleException(e);
            return "Command execution failed";
        } finally {
            cleanup(execChannel, session);
        }
    }

然后:

sshClient.runCommand("stat --printf=\"%y\\n\" /path/to/your/file-or-folder")

它将返回以下格式的字符串:

2020-11-11 14:32:36.936232330 +0530

因此很容易将其解析为某种数据类型。

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多