加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 19:43:22 所属栏目:Java 来源:网络整理
导读:最近用到SFTP上传文件查找了一些资料后自己做了一点总结,方便以后的查询。具体代码如下所示: /** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp * channelSftp对象 * @return */ public static boolean uploadFile(String fil

最近用到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,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,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上传文件到服务器的简单使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:

  • JAVA SFTP文件上传、下载及批量下载实例
  • java实现sftp客户端上传文件以及文件夹的功能代码
  • Java使用sftp定时下载文件的示例代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读