Ejemplo n.º 1
0
  public static void main(String[] args) {
    SFTPGetTest test = new SFTPGetTest();

    Map<String, String> sftpDetails = new HashMap<String, String>();
    // 设置主机ip,用户名,密码,端口
    sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, IP);
    sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, PORT);
    sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, USERNAME);
    sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, PASSWOED);

    // 创建下载通道。以及超时时长,单位是毫秒
    SFTPChannel channel = test.getSFTPChannel();
    ChannelSftp chSftp = null;
    try {
      chSftp = channel.getChannel(sftpDetails, delayTime);
    } catch (JSchException e1) {
      System.out.println("error:创建下载通道失败!failed to creat a channel!");
      e1.printStackTrace();
    }

    SftpATTRS attr = null;
    try {
      attr = chSftp.stat(FILE_PATH_SFTP);
    } catch (SftpException e1) {
      System.out.println("error:获取文件大小失败失败!failed to get the size of the file!");
      e1.printStackTrace();
    }
    long fileSize = attr.getSize();
    System.out.println("fileSize=" + fileSize);
    try {

      chSftp.get(FILE_PATH_SFTP, DESTINATION_LOCAL, new MyProgressMonitor(fileSize)); // 代码段1

      //            OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件
      //             chSftp.get(FILE_PATH_SFTP, out, new FileProgressMonitor(fileSize)); // 代码段2

      /**
       * 代码段3
       *
       * <p>OutputStream out = new FileOutputStream(DESTINATION_LOCAL);//这是采用文件输入流方式下载文件 InputStream
       * is = chSftp.get(FILE_PATH_SFTP, new MyProgressMonitor()); byte[] buff = new byte[1024 * 2];
       * int read; if (is != null) { System.out.println("Start to read input stream"); do { read =
       * is.read(buff, 0, buff.length); if (read > 0) { out.write(buff, 0, read); } out.flush(); }
       * while (read >= 0); System.out.println("input stream read done."); }
       */
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      chSftp.quit();
      try {
        channel.closeChannel();
      } catch (Exception e) {
        System.out.println("error:关闭下载通道失败!failed to close the channel!");
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 2
0
  public String commandLs(String dir) {
    String token = "{\"ls\": [{";
    String[] files;

    try {
      files = this.sftp.ls(dir);
    } catch (SftpException e) {
      files = new String[0];
      e.printStackTrace();
    }

    for (String filename : files) {
      String type = filename.substring(0, 1);
      token +=
          "\"name\": \""
              + filename.substring(1)
              + "\", \"isdir\": "
              + String.valueOf(type.equals("d"))
              + "}, {";
    }
    token = token.substring(0, token.length() - 3) + "]}";

    return token;
  }
  /**
   * This method should only be run when a new version of the Manager will be released. It tries to
   * make the process more automatic. Currently: - Writes in the Version file in LOCAL DISK (Dropbox
   * folder, so Dropbox must be running!) - Uploads the file to the correct folder tree in the
   * SourceForge project, creating all needed folder on the way.
   */
  public static void main(String[] args) throws ZipException, IOException, JSchException {

    /*
     * IMPORTANT
     *
     * Before releasing a new version of the Manager, please, follow these instructions:
     * 0 - Update the Changelog.txt and Version.txt files
     * 1 - Check if the older version is fully compatible with this one after an update. managerOptions.xml shall not be lost by any reasons.
     * 2 - Check if the file paths below are correct.
     * 3 - Clean and build the project, then Package-for-store the Manager.jar
     * 4 - Goto Sourceforge.net and update the LABEL and the OS supported for the new Manager file.
     * 5 - Update in the HoN forums page (changelog, topic title and version in first line)
     */

    // First step is to get the version we want to release.
    String targetVersion = ManagerOptions.getInstance().getVersion();
    // Get the old jar
    File oldJarVersion =
        new File(
            "C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions\\Manager.jar");
    // And the newly generated one
    File newJarVersion = new File("store\\Manager.jar");
    // Target output for where the differences will be generated
    String verionsFile =
        "C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions.txt";
    File rootVersionsFolder =
        new File("C:\\Users\\Shirkit\\Dropbox\\HonModManager\\Dropbox\\Public\\versions\\");
    File output = new File(rootVersionsFolder, targetVersion + ".jar");

    System.out.println("Version to be released=" + targetVersion);
    System.out.println("Old version file=" + oldJarVersion.getAbsolutePath());
    System.out.println("New version file=" + newJarVersion.getAbsolutePath());
    System.out.println();

    if (calculate(oldJarVersion, newJarVersion, output.getAbsolutePath())) {
      System.out.println(
          "Output file generated.\nPath="
              + output.getAbsolutePath()
              + "\nSize="
              + output.length() / 1024
              + "KB");

      // Read the current versions file and store it for later
      FileInputStream fis = new FileInputStream(verionsFile);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      copyInputStream(fis, baos);
      String s = baos.toString();
      fis.close();
      baos.close();

      System.out.println(s);

      if (!s.trim().isEmpty()) {
        if (s.contains("\n")) {
          System.out.println("Older version=" + s.substring(0, s.indexOf("\n")));
        } else {
          System.out.println("Older version=" + s);
        }
        s = targetVersion + "\n" + s;
      } else {
        System.out.println("First version!");
        s = targetVersion;
      }

      if (JOptionPane.showConfirmDialog(
              null, "Confirm upload?", "Confirmation", JOptionPane.YES_NO_OPTION)
          != 0) {
        System.exit(0);
      }

      // Write new versions file with the new released version
      FileWriter fw = new FileWriter(verionsFile);
      fw.write(s);
      fw.flush();
      fw.close();
      System.out.println("Versions file written with sucess!");

      fis = new FileInputStream(newJarVersion);
      FileOutputStream fos =
          new FileOutputStream(rootVersionsFolder + File.separator + "Manager.jar");
      copyInputStream(fis, fos);
      fis.close();
      fos.close();
      System.out.println("Manager.jar file written!");

      System.out.println();
    } else {
      System.err.println("No differences file. Output file not generated.");
      System.exit(0);
    }

    JSch jsch = new JSch();
    Session session = null;
    try {
      System.out.println("Connecting to SF");

      session =
          jsch.getSession(
              JOptionPane.showInputDialog("SourceForge Username") + ",all-inhonmodman",
              "frs.sourceforge.net",
              22);
      session.setConfig("StrictHostKeyChecking", "no");
      session.setPassword(JOptionPane.showInputDialog("SourceForge Password"));
      session.connect();

      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp sftpChannel = (ChannelSftp) channel;
      System.out.println("Connected!");
      String root = "/home/frs/project/a/al/all-inhonmodman";
      sftpChannel.cd(root);
      StringTokenizer versionTokens = new StringTokenizer(targetVersion, " ");
      boolean flag = true;
      while (versionTokens.hasMoreTokens()) {
        String s = versionTokens.nextToken();
        if (!cdExists(sftpChannel, s)) {
          sftpChannel.mkdir(s);
          flag = false;
        }
        sftpChannel.cd(s);
      }
      if (flag) {
        System.err.println("Version already exists!");
        sftpChannel.exit();
        session.disconnect();
        System.exit(0);
      }
      System.out.println("Uploading file");
      OutputStream out = sftpChannel.put("Manager.jar");
      FileInputStream fis = new FileInputStream(newJarVersion);
      copyInputStream(fis, out);
      out.close();
      fis.close();
      System.out.println("Upload complete");
      sftpChannel.exit();
      session.disconnect();
      System.out.println("SUCESS!");
    } catch (JSchException e) {
      e.printStackTrace();
    } catch (SftpException e) {
      e.printStackTrace();
    }

    System.exit(0);
  }