简体   繁体   中英

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.

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'.

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. 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. 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.

You need to figure out the correct syntax to refer to the paths on the D: drive through this SFTP server. If it's the Cygwin SFTP server, I think the right syntax would be "/cygdrive/d/Datastores/RootStore/Test/...". If the SFTP server is by some other vendor, you may need to consult the server documentation. Alternately, you could try logging into the SFTP server interactively, cd'ing to the "/" directory, and exploring from there. It ought to become obvious how to refer to folders on the D drive.

I agree with Kenster's answer. I had similar problem on remote windows machine and the path with linux style solve my problem. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM