file uploading successfully, but 0kb file is uploading on remote server using jsch sftp java

谁说胖子不能爱 提交于 2019-12-08 09:36:52

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31469904/file-uploading-successfully-but-0kb-file-is-uploading-on-remote-server-using-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!