Package com.jcraft.jsch

Examples of com.jcraft.jsch.ChannelSftp


     * @param session
     *            to connect to
     * @return channelSftp or null if not successful (channel not existent or dead)
     */
    public ChannelSftp getChannelSftp(Session session) throws IOException {
        ChannelSftp channel = null;
        Entry entry = getCacheEntry(session);
        if (entry != null) {
            channel = entry.getChannelSftp();
            if (channel != null && !channel.isConnected()) {
                entry.releaseChannelSftp();
                channel = null;
            }
        }
        return channel;
View Full Code Here


   * @throws IOException if any connection problem occurs
   */
  private ChannelSftp getSftpChannel(String pathOrUri) throws IOException {
      Session session = getSession(pathOrUri);
        String host = session.getHost();
        ChannelSftp channel = SshCache.getInstance().getChannelSftp(session);
        if(channel == null) {
            try {
                channel = (ChannelSftp) session.openChannel("sftp");
                channel.connect();
                Message.verbose(":: SFTP :: connected to "+host+"!");
                SshCache.getInstance().attachChannelSftp(session,channel);
            } catch (JSchException e) {
                IOException ex = new IOException(e.getMessage());
                ex.initCause(e);
View Full Code Here

     * retrieves an sftp channel from the cache
     * @param host to connect to
     * @return channelSftp or null if not successful (channel not existent or dead)
     */
    public ChannelSftp getChannelSftp(Session session) throws IOException {
        ChannelSftp channel = null;
        Entry entry = getCacheEntry(session);
        if(entry != null) {
            channel = entry.getChannelSftp();
            if(channel != null && !channel.isConnected()) {
                entry.releaseChannelSftp();
                channel = null;
            }
        }   
        return channel;
View Full Code Here

     * @return a fully initialised resource, able to answer to all its methods without needing
     *   any further connection
     */
  public Resource resolveResource(String path) {
    try {
      ChannelSftp c = getSftpChannel(path);
      Collection r = c.ls(path);
      if (r != null) {
        for (Iterator iter = r.iterator(); iter.hasNext();) {
          Object obj=iter.next();
                  if(obj instanceof LsEntry){
                    LsEntry entry = (LsEntry) obj;
View Full Code Here

    }
    return new BasicResource(path, false, 0, 0, false);
  }

  public InputStream openStream(SFTPResource resource) throws IOException {
        ChannelSftp c = getSftpChannel(resource.getName());
        try {
      return c.get(resource.getName());
    } catch (SftpException e) {
      e.printStackTrace();
      IOException ex = new IOException("impossible to open stream for "+resource+" on "+getHost()+(e.getMessage() != null?": " + e.getMessage():""));
      ex.initCause(e);
      throw ex;
View Full Code Here

    }
  }

    public void get(String source, File destination) throws IOException {
        fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
        ChannelSftp c = getSftpChannel(source);
        try {
      c.get(source, destination.getAbsolutePath(), new MyProgressMonitor());
    } catch (SftpException e) {
      e.printStackTrace();
      IOException ex = new IOException("impossible to get "+source+" on "+getHost()+(e.getMessage() != null?": " + e.getMessage():""));
      ex.initCause(e);
      throw ex;
View Full Code Here

    }
    }

  public void put(File source, String destination, boolean overwrite) throws IOException {
        fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT);
        ChannelSftp c = getSftpChannel(destination);
        try {
            if(!overwrite && checkExistence(destination, c))
                throw new IOException("destination file exists and overwrite == true");
          if (destination.indexOf('/') != -1) {
            mkdirs(destination.substring(0, destination.lastIndexOf('/')),c);
          }
      c.put(source.getAbsolutePath(), destination, new MyProgressMonitor());
    } catch (SftpException e) {
      IOException ex = new IOException(e.getMessage());
      ex.initCause(e);
      throw ex;
    }
View Full Code Here

  }


  public List list(String parent) throws IOException {
    try {
      ChannelSftp c = getSftpChannel(parent);
      Collection r = c.ls(parent);
      if (r != null) {
        if (!parent.endsWith("/")) {
          parent = parent+"/";
        }
        List result = new ArrayList();
View Full Code Here

        String unixPath = "target/scp/out.txt";
        File target = new File(unixPath);
        root.mkdirs();
        assertTrue(root.exists());

        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
        c.connect();
        c.put(new ByteArrayInputStream("0123456789".getBytes()), unixPath);

        assertTrue(target.exists());
        assertEquals("0123456789", readFile(unixPath));

        OutputStream os = c.put(unixPath, null, ChannelSftp.APPEND, -5);
        os.write("a".getBytes());
        os.close();
        c.disconnect();

        assertTrue(target.exists());
        assertEquals("01234a", readFile(unixPath));

        target.delete();
View Full Code Here

        root.delete();
    }

    @Test
    public void testReadDir() throws Exception {
        ChannelSftp c = (ChannelSftp) session.openChannel("sftp");
        c.connect();
        Vector res = c.ls("target/classes/org/apache/sshd/");
        for (Object f : res) {
            System.out.println(f.toString());
        }
    }
View Full Code Here

TOP

Related Classes of com.jcraft.jsch.ChannelSftp

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.