English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java simple usage of SFTP to upload files to the server

After recently using SFTP to upload files and finding some information, I have made some summaries for future reference. The specific code is as follows:

 /**
  * Upload the file to the server
  * 
  * @param filePath
  *   File path
  * @param channelSftp
  *   The channelSftp object
  * @return
  */
 public static boolean uploadFile(String filePath, ChannelSftp channelSftp) {
  OutputStream outstream = null;
  InputStream instream = null;
  boolean successFlag = false;
  try {
   File isfile = new File(filePath);
   if (isfile.isFile()) {
    outstream = channelSftp.put(isfile.getName());
    File file = new File(filePath);
    if (file.exists()) {
     instream = new FileInputStream(file);
     byte b[] = new byte[1024];
     int n;
     while ((n = instream.read(b)) != -1) {
      outstream.write(b, 0, n);
     }
     outstream.flush();
    }
    successFlag = true;
   }
  }
   e.printStackTrace();
  }
   try {
    if (instream != null) {
     instream.close();
    }
    if (outstream != null) {
     outstream.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return successFlag;
 }
 private static Session initJschSession()
   throws JSchException {
  int ftpPort = 0;
  String ftpHost = "";
  String port = "00"; //The port number of sftp
  String ftpUserName = ""; //Username
  String ftpPassword = ""; //Link password
  String privateKey = ""; //
  String passphrase = "";
  if (port != null && !port.equals("")) {
   ftpPort = Integer.valueOf(port);
  }
  JSch jsch = new JSch(); // Create JSch object
  if (StringUtils.isNotBlank(privateKey)
    && StringUtils.isNotBlank(passphrase)) {
   jsch.addIdentity(privateKey, passphrase);
  }
  if (StringUtils.isNotBlank(privateKey)
    && StringUtils.isBlank(passphrase)) {
   jsch.addIdentity(privateKey);
  }
  jsch.getSession(ftpUserName, ftpHost, ftpPort);
  Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // Get a Session object based on username, host IP, and port
  if (StringUtils.isNotBlank(ftpPassword)) {
   session.setPassword(ftpPassword); // Set password
  }
  return session;
 }
 /**
  * Get ChannelSftp link
  * 
  * @param timeout
  *   Timeout time
  * @return Returns the ChannelSftp object
  * @throws JSchException
  */
 public static ChannelSftp getChannelSftp(Session session, int timeout)
   throws JSchException {
  Channel channel = null;
  Properties config = new Properties();
  config.put("StrictHostKeyChecking", "no");
  session.setConfig(config); // Set properties for the Session object
  session.setTimeout(timeout); // Set timeout time
  session.connect(); // Establish link through Session
  channel = session.openChannel("sftp"); // Open SFTP channel
  channel.connect(); // Establish connection of SFTP channel
  return (ChannelSftp) channel; 
 }
 /**
  * Disconnect SFTP link
  * 
  * @param session
  *   Session
  * @param channel
  *   Channel
  */
 public static void closeConnection(Channel channel, Session session) {
  try {
   if (session != null) {
    session.disconnect(); //Close session link
   }
   if (channel != null) {
    channel.disconnect(); //Disconnect
   }
  }
   e.printStackTrace();
  }
 }

The usernames and passwords here are all set by themselves. The method has been simply encapsulated for easy use.

The above is a simple introduction by the editor on how to use Java to upload files to the server using SFTP, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to you in time. I also want to express my sincere gratitude to everyone for supporting the Yell Tutorial website!

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting, please replace # with @) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like