[英]file uploading successfully, but 0kb file is uploading on remote server using jsch sftp java
I am uploading a file from my local computer to remote server in a simple java application using Jsch, and Sftp protocol. 我正在使用Jsch和Sftp协议在一个简单的Java应用程序中将文件从本地计算机上传到远程服务器。
My code is getting no error or no exception and it runs successfully, but when I look at the remote location, the file is uploaded as 0kb with no extension and named as 'D'. 我的代码没有错误也没有异常,并且可以成功运行,但是当我查看远程位置时,文件以0kb的格式上传,没有扩展名,并且名为“ D”。
I have tried many ways but I am not able to figure out the mistake. 我已经尝试了很多方法,但是我无法弄清错误。
Here is my code.. 这是我的代码。
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();
}
My connection makes successfully, the line 我的连接成功,线路
System.out.println(sftpChannel.getSession().isConnected());
gives true 给真
and the following line prints successfully 并且以下行成功打印
System.out.println("File uploaded successfully");
String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\";
The SFTP protocol uses a unix-like naming scheme for file pathnames on the remote server. SFTP协议对远程服务器上的文件路径名使用类unix的命名方案。 The file separator is "/", and a pathname without a leading "/" is considered to be relative to the current directory.
文件分隔符为“ /”,并且不带前导“ /”的路径名被认为是相对于当前目录的。 Colons and backslashes have no special meaning.
冒号和反斜杠没有特殊含义。
By those rules, you're asking the remote server to create a file in your home directory named "D:\\DataStores..." with a literal colon and backslashes. 根据这些规则,您要求远程服务器在主目录中创建一个名为“ D:\\ DataStores ...”的文件,并使用文字冒号和反斜杠。 If your remote server is the Cygwin OpenSSH server or some other port of a Unix-based server, there may be a mismatch between the way the SFTP server interprets the filename and the way the Windows OS interprets the filename.
如果您的远程服务器是Cygwin OpenSSH服务器或基于Unix的服务器的某些其他端口,则SFTP服务器解释文件名的方式与Windows操作系统解释文件名的方式可能不匹配。
You need to figure out the correct syntax to refer to the paths on the D: drive through this SFTP server. 您需要找出正确的语法来引用通过此SFTP服务器的D:驱动器上的路径。 If it's the Cygwin SFTP server, I think the right syntax would be "/cygdrive/d/Datastores/RootStore/Test/...".
如果是Cygwin SFTP服务器,我认为正确的语法应该是“ / cygdrive / d / Datastores / RootStore / Test / ...”。 If the SFTP server is by some other vendor, you may need to consult the server documentation.
如果SFTP服务器是其他供应商提供的,则可能需要查阅服务器文档。 Alternately, you could try logging into the SFTP server interactively, cd'ing to the "/" directory, and exploring from there.
或者,您可以尝试以交互方式登录SFTP服务器,cd进入“ /”目录,然后从那里进行探索。 It ought to become obvious how to refer to folders on the D drive.
如何引用D驱动器上的文件夹应该变得显而易见。
I agree with Kenster's answer. 我同意肯斯特的回答。 I had similar problem on remote windows machine and the path with linux style solve my problem.
我在远程Windows机器上有类似的问题,Linux风格的路径解决了我的问题。 Just wanted to add, the root directory for ChannelSftp is C:\\ disk on remote windows machine and I'm not sure there is a way to put file on D:\\ disk.
只是想添加,ChannelSftp的根目录是远程Windows机器上的C:\\磁盘,我不确定是否有办法将文件放在D:\\磁盘上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.