using java with sftp

Discussion in 'Java' started by Bumsys@gmail.com, Feb 22, 2008.

  1. Guest

    import com.jcraft.jsch.*;
    import java.io.*;
    import java.util.*;

    /**
    * This class is used to demonstrate the usage of the
    * JCraft JSch package to SFTP files.
    *
    * @author Tim Archer 04/20/07
    * @version $Revision: 1.1 $
    */
    public class TestJSch {

    /** Creates a new instance of TestCommonsNet */
    public TestJSch() {
    }

    /**
    * main - Unit test program
    * @param args Command line arguments
    *
    */
    public static void main(String[] args) {
    try {
    String ftpHost = "10.64.3.27";
    int ftpPort = 14147;
    String ftpUserName = "anabolik";
    String ftpPassword = "anabolik";
    String ftpRemoteDirectory = "/home/tarcher/tmp/";
    String fileToTransmit = "c:\\temp\\test.txt";

    //
    //First Create a JSch session
    //
    System.out.println("Creating session.");
    JSch jsch = new JSch();
    Session session = null;
    Channel channel = null;
    ChannelSftp c = null;

    //
    //Now connect and SFTP to the SFTP Server
    //
    try {
    //Create a session sending through our username and
    password
    session = jsch.getSession(ftpUserName, ftpHost,
    ftpPort);
    System.out.println("Session created.");
    session.setPassword(ftpPassword);
    //Security.addProvider(new
    com.sun.crypto.provider.SunJCE());

    //
    //Setup Strict HostKeyChecking to no so we dont get
    the
    //unknown host key exception
    //
    java.util.Properties config = new
    java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    System.out.println("Session connected.");

    //
    //Open the SFTP channel
    //
    System.out.println("Opening Channel.");
    channel = session.openChannel("sftp");
    channel.connect();
    c = (ChannelSftp)channel;
    } catch (Exception e) {
    System.err.println("Unable to connect to FTP server.
    "+e.toString());
    throw e;
    }

    //
    //Change to the remote directory
    //
    System.out.println("Changing to FTP remote dir: " +
    ftpRemoteDirectory);
    c.cd(ftpRemoteDirectory);

    //
    //Send the file we generated
    //
    try {
    File f = new File(fileToTransmit);
    System.out.println("Storing file as remote filename: "
    + f.getName());
    c.put(new FileInputStream(f), f.getName());
    } catch (Exception e) {
    System.err.println("Storing remote file failed.
    "+e.toString());
    throw e;
    }

    //
    //Get the list of files in the remote server directory
    //
    Vector files = c.ls(ftpRemoteDirectory);

    //
    //Log if we have nothing to download
    //
    if (files.size() == 0) {
    System.out.println("No files are available for
    download.");
    }
    //
    //Otherwise download all files except for the . and ..
    entries
    //
    else {
    for (int i=0; i<files.size(); i++) {
    com.jcraft.jsch.ChannelSftp.LsEntry lsEntry =
    (com.jcraft.jsch.ChannelSftp.LsEntry) files.get(i);

    if (!lsEntry.getFilename().equals(".") && !
    lsEntry.getFilename().equals("..")) {
    System.out.println("Downloading file
    "+lsEntry.getFilename());

    String outputFileName = "c:\\temp\
    \"+lsEntry.getFilename();

    //Get the write and write it to our local file
    system
    File f = new
    File(outputFileName);
    c.get(lsEntry.getFilename(), new
    FileOutputStream(f));

    //
    //Remove the file from the server
    //
    /*
    c.rm(lsEntry.getFilename());
    */
    }
    }
    }

    //
    //Disconnect from the FTP server
    //
    try {
    c.quit();
    } catch (Exception exc) {
    System.err.println("Unable to disconnect from FTP
    server. " + exc.toString());
    }

    } catch (Exception e) {
    System.err.println("Error: "+e.toString());
    }

    System.out.println("Process Complete.");
    System.exit(0);
    }
    }

    When i run code i have error:
    Creating session.
    Session created.
    Process Complete.
    Unable to connect to FTP server. com.jcraft.jsch.JSchException:
    java.net.ConnectException: Connection refused: connect
    Error: com.jcraft.jsch.JSchException: java.net.ConnectException:
    Connection refused: connect

    what is wrong?
     
    , Feb 22, 2008
    #1
    1. Advertising

  2. GArlington Guest

    On Feb 22, 10:44 am, wrote:
    > import com.jcraft.jsch.*;
    > import java.io.*;
    > import java.util.*;
    >
    > /**
    > * This class is used to demonstrate the usage of the
    > * JCraft JSch package to SFTP files.
    > *
    > * @author Tim Archer 04/20/07
    > * @version $Revision: 1.1 $
    > */
    > public class TestJSch {
    >
    > /** Creates a new instance of TestCommonsNet */
    > public TestJSch() {
    > }
    >
    > /**
    > * main - Unit test program
    > * @param args Command line arguments
    > *
    > */
    > public static void main(String[] args) {
    > try {
    > String ftpHost = "10.64.3.27";
    > int ftpPort = 14147;
    > String ftpUserName = "anabolik";
    > String ftpPassword = "anabolik";
    > String ftpRemoteDirectory = "/home/tarcher/tmp/";
    > String fileToTransmit = "c:\\temp\\test.txt";
    >
    > //
    > //First Create a JSch session
    > //
    > System.out.println("Creating session.");
    > JSch jsch = new JSch();
    > Session session = null;
    > Channel channel = null;
    > ChannelSftp c = null;
    >
    > //
    > //Now connect and SFTP to the SFTP Server
    > //
    > try {
    > //Create a session sending through our username and
    > password
    > session = jsch.getSession(ftpUserName, ftpHost,
    > ftpPort);
    > System.out.println("Session created.");
    > session.setPassword(ftpPassword);
    > //Security.addProvider(new
    > com.sun.crypto.provider.SunJCE());
    >
    > //
    > //Setup Strict HostKeyChecking to no so we dont get
    > the
    > //unknown host key exception
    > //
    > java.util.Properties config = new
    > java.util.Properties();
    > config.put("StrictHostKeyChecking", "no");
    > session.setConfig(config);
    > session.connect();
    > System.out.println("Session connected.");
    >
    > //
    > //Open the SFTP channel
    > //
    > System.out.println("Opening Channel.");
    > channel = session.openChannel("sftp");
    > channel.connect();
    > c = (ChannelSftp)channel;
    > } catch (Exception e) {
    > System.err.println("Unable to connect to FTP server.
    > "+e.toString());
    > throw e;
    > }
    >
    > //
    > //Change to the remote directory
    > //
    > System.out.println("Changing to FTP remote dir: " +
    > ftpRemoteDirectory);
    > c.cd(ftpRemoteDirectory);
    >
    > //
    > //Send the file we generated
    > //
    > try {
    > File f = new File(fileToTransmit);
    > System.out.println("Storing file as remote filename: "
    > + f.getName());
    > c.put(new FileInputStream(f), f.getName());
    > } catch (Exception e) {
    > System.err.println("Storing remote file failed.
    > "+e.toString());
    > throw e;
    > }
    >
    > //
    > //Get the list of files in the remote server directory
    > //
    > Vector files = c.ls(ftpRemoteDirectory);
    >
    > //
    > //Log if we have nothing to download
    > //
    > if (files.size() == 0) {
    > System.out.println("No files are available for
    > download.");
    > }
    > //
    > //Otherwise download all files except for the . and ..
    > entries
    > //
    > else {
    > for (int i=0; i<files.size(); i++) {
    > com.jcraft.jsch.ChannelSftp.LsEntry lsEntry =
    > (com.jcraft.jsch.ChannelSftp.LsEntry) files.get(i);
    >
    > if (!lsEntry.getFilename().equals(".") && !
    > lsEntry.getFilename().equals("..")) {
    > System.out.println("Downloading file
    > "+lsEntry.getFilename());
    >
    > String outputFileName = "c:\\temp\
    > \"+lsEntry.getFilename();
    >
    > //Get the write and write it to our local file
    > system
    > File f = new
    > File(outputFileName);
    > c.get(lsEntry.getFilename(), new
    > FileOutputStream(f));
    >
    > //
    > //Remove the file from the server
    > //
    > /*
    > c.rm(lsEntry.getFilename());
    > */
    > }
    > }
    > }
    >
    > //
    > //Disconnect from the FTP server
    > //
    > try {
    > c.quit();
    > } catch (Exception exc) {
    > System.err.println("Unable to disconnect from FTP
    > server. " + exc.toString());
    > }
    >
    > } catch (Exception e) {
    > System.err.println("Error: "+e.toString());
    > }
    >
    > System.out.println("Process Complete.");
    > System.exit(0);
    > }
    >
    > }
    >
    > When i run code i have error:
    > Creating session.
    > Session created.
    > Process Complete.
    > Unable to connect to FTP server. com.jcraft.jsch.JSchException:
    > java.net.ConnectException: Connection refused: connect
    > Error: com.jcraft.jsch.JSchException: java.net.ConnectException:
    > Connection refused: connect
    >
    > what is wrong?


    "Connection refused"!!!???
     
    GArlington, Feb 22, 2008
    #2
    1. Advertising

  3. Guest

    but why my connection is refused. what can it be?
     
    , Feb 22, 2008
    #3
  4. On Fri, 22 Feb 2008 03:28:18 -0800 (PST), wrote:
    > but why my connection is refused. what can it be?


    The connection is refused because noone is listening for incoming
    connections at the destination address:port that you attempted to
    connect to.

    /gordon

    --
     
    Gordon Beaton, Feb 22, 2008
    #4
  5. Fri, 22 Feb 2008 03:28:18 -0800 (PST), //:
    > Fri, 22 Feb 2008 03:11:34 -0800 (PST), /GArlington/:
    >> On Feb 22, 10:44 am, wrote:
    >>
    >>> [...]
    >>>
    >>> When i run code i have error:
    >>> Creating session.
    >>> Session created.
    >>> Process Complete.
    >>> Unable to connect to FTP server. com.jcraft.jsch.JSchException:
    >>> java.net.ConnectException: Connection refused: connect
    >>> Error: com.jcraft.jsch.JSchException: java.net.ConnectException:
    >>> Connection refused: connect
    >>>
    >>> what is wrong?

    >>
    >> "Connection refused"!!!???

    >
    > but why my connection is refused. what can it be?


    Do your server support SFTP connections? Could you connect to it
    using another software?

    --
    Stanimir
     
    Stanimir Stamenkov, Feb 22, 2008
    #5
  6. Nigel Wade Guest

    wrote:

    > but why my connection is refused. what can it be?


    Connection refused means that when you attempted to connect to the host/port the
    host responded with RST/ACK response, i.e. go away.

    This is normally the result of there being nothing listening on that port. Also,
    some firewalls are configured to respond in this manner to any attempted
    connection.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Feb 22, 2008
    #6
  7. RichTheCoder

    Joined:
    Aug 16, 2011
    Messages:
    1
    i would just add that 14147 might be the wrong port, and 22 often is used.
     
    RichTheCoder, Aug 16, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke

    java and ssh/sftp

    Luke, Nov 12, 2006, in forum: Java
    Replies:
    3
    Views:
    1,061
    Daniel Pitts
    Nov 13, 2006
  2. sftp + java

    , Feb 22, 2008, in forum: Java
    Replies:
    1
    Views:
    613
    GArlington
    Feb 22, 2008
  3. SFTP + Java

    , Apr 15, 2008, in forum: Java
    Replies:
    0
    Views:
    731
  4. MaggotChild

    Net::SFTP Fails, sftp OK

    MaggotChild, Oct 28, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    1,002
    C.DeRykus
    Oct 29, 2009
  5. Kevin13
    Replies:
    3
    Views:
    946
    Kevin13
    Dec 1, 2009
Loading...

Share This Page