【发布时间】:2015-07-17 06:49:04
【问题描述】:
我正在使用 Jsch 和 Sftp 协议在一个简单的 Java 应用程序中将文件从本地计算机上传到远程服务器。
我的代码没有出现错误或异常,并且运行成功,但是当我查看远程位置时,文件被上传为 0kb,没有扩展名并命名为“D”。
我尝试了很多方法,但我无法找出错误。
这是我的代码..
String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\";
String localDirectory = "C:\\pdffiles\\";
String fileToFTP = "demo.pdf";
String SFTPHOST = "hostIPaddress";
int SFTPPORT = 22;
String SFTPUSER = "username";
String SFTPPASS = "password";
String local_filename = fileToFTP;
String local_pathname = localDirectory;
String remote_filename = fileToFTP;
JSch jsch = null;
Session session = null;
ChannelSftp sftpChannel = null;
try
{
jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST);
session.setPassword(SFTPPASS);
session.setPort(SFTPPORT);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
session.setConfig("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
session.connect();
sftpChannel = (ChannelSftp)session.openChannel("sftp");
sftpChannel.connect();
System.out.println(sftpChannel);
System.out.println(sftpChannel.getSession().isConnected());
FileInputStream fileBodyIns = new FileInputStream(local_pathname + local_filename);
System.out.println(local_pathname + local_filename);
System.out.println(remoteDirectory + remote_filename);
sftpChannel.put(fileBodyIns, remoteDirectory + remote_filename);
fileBodyIns.close();
sftpChannel.exit();
session.disconnect();
System.out.println("File uploaded successfully");
return "File uploaded successfully";
}
catch (Exception e)
{
e.printStackTrace();
return e.getMessage();
}
我的连接成功,线路
System.out.println(sftpChannel.getSession().isConnected());
给出真实
下面一行打印成功
System.out.println("File uploaded successfully");
【问题讨论】:
-
您可以在 put 操作中包含一个监视器来调试此问题。
public void put(InputStream src, String dst, SftpProgressMonitor monitor) throws SftpException见:epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/…
标签: java file upload sftp jsch