使用JSch实现SFTP文件传输

maven依赖:

        <!-- FTP -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.51</version>
        </dependency>
        <!-- jsch 所依赖的jar包  -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jzlib</artifactId>
            <version>1.0.7</version>
        </dependency>

代码:



import com.jcraft.jsch.*;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class FtpJSch {

    private static ChannelSftp sftp = null;
    private static Session sshSession = null;
    private static Channel channel = null;


    public static FtpJSch getConnect(String host, String port, String userName, String password)
            throws JSchException {
        FtpJSch ftp = new FtpJSch();
        JSch jsch = new JSch();

        //获取sshSession  账号-ip-端口
        sshSession = jsch.getSession(userName, host, Integer.parseInt(port));
        //添加密码
        sshSession.setPassword(password);
        Properties sshConfig = new Properties();
        //严格主机密钥检查
        sshConfig.put("StrictHostKeyChecking", "no");

        sshSession.setConfig(sshConfig);
        //开启sshSession链接
        sshSession.connect();
        //获取sftp通道
        channel = sshSession.openChannel("sftp");
        //开启
        channel.connect();

        sftp = (ChannelSftp) channel;
        return ftp;
    }

    /**
     * @param in        输入流
     * @param directory 上传文件的路径
     * @return fileName 服务器上文件名
     */
    public void upload(InputStream in, String directory, String fileName) throws SftpException {
        sftp.cd(directory);
        sftp.put(in, fileName);
    }

    /**
     * @param directory 上传文件的路径
     * @return fileName 服务器上文件名
     */
    public OutputStream upload(String directory, String fileName) throws SftpException {
        sftp.cd(directory);
        return sftp.put(fileName);
    }

    /**
     * 下载文件
     *
     * @param directory        要下載的文件路徑
     * @param downloadFileName 下载的文件名
     * @param os               下载文件输出
     */
    public void download(String directory, String downloadFileName, OutputStream os) throws
            SftpException {

        sftp.cd(directory);
        sftp.get(downloadFileName, os);

    }

    /***
     * 下载到本地服务器
     * 2018年5月9日
     * directory: 指定目录
     * downloadFileName :文件名称
     * dest: 本地目录
     */

    public void download(String directory, String downloadFileName, String dest) throws
            SftpException {

        sftp.cd(directory);
        sftp.get(directory + downloadFileName, dest);

    }

    public void close() {
        if (sftp != null) {
            sftp.disconnect();
        }
        if (channel != null) {
            channel.disconnect();
        }
        if (sshSession != null) {
            sshSession.disconnect();
        }
    }
}



1、JSch中文乱码处理
     使用jsch-0.1.51进行SFTP文件传输时,对中文处理会出现乱码,并且也无法通过setFileNameEncoding()方法来设置编码。
     解决方案:
         下载jsch-0.1.51源代码,在ChannelSFTP.java文件中找到SENDINIT( )方法,修改红色部分的内容
           private void sendINIT() throws Exception {
    this.packet.reset();
    putHEAD((byte)1, 5);
    this.buf.putInt(3);   //修改为  this.buf.putInt(2);
    getSession().write(this.packet, this, 9);
  }
 
然后编译并更改jar中的对应class文件即可。

2、ChannelSftp类的主要API说明如下:

方法名

方法说明

 

public void put(String src, String dst)

将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。采用默认的传输模式:OVERWRITE

 

public void put(String src, String dst, int mode)

将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。指定文件传输模式为modemode可选值为:ChannelSftp.OVERWRITEChannelSftp.RESUMEChannelSftp.APPEND)。

 

public void put(String src, String dst, SftpProgressMonitor monitor)

将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。采用默认的传输模式:OVERWRITE,并使用实现了SftpProgressMonitor接口的monitor对象来监控文件传输的进度。

 

public void put(String src, String dst,SftpProgressMonitor monitor, int mode) 

将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。指定传输模式为mode,并使用实现了SftpProgressMonitor接口的monitor对象来监控文件传输的进度。

 

public void put(InputStream src, String dst)

将本地的input stream对象src上传到目标服务器,目标文件名为dstdst不能为目录。采用默认的传输模式:OVERWRITE

 

public void put(InputStream src, String dst, int mode)

将本地的input stream对象src上传到目标服务器,目标文件名为dstdst不能为目录。指定文件传输模式为mode

public void put(InputStream src, String dst, SftpProgressMonitor monitor)

将本地的input stream对象src上传到目标服务器,目标文件名为dstdst不能为目录。采用默认的传输模式:OVERWRITE。并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。

public void put(InputStream src, String dst,SftpProgressMonitor monitor, int mode) 

将本地的input stream对象src上传到目标服务器,目标文件名为dstdst不能为目录。指定文件传输模式为mode。并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。

public OutputStream put(String dst)

该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dstdst不能为目录。采用默认的传输模式:OVERWRITE

public OutputStream put(String dst, final int mode)

该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dstdst不能为目录。指定文件传输模式为mode

public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode) 

该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dstdst不能为目录。指定文件传输模式为mode。并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。

public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode, long offset)

该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dstdst不能为目录。指定文件传输模式为mode。并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。offset指定了一个偏移量,从输出流偏移offset开始写入数据。

get(String src, String dst)

下载文件到本地,src为目标服务器上的文件,不能为目录,dst为本地文件路径。若dst为目录,则本地文件名与目标服务器上的文件名一样。

get(String src, String dst ,SftpProgressMonitor monitor)

get(String src, String dst),只是该方法允许传入传输进度的监控对象monitor

get(String src, String dst ,SftpProgressMonitor monitor ,int mode)

get(String src, String dst ,SftpProgressMonitor monitor),同时,该方法增加了mode参数,允许指定文件传输模式

rm(String path)

删除文件,path不能为目录,删除目录使用rmdir

rmdir(String path)

删除目录,但是只能删除空目录

rename(String oldpath, String newpath)

如果oldPath为目录,不要求目录必须为空
如果newpath为目录,则newpath必须不能存在,如果已经存在该目录,则会出现重名或者移动失败
1
、重命名文件或者目录
2
、移动文件或者目录

ls(String path)

列出指定目录下的所有文件和子目录。该方法返回Vector对象,该列表具体存放的是LsEntry对象

 



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值