我想获取远程文件的创建日期,但我找不到提供此选项的方法。 我只能获得最后修改日期但我想获得此文件的创建日期:
这是我的代码:
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) {
}
}
}
谢谢
答案 0 :(得分:1)
SFTP standard不包括获取文件创建日期的方法。对不起,你运气不好。
答案 1 :(得分: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);
}
答案 2 :(得分:0)
通过sshChannel运行linux命令terraform plan -out samplefile.txt
解决了此问题。
首先是运行命令并返回系统输出的通用方法:
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);
}
}
它将返回以下格式的字符串:
2020-11-11 14:32:36.936232330 +0530
因此将其解析为某种数据类型非常容易。