Home > Back-end >  JSCH Library is not giving file timestamp with milli seconds
JSCH Library is not giving file timestamp with milli seconds

Time:12-23

How to get remote file timestamp including milliseconds using jsch framework. I tried as below but till seconds only I get.

ChannelSftp channelSftp = (ChannelSftp) jschSession.openChannel("sftp");
channelSftp.connect();
List<LsEntry> files = channelSftp.ls("*.log");
for (LsEntry entry : files) {
            log.info("File ", entry.getFilename() ":" entry.getAttrs().getMtimeString());
}

Output:

File app1.log: Fri Dec 23 12:32:52 IST 2022
File app2.log: Fri Dec 23 12:32:52 IST 2022

Expected Output: with timestamp

File app1.log: Fri Dec 23 12:32:52.1234556789 IST 2022
File app2.log: Fri Dec 23 12:32:52.3334556789 IST 2022

Please help to get milliseconds too.

CodePudding user response:

The SFTP protocol can provide milliseconds only since version 4. The JSch supports only SFTP version 3. Even if JSch did support newer version of SFTP, it won't help you probably, as most SFTP servers run on OpenSSH, which supports SFTP 3 only too. So you cannot have really "expected" to get the milliseconds.


If you need milliseconds, you will have to use another library (and mostly likely another server), or completely different API, not SFTP.

If you have a shell access, you can run an appropriate shell command and parse its output. For example:

ls --full-time *.log

CodePudding user response:

To get the timestamp of a file with milliseconds precision, you will need to use a different approach. One option is to execute a command on the remote server that returns the timestamp of the file with milliseconds precision. For example, you could use the stat command, which is available on most Unix-like systems, to get the timestamp of a file with the -c flag:

stat -c "%y" /path/to/file

This command will return the timestamp of the file in the format YYYY-MM-DD HH:MM:SS.mmm, where mmm is the milliseconds part of the timestamp.

To execute this command using the JSch library, you can use the ChannelExec class to create an exec channel and execute the command. Here's an example of how you could do this:

import com.jcraft.jsch.*;

public class TimestampExample {
    public static void main(String[] args) {
        try {
            // Set up JSch and establish a connection
            JSch jsch = new JSch();
            Session session = jsch.getSession("user", "host", 22);
            session.setPassword("password");
            session.connect();

            // Create an exec channel and execute the command
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand("stat -c \"%y\" /path/to/file");
            channel.connect();

            // Read the output of the command
            BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            String line = reader.readLine();
            System.out.println(line);

            // Close the channel and session
            channel.disconnect();
            session.disconnect();
        } catch (JSchException | IOException e) {
            e.printStackTrace();
        }
    }
}

This example establishes a connection to the remote server, creates an exec channel, executes the stat command, and reads the output of the command. The output of the command, which is the timestamp of the file with milliseconds precision, is then printed to the console.

  • Related