File upload successful but 0kb file uploaded to remote server using jsch sftp java

I upload a file from my local computer to a remote server in a simple java application using the Jsch and Sftp protocol.

My code does not receive any errors or exceptions and works successfully, but when I look at a remote location, the file loads as 0kb without the extension and is called "D".

I tried many ways, but I can not understand the error.

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 succeeds, line

 System.out.println(sftpChannel.getSession().isConnected()); 

gives true

and the next line completed successfully

 System.out.println("File uploaded successfully"); 
0
source share
2 answers
 String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\"; 

The SFTP protocol uses a naming scheme for file paths on a remote server. The file separator is "/", and the path name without a leading "/" is considered relative to the current directory. Columns and backslashes are not significant.

According to these rules, you ask the remote server to create a file in your home directory with the name "D: \ DataStores ..." with a literal colon and a backslash. If your remote server is a Cygwin OpenSSH server or other Unix-based server port, a mismatch may occur between the way the SFTP server interprets the file name and the way the Windows file name is interpreted.

You need to figure out the correct syntax to refer to the paths on drive D: through this SFTP server. If this is a Cygwin SFTP server, I think the correct syntax would be "/ cygdrive / d / Datastores / RootStore / Test / ...". If the SFTP server belongs to a different provider, you may need to refer to the server documentation. Alternatively, you can try to log into the SFTP server interactively, cd'ing to the "/" directory and explore from there. It should be obvious how to access the folders on drive D.

0
source

I agree with Kenster's answer. I had a similar problem on a remote windows machine, and the linux-style path solves my problem. Just wanted to add, the root directory for ChannelSftp is C: \ disk on a remote Windows computer, and I'm not sure there is a way to put the file on D: \ disk.

0
source

Source: https://habr.com/ru/post/1201792/


All Articles