JAVA:通过sftp获取远程文件的创建日期

问题描述 投票:2回答:2

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

这是我的代码:

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
2个回答
1
投票

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


0
投票

是的你可以用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);

}
© www.soinside.com 2019 - 2024. All rights reserved.