Git Product home page Git Product logo

sftp-utils's Introduction

sftp-utils

execute

Execute sftp commands giving a managed ChannelSftp

Complete example:

// Create SessionFactory instance using supplied SimpleSessionFactory class

Properties properties = new Properties();
properties.setProperty("username", "mhewedy");
properties.setProperty("host", "192.168.1.10");
properties.setProperty("port", "22");
properties.setProperty("password", "system");

//Thread safe class, better to cache instance of it. (e.g. as a Spring Bean)
SessionFactory sessionFactory = new SessionFactory.SimpleSessionFactory(properties);

String fileDir = "test/files/goes/here";
byte[] bytes = "Test me\n".getBytes("utf8");

// create dir recursive:

SftpUtils.execute(sessionFactory, channel -> {
   SftpUtils.mkdirp(channel, fileDir);
});

// write a file:

String filePath = SftpUtils.execute(sessionFactory, channel -> {
   String path = String.format("%s/%s.%s", fileDir, System.currentTimeMillis(), "txt");
   SftpUtils.mkdirp(channel, fileDir);
   channel.put(new ByteArrayInputStream(bytes), path);
   return path;
});

System.out.println("file written at: " + filePath);

// download the file:

String fileContents = SftpUtils.execute(sessionFactory, channel -> {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   channel.get(filePath, baos);
   return new String(baos.toByteArray());
});

System.out.println("file contents from the server: " + fileContents);

// delete the file:

SftpUtils.execute(sessionFactory, channel -> {
   channel.rm(filePath);
});

// delete the empty directory:

SftpUtils.execute(sessionFactory, channel -> {
   SftpUtils.rmr(channel, fileDir.split("/")[0]);
});

Check file is found:

SftpUtils.execute(sessionFactory, channel -> {
    SftpUtils.mkdirp(channel, "path/to/new/file");
});

boolean findFound = SftpUtils.execute(sessionFactory, channel -> {
    try{
        channel.lstat("non_found_find");
    }catch (SftpException ex){  // only handel exception when needed
        if (ex.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
            return false;
        }
    }
    return true;
});
System.out.println("find found: " + findFound);

Copy File and print its content from SFTP after copy:

InputStream transfer = Files.newInputStream(Paths.get("/Users/mhewedy/Work/Code/sftp-utils/README.md"));
String ftpPath = "path/to/new/file/";
String ftpFileName = "README.md2";

SftpUtils.execute(sessionFactory, channel -> {
    SftpUtils.mkdirp(channel, ftpPath);
    channel.put(transfer, ftpPath + ftpFileName);
});

String readmeFile = SftpUtils.execute(sessionFactory, channel -> {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    channel.get(ftpPath + ftpFileName, baos);
    byte[] bytes = baos.toByteArray();
    return new String(bytes);
});

mkdirp

Create directory structure recursively (from mkdir -p command)

SftpUtils.mkdirp(sftpChannel, "/path/to/dir"); //absolute
SftpUtils.mkdirp(sftpChannel, "another/path/to/dir"); // relative

rmr

Removes dirs and files recursivly, starting from a dir name (same as rm -r command)

SftpUtils.rmr(sftpChannel, "/existence/path/to/remove");  // removes all files and dirs inside the "remove" directory, including "remove" directory it self.
SftpUtils.rmr(sftpChannel, "/existence/path/"); // removes all files and dirs insdie the "path" directory, including "path" directory it self.

Usage:

  <dependency>
    <groupId>com.github.mhewedy</groupId>
    <artifactId>sftp-utils</artifactId>
    <version>2.0.0</version>
  </dependency>

sftp-utils's People

Contributors

mhewedy avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.