Package com.trilead.ssh2

Examples of com.trilead.ssh2.SFTPv3Client


        }
      }

    } catch (Exception e) {
      if (e instanceof SFTPException) {
        SFTPException sftpExp = (SFTPException) e;
        if (sftpExp.getServerErrorCode() != 2)
          sftpExp.printStackTrace();
      } else
        e.printStackTrace();

    }
View Full Code Here


  }

  public File transferCommandScript(File commandFile, boolean isWindows) throws Exception {
    try {
      SFTPv3Client sftpClient = new SFTPv3Client(this.getSshConnection());
      SFTPv3FileAttributes attr = new SFTPv3FileAttributes();
      attr.permissions = new Integer(0700);

      boolean exists = true;
      // check if File already exists
      while (exists) {
        try {
          SFTPv3FileAttributes attribs = sftpClient.stat(commandFile.getName());
        }
        catch (SFTPException e) {
          getLogger().debug9("Error code when checking file existence: " + e.getServerErrorCode());
          exists = false;
        }
        if (exists) {
          getLogger().debug1("file with that name already exists, trying new name...");
          String suffix = (isWindows ? ".cmd" : ".sh");
          File resultFile = File.createTempFile("sos", suffix);
          resultFile.delete();
          commandFile.renameTo(resultFile);
          commandFile = resultFile;
        }
      }

      // set execute permissions for owner
      SFTPv3FileHandle fileHandle = sftpClient.createFileTruncate(commandFile.getName(), attr);

      FileInputStream fis = null;
      long offset = 0;
      try {
        fis = new FileInputStream(commandFile);
        byte[] buffer = new byte[1024];
        while (true) {
          int len = fis.read(buffer, 0, buffer.length);
          if (len <= 0)
            break;
          sftpClient.write(fileHandle, offset, buffer, 0, len);
          offset += len;
        }
        fis.close();
        fis = null;

      }
      catch (Exception e) {
        throw new Exception("error occurred writing file [" + commandFile.getName() + "]: " + e.getMessage());
      }
      finally {
        if (fis != null)
          try {
            fis.close();
            fis = null;
          }
          catch (Exception ex) {
          } // gracefully ignore this error
      }
      sftpClient.closeFile(fileHandle);

      fileHandle = null;
      sftpClient.close();
      return commandFile;
    }
    catch (Exception e) {
      throw new Exception("Error transferring command script: " + e, e);
    }
View Full Code Here

    }
  }

  private void deleteCommandScript(String commandFile) throws Exception {
    try {
      SFTPv3Client sftpClient = new SFTPv3Client(this.getSshConnection());
      sftpClient.rm(commandFile);
      sftpClient.close();
    }
    catch (Exception e) {
      getLogger().warn("Failed to delete remote command script: " + e);
    }
  }
View Full Code Here

    }
  }

  private SFTPv3Client FtpClient() throws Exception {
    if (sftpClient == null) {
      sftpClient = new SFTPv3Client(this.getSshConnection());
    }
    return sftpClient;
  }
View Full Code Here

            if (!isAuthenticated) throw new Exception("authentication failed [host=" + this.getHost() + ", port=" + this.getPort() + ", user:" + this.getUser()
                    + ", auth_method=" + this.getAuthenticationMethod() + ", auth_file=" + this.getAuthenticationFilename());

           
            sftpClient = new SFTPv3Client(sshConnection);
            connected = true;
            reply = "OK";
        } catch (Exception e) {
          reply = e.toString();
            if (sshConnection != null) try { disconnect(); } catch (Exception ex) {} // gracefully ignore this error
View Full Code Here

  }

  private SFTPv3Client Client() {
    if (objFTPClient == null) {
      try {
        objFTPClient = new SFTPv3Client(sshConnection);
      }
      catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
View Full Code Here

   *
   * @see jSimMacs.logic.location.DataHandler#createDirectory(java.lang.String, boolean)
   */
  public boolean createDirectory(String dir, boolean absolutePath) throws IOException {
    boolean success = true;
    SFTPv3Client sftpConn = null;
    String directory = dir;
    if(!absolutePath)
      directory = rProject.getPath() + dir;
    try {

      sftpConn = new SFTPv3Client(conn);
      boolean createDir = true;
      try {
        sftpConn.lstat(directory);
        createDir = false;
      } catch (IOException e) {
        createDir = true;
      }
      if (createDir)
        sftpConn.mkdir(directory, standardPermission);
    } catch (IOException e) {
      success = false;
      throw e;
    } finally {
      if (sftpConn != null)
        sftpConn.close();
    }
    return success;
  }
View Full Code Here

   * @throws IOException
   */
  public SFTPv3DirectoryEntry getDirectoryEntry(String path)
      throws IOException {
    SFTPv3DirectoryEntry entry = new SFTPv3DirectoryEntry();
    SFTPv3Client sftpConn = null;
    try {
      sftpConn = new SFTPv3Client(conn);
      SFTPv3FileAttributes attribute = sftpConn.lstat(path);
      entry.attributes = attribute;
      entry.filename = path.substring(path.lastIndexOf("/") + 1);
    } catch (IOException e) {
      throw e;
    } finally {
      if (sftpConn != null)
        sftpConn.close();
    }
    return entry;
  }
View Full Code Here

   *
   * @see jSimMacs.logic.location.DataHandler#createFile(java.lang.String)
   */
  public boolean createFile(String fileDirectory) throws IOException {
    boolean success = true;
    SFTPv3Client sftpConn = null;
    try {
      sftpConn = new SFTPv3Client(conn);
      sftpConn.createFile(fileDirectory);
    } catch (IOException e) {
      success = false;
      throw new IOException();
    } finally {
      if (sftpConn != null)
        sftpConn.close();
    }
    return success;
  }
View Full Code Here

   * @return Vector of DirectoryEntries
   * @throws IOException
   */
  public Vector<SFTPv3DirectoryEntry> listFiles(String path)
      throws IOException {
    SFTPv3Client sftpConn = null;
    Vector<SFTPv3DirectoryEntry> nodes;
    try {
      sftpConn = new SFTPv3Client(conn);
      nodes = sftpConn.ls(path);
      filterNodes(nodes);
    } catch (IOException e) {
      throw e;
    } finally {
      if (sftpConn != null)
        sftpConn.close();
    }
    return nodes;

  }
View Full Code Here

TOP

Related Classes of com.trilead.ssh2.SFTPv3Client

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.