当前位置: 移动技术网 > IT编程>开发语言>Java > Java使用SFTP上传文件到服务器的简单使用

Java使用SFTP上传文件到服务器的简单使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

上海会展公司,生活大爆炸第十一季,卡奇娱乐荐藏片

最近用到sftp上传文件查找了一些资料后自己做了一点总结,方便以后的查询。具体代码如下所示:

 /**
  * 将文件上传到服务器
  * 
  * @param filepath
  *   文件路径
  * @param channelsftp
  *   channelsftp对象
  * @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的端口号
  string ftpusername = ""; //用户名
  string ftppassword = ""; //链接的密码
  string privatekey = ""; //
  string passphrase = "";
  if (port != null && !port.equals("")) {
   ftpport = integer.valueof(port);
  }
  jsch jsch = new jsch(); // 创建jsch对象
  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); // 根据用户名,主机ip,端口获取一个session对象
  if (stringutils.isnotblank(ftppassword)) {
   session.setpassword(ftppassword); // 设置密码
  }
  return session;
 }
 /**
  * 获取channelsftp链接
  * 
  * @param timeout
  *   超时时间
  * @return 返回channelsftp对象
  * @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); // 为session对象设置properties
  session.settimeout(timeout); // 设置timeout时间
  session.connect(); // 通过session建立链接
  channel = session.openchannel("sftp"); // 打开sftp通道
  channel.connect(); // 建立sftp通道的连接
  return (channelsftp) channel; 
 }
 /**
  * 断开sftp链接
  * 
  * @param session
  *   会话
  * @param channel
  *   通道
  */
 public static void closeconnection(channel channel, session session) {
  try {
   if (session != null) {
    session.disconnect(); //关闭session链接
   }
   if (channel != null) {
    channel.disconnect(); //断开连接
   }
  } catch (exception e) {
   e.printstacktrace();
  }
 }

这里的用户名密码都是自己设置,这里的方法进行了简单的封装,方便使用。

以上所述是小编给大家介绍的java使用sftp上传文件到服务器的简单使用,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网