JAVA SFTP操作

sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

本篇示例中使用的第三方包为:jsch.jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * SFTP客户端工具类
 * 
 * @author jianggujin
 * 
 */
public class SftpClientTool
{
   /** Sftp */
   private ChannelSftp sftp = null;
   /** 主机 */
   private String host = "";
   /** 端口 */
   private int port = 22;
   /** 用户名 */
   private String username = "";
   /** 密码 */
   private String password = "";

   /**
    * 构造函数
    * 
    * @param host
    *           主机
    * @param port
    *           端口
    * @param username
    *           用户名
    * @param password
    *           密码
    * 
    */
   public SftpClientTool(String host, int port, String username, String password)
   {
      this.host = host;
      this.port = port;
      this.username = username;
      this.password = password;
   }

   /**
    * 构造函数
    * 
    * @param host
    *           主机
    * @param username
    *           用户名
    * @param password
    *           密码
    * 
    */
   public SftpClientTool(String host, String username, String password)
   {
      this.host = host;
      this.username = username;
      this.password = password;
   }

   /**
    * 连接sftp服务器,默认超时时间20000
    * 
    * @throws Exception
    */
   public void connect() throws Exception
   {
      connect(20000);
   }

   /**
    * 连接sftp服务器
    * 
    * @param timeout
    *           超时时间
    * 
    * @throws Exception
    */
   public void connect(int timeout) throws Exception
   {
      JSch jsch = new JSch();
      Session sshSession = jsch.getSession(this.username, this.host, this.port);
      sshSession.setPassword(password);
      Properties sshConfig = new Properties();
      sshConfig.put("StrictHostKeyChecking", "no");
      sshSession.setConfig(sshConfig);
      sshSession.connect(timeout);
      Channel channel = sshSession.openChannel("sftp");
      channel.connect();
      this.sftp = (ChannelSftp) channel;
   }

   /**
    * 断开sftp服务器连接
    * 
    * @throws Exception
    */
   public void disconnect() throws Exception
   {
      if (this.sftp != null)
      {
         if (this.sftp.isConnected())
         {
            this.sftp.disconnect();
         }
         else if (this.sftp.isClosed())
         {
         }
         this.sftp = null;
      }
   }

   /**
    * 上传单个文件
    * 
    * @param directory
    *           上传的目录
    * @param uploadFile
    *           要上传的文件
    * 
    * @throws Exception
    */
   public void upload(String directory, String uploadFile) throws Exception
   {
      this.sftp.cd(directory);
      File file = new File(uploadFile);
      this.sftp.put(new FileInputStream(file), file.getName());
   }

   /**
    * 下载单个文件
    * 
    * @param directory
    *           下载目录
    * @param downloadFile
    *           下载的文件
    * @param saveFile
    *           保存文件
    * 
    * @throws Exception
    */
   public void download(String directory, String downloadFile, String saveFile)
         throws Exception
   {
      this.sftp.cd(directory);
      File file = new File(saveFile);
      this.sftp.get(downloadFile, new FileOutputStream(file));
   }

   /**
    * 删除文件
    * 
    * @param directory
    *           要删除文件所在目录
    * @param deleteFile
    *           要删除的文件
    * 
    * @throws Exception
    */
   public void delete(String directory, String deleteFile) throws Exception
   {
      this.sftp.cd(directory);
      this.sftp.rm(deleteFile);
   }

   /**
    * 列出目录下的文件
    * 
    * @param directory
    *           要列出的目录
    * 
    * @return list 文件名列表
    * 
    * @throws Exception
    */
   @SuppressWarnings("unchecked")
   public List<String> listFiles(String directory) throws Exception
   {
      List<String> fileNameList = new ArrayList<String>();
      Vector<LsEntry> fileList = this.sftp.ls(directory);
      Iterator<LsEntry> it = fileList.iterator();
      while (it.hasNext())
      {
         String fileName = it.next().getFilename();
         // 特殊文件名,忽略
         if (".".equals(fileName) || "..".equals(fileName))
         {
            continue;
         }
         fileNameList.add(fileName);
      }
      return fileNameList;
   }

   /**
    * 更改文件名
    * 
    * @param directory
    *           文件所在目录
    * @param oldFileNm
    *           原文件名
    * @param newFileNm
    *           新文件名
    * 
    * @throws Exception
    */
   public void rename(String directory, String oldFileNm, String newFileNm)
         throws Exception
   {
      this.sftp.cd(directory);
      this.sftp.rename(oldFileNm, newFileNm);
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值