我正在尝试将文件上传到服务器(使用java),其中我有一个帐户,并且SFTP连接停止工作(我想是超时问题)。我想知道有没有办法重置此连接并使其正常工作。这是功能:
public static void upload(File file, boolean retry) {
try
{
System.out.println("Uplodaing file " + file.getName());
JSch jsch = new JSch();
Session session = jsch.getSession("****", "*****", 22);
session.setPassword("*****");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(5);
System.out.println("Establishing connection");
session.connect(10);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
if (!retry)
sftpChannel.put(file.getAbsolutePath(), file.getName(),new SystemOutProgressMonitor(), ChannelSftp.OVERWRITE);
else
sftpChannel.put(file.getAbsolutePath(), file.getName(),new SystemOutProgressMonitor(), ChannelSftp.RESUME);
channel.disconnect();
session.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
upload(file, false);
}
}