我的文件传输到SFTP服务器失败,异常:java.io.IOException:inputstream已关闭
我试图检查过去几天图书馆是否有任何更改,但没有,我也使用最新版本:jsch-0.1.54.jar
这是我的代码:
public void sendVsbFile(String fileName, MyObject c) {
File f = new File(fileName);
if (f.exists()) {
String SFTPHOST = c.SFTPURL;
int SFTPPORT = Integer.parseInt(c.SFTPPORT);
String SFTPUSER = c.SFTPUserName;
String SFTPPASS = c.SFTPPassword;
String SFTPWORKINGDIR = c.FTPFolderLocation;
Session session;
Channel channel;
ChannelSftp channelSftp;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("TCPKeepAlive", "yes");
session.setConfig(config);
session.setServerAliveInterval(120 * 1000);
session.setServerAliveCountMax(1000);
session.setTimeout(timeout);
session.connect();
System.out.println("Host connected.");
channel = session.openChannel("sftp");
channelSftp = (ChannelSftp) channel;
channelSftp.connect();
System.out.println("sftp channel opened and connected.");
channelSftp.cd(SFTPWORKINGDIR);
channelSftp.cd(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyLocalAndroidPath/");
String suffix = ".filepart";
Log.d(TAG, "file name = " + f.getName());
String tempFileName = f.getName() + suffix;
channelSftp.put(new FileInputStream(f), tempFileName, ChannelSftp.RESUME);
String newFileName = tempFileName.substring(0, tempFileName.length() - suffix.length());
channelSftp.rename(tempFileName, newFileName);
System.out.println("File transferred successfully to host.");
} catch (JSchException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
许多开发人员已经发布了这类问题,但没有提供具体的解决方案。如果有人曾经遇到过这个问题,请帮忙。感谢。
这是堆栈跟踪:
java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:697)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
at myMethod(MyClass.java:933)
at myMethod.run(MyClass.java:804)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2911)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2935)
at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:2473)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:686)
... 5 more