Java USES SFTP to upload files to the server for simple use

  • 2020-06-07 04:35:26
  • OfStack

Recently, I used SFTP to upload a file and find some information. After that, I made a summary for the convenience of future query. The specific code is as follows:


 /**
  *  Upload the file to the server 
  * 
  * @param filePath
  *    The file path 
  * @param channelSftp
  *   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;
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   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"; //sftp The port number 
  String ftpUserName = ""; // The user name 
  String ftpPassword = ""; // The password of the link 
  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); //  According to user name, host ip , port acquisition 1 a Session object 
  if (StringUtils.isNotBlank(ftpPassword)) {
   session.setPassword(ftpPassword); //  Set the password 
  }
  return session;
 }
 /**
  *  To obtain ChannelSftp link 
  * 
  * @param timeout
  *    timeout 
  * @return  return 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); //  for Session Object to set properties
  session.setTimeout(timeout); //  Set up the timeout time 
  session.connect(); //  through Session Link building 
  channel = session.openChannel("sftp"); //  Open the SFTP channel 
  channel.connect(); //  To establish SFTP Channel connection 
  return (ChannelSftp) channel; 
 }
 /**
  *  Disconnect the sftp link 
  * 
  * @param session
  *    The session 
  * @param channel
  *    channel 
  */
 public static void closeConnection(Channel channel, Session session) {
  try {
   if (session != null) {
    session.disconnect(); // Shut down session link 
   }
   if (channel != null) {
    channel.disconnect(); // disconnect 
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

The username and password here are set by themselves, the method here is a simple package, easy to use.


Related articles: