This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Apr 2, 2013 7:56 AM by gimbal2 RSS

jcraft jsch channelSftp.put help

1000454 Newbie
Currently Being Moderated
Hi everyone!

I'm using jsch to connect to sftp and upload/download some files.

Does anyone know if there is some way to add some extra flags when uploading a file over sftp, using JSCH library?

Until now, we did all the work by hand, typing a command into FTP client:

put local_file_path  remote_file_path , 0 , 16 , 1080 , 900

Now I need this to be done programatically, by using Jsch and SFTP protocol.


I did manage to upload files with channelSftp.put() method, but I need this extra commands ( ,0,16,1080,900) in order to create a valid file on server.

Server is not windows or linux and does not have any famous file system.
It is rare OS and file system so it needs this extra flags...
  • 1. Re: jcraft jsch channelSftp.put help
    gimbal2 Guru
    Currently Being Moderated
    The source is the documentation I'm afraid. Check out the many example programs that come with JSCH and if it isn't in there you'll have to check the source of JSCH itself. That's the kind of effort you have to make when you want to do something very unusual.
  • 2. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    Having spent significant time studying the SSH and SFTP protocol documents and implementing both an SSH client and server I'm pretty sure (but not certain without going through the documentation again) that there is nothing in the SFTP protocol that stops one appending a comma separated list of parameters to the remote file path. I'm also pretty sure that there is nothing in the JSCH SFTP client library that parses the remote file name so I don't see why one just can't append the parameters to the remote file name. Have you tried doing this?

    Why are you being so mysterious about the server OS? It might help us help you if we knew which OS and which SSH server is being used.

    P.S. Since you are able to perform the transfer using the command line you could always use Runtime.exec() or ProcessBuilder!
  • 3. Re: jcraft jsch channelSftp.put help
    1000454 Newbie
    Currently Being Moderated
    sabre150, thank you for your responce.

    Unfortunatelly, adding a list of flags to remoteFile name does not work.
    I get the same thing I been getting when I try to upload it without these flags:

    ORA-29532: Java call terminated by uncaught Java exception: 4: java.io.InterruptedIOException: send() timed out
    A non-blocking socket operation could not be completed immediately.

    However, when I connect to sftp through putty, this command work normally...


    Am i missing something?

    Edited by: 997451 on 02.04.2013. 03.28
  • 4. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    Since SFTP does not communicate directly with sockets but sits on top of SSH the implication of that exception is that the problem is with the SSH communication and not SFTP itself. Since it interests me, if I can find the time I will try to use JSCH to send a file with that peculiar file name construct to an openssh server. Of course you could make it easier for me to find the time by posting your Java code! This might also allow us to decide whether or not you are missing something!

    Again, why are you so reluctant to publish which OS and which SSH server your are using? Surely it can't hurt to publish this information.

    Edited by: sabre150 on Apr 2, 2013 12:37 PM

    I have just transferred files with the added comma separated paramters using JSCH/SFTP version jsch-0.1.48 to an openssh server and to my own server and both produce the correct files on the server. This taken with your ability to send the files using Putty implies to me that you are using JSCH wrongly. Without seeing your code I have no way of being certain.
  • 5. Re: jcraft jsch channelSftp.put help
    1000454 Newbie
    Currently Being Moderated
    No, of course it cannot hurt :-)

    OS is "Guardian".


    I connect to it using java stored procedures, DB is oracle 10g.


    The code is in several java stored procedures. In short I do this:


    // inside class "SftpConnector", i build sftpChannel

    public void buildChannelSftp
    {
    JSch jsch = new JSch();
    jsch.setKnownHosts(knownHosts);
    session = jsch.getSession(sftpUser,sftpHost,sftpPort);
    session.setPassword(sftpPassword);

    session.setConfig(sessionConfig); // StrictHostKeyChecking = "no"

    session.setTimeout(sessionTimeout * 1000); // sessionTimeout = 0
    session.connect();
    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp)channel;
    }

    later, in some other class:

    ...// I take built channel...

    channelSftp.lcd(localDirectory);
    channelSftp.cd(remoteDirectory);
    channelSftp.put(localFile, remoteFile, ChannelSftp.RESUME); // I also tried without ChannelSftp.RESUME

    Edited by: 997451 on 02.04.2013. 05.00

    Edited by: 997451 on 02.04.2013. 05.01
  • 6. Re: jcraft jsch channelSftp.put help
    1000454 Newbie
    Currently Being Moderated
    sabre 150, may I see your code?

    I'm really curious where I went wrong
  • 7. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    I have never used JSCH from stored procedure. I don't think you should be keeping open the JSch connection; at least until you have proved you can actually perform the transfer I think you should be opening the connection each time you want to do a transfer.

    Why are you not trying this code outside of a stored procedure? If I were doing this I would create a class that I could test outside of a store procedure and then when I was sure it was working properly I would use it, without modifications, within the stored procedure.
  • 8. Re: jcraft jsch channelSftp.put help
    1000454 Newbie
    Currently Being Moderated
    I do create connection every time I transfer something.
    After that I close it.


    I cannot create standalone java app because I only have these resources, and I can only connect to sftp through DB ( network restrictions).


    Did you find anything wrong with my source?
  • 9. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    997451 wrote:
    I do create connection every time I transfer something.
    After that I close it.
    But you say "later, in some other class" !

    >
    >
    I cannot create standalone java app because I only have these resources, and I can only connect to sftp through DB ( network restrictions).
    Presumably on your development system you can install an 'openssh' server and then test basic functionality against that!

    >
    >
    Did you find anything wrong with my source?
    You seem to do a lot more than I do and the fragmented nature of your code makes it difficult for me duplicate. You don't show the construction of 'sessionConfig' or 'known' hosts so I cannot use what you have to diagnose anything. Also, I use public key authentication rather than 'password' authentication.
  • 10. Re: jcraft jsch channelSftp.put help
    gimbal2 Guru
    Currently Being Moderated
    fair is fair dude - now I wonder why you are so reluctant to share your code :) I am really curious how you invoked the 'put' method to be able to claim this:
    I have just transferred files with the added comma separated paramters using JSCH/SFTP version jsch-0.1.48 to an openssh server
  • 11. Re: jcraft jsch channelSftp.put help
    1000454 Newbie
    Currently Being Moderated
    fair is fair dude - now I wonder why you are so reluctant to share your code  I am really curious how you invoked the 'put' method to be able to claim this:

    I'm curious too :-)

    Edited by: 997451 on 02.04.2013. 07.15
  • 12. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    gimbal2 wrote:
    fair is fair dude - now I wonder why you are so reluctant to share your code :) I am really curious how you invoked the 'put' method to be able to claim this:
    I have just transferred files with the added comma separated paramters using JSCH/SFTP version jsch-0.1.48 to an openssh server
    I'm reluctant to do the OPs work for him; this forum is not a coding service and he is getting paid for this and I'm not. The line of code I use is simply
           channel.put(System.getProperty("user.home") + "/PIC-LIST.txt", "xxxxx.txt, 0, 16, 1080, 900");
    which transfers a file on my laptop to a file on my server. I run an 'openssh' server on both my laptop and server.

    There is just too much of code missing for me to turn the OPs code fragments into a representative SSCCE so if the OP wants me to help any further then he is going to have to create an SSCCE and post it.
  • 13. Re: jcraft jsch channelSftp.put help
    gimbal2 Guru
    Currently Being Moderated
    I believe that single line of code saves the day though; to be honest I wouldn't have considered trying that, I would have searched for a way to execute a low level FTP command like you can do with Apache commons.net; there doesn't seem to be one if I quickly glance over the javadoc.
  • 14. Re: jcraft jsch channelSftp.put help
    sabre150 Expert
    Currently Being Moderated
    gimbal2 wrote:
    I believe that single line of code saves the day though; to be honest I wouldn't have considered trying that, I would have searched for a way to execute a low level FTP command like you can do with Apache commons.net; there doesn't seem to be one if I quickly glance over the javadoc.
    SFTP is not in any way related to FTP and all I did was to look at the Javadoc for class ChannelSftp.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points