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");
source share