博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java实操之使用jcraft进行sftp上传下载文件
阅读量:7206 次
发布时间:2019-06-29

本文共 7003 字,大约阅读时间需要 23 分钟。

  sftp作为临时的文件存储位置,在某些场合还是有其应景的,比如对账文件存放。需要提供一个上传的工具类。实现方法参考下:

pom.xml中引入类库:

com.jcraft
jsch
0.1.54

sftp工具类和使用命令行是一样的操作方法,上传下载如下:

public class SftpUploadTest {    private ChannelSftp sftp = null;    private Session sshSession = null;    private String username;    private String password;    private String host;    private int port;    public SftpUploadTest(String username, String password, String host, int port) {        this.username = username;        this.password = password;        this.host = host;        this.port = port;    }    /**     * 连接sftp服务器     *     * @return ChannelSftp sftp连接实例     */    public ChannelSftp connect() {        info("-->sftp连接开始>>>>>> " + host + ":" + port + " >>>username=" + username);        JSch jsch = new JSch();        try {            jsch.getSession(username, host, port);            sshSession = jsch.getSession(username, host, port);            sshSession.setPassword(password);            Properties properties = new Properties();            properties.put("StrictHostKeyChecking", "no");            sshSession.setConfig(properties);            sshSession.connect();            Channel channel = sshSession.openChannel("sftp");            channel.connect();            sftp = (ChannelSftp) channel;            info(" ftp Connected to " + host + ":" + port);        } catch (JSchException e) {            throw new RuntimeException("sftp连接失败", e);        }        return sftp;    }    /**     * 下载单个文件,如果指定文件名,则下载到文件名否则保持原有文件名     *     * @param remoteFilePath    远程文件路径 /tmp/xxx.txt || xxx.txt.zip     * @param localFilePath     本地文件路径 如 D:\\xxx.txt     * @return 下载的文件     */    public File downloadFile(String remoteFilePath, String localFilePath) {        info(">>>>>>>>>downloadFile--ftp下载文件" + remoteFilePath + "开始>>>>>>>>>>>>>");        connect();        String remoteFileName = "";        // 远端目录确定以 / 作为目录格式        String rFileSeparator = "/";        int rDirNameSepIndex = remoteFilePath.lastIndexOf(rFileSeparator) + 1;        String rDir = remoteFilePath.substring(0, rDirNameSepIndex);        remoteFileName = remoteFilePath.substring(rDirNameSepIndex);        if(localFilePath.endsWith(File.separator)) {            localFilePath = localFilePath + (localFilePath.endsWith(File.separator) ? remoteFileName : "/" + remoteFileName);        }        File file = null;        OutputStream output = null;        try {            file = new File(localFilePath);            if (file.exists()) {                file.delete();            }            file.createNewFile();            sftp.cd(rDir);            output = new FileOutputStream(file);            sftp.get(remoteFileName, output);            info("===DownloadFile:" + remoteFileName + " success from sftp.");        } catch (SftpException e) {            error("ftp下载文件失败", e);        } catch (FileNotFoundException e) {            error("本地目录异常,请检查" + file.getPath(), e);        } catch (IOException e) {            error("创建本地文件失败" + file.getPath(), e);        } finally {            if (output != null) {                try {                    output.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            disconnect();        }        info(">>>>>>>>>downloadFile--ftp下载文件结束>>>>>>>>>>>>>");        return file;    }    /**     * 上传单个文件,如果指正下载文件名则使用,否则保留原有文件名     *     * @param remoteFilePath    远程文件路径 /tmp/xxx.txt ||xxx.txt.zip     * @param uploadFilePath    要上传的文件 如:D:\\test\\xxx.txt     */    public void uploadFile(String remoteFilePath, String uploadFilePath) {        info(" begin uploadFile from:" + uploadFilePath +                ", to: " + remoteFilePath);        FileInputStream in = null;        connect();        String remoteFileName = "";        String remoteDir = remoteFilePath;        String localFileName = "";        // 远端目录确定以 / 作为目录格式        String rFileSeparator = "/";        if(remoteFilePath.endsWith(rFileSeparator)) {            localFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf(File.separator) + 1);            remoteFileName = localFileName;        } else {            int fileNameDirSep = remoteFilePath.lastIndexOf(rFileSeparator) + 1;            remoteDir = remoteFilePath.substring(0, fileNameDirSep);            remoteFileName = remoteFilePath.substring(fileNameDirSep);        }        try {            sftp.cd(remoteDir);        } catch (SftpException e) {            try {                sftp.mkdir(remoteDir);                sftp.cd(remoteDir);            } catch (SftpException e1) {                error("ftp创建文件路径失败,路径为" + remoteDir);                throw new RuntimeException("ftp创建文件路径失败" + remoteDir);            }        }        File file = new File(uploadFilePath);        try {            in = new FileInputStream(file);            sftp.put(in, remoteFileName);        } catch (FileNotFoundException e) {            error("文件不存在-->" + uploadFilePath);        } catch (SftpException e) {            error("sftp异常-->", e);        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e) {                    info("Close stream error." + e.getMessage());                }            }            disconnect();        }        info(">>>>>>>>>uploadFile--ftp上传文件结束>>>>>>>>>>>>>");    }    /**     * 关闭连接     */    public void disconnect() {        if (this.sftp != null) {            if (this.sftp.isConnected()) {                this.sftp.disconnect();                this.sftp = null;                info("sftp 连接已关闭!");            }        }        if (this.sshSession != null) {            if (this.sshSession.isConnected()) {                this.sshSession.disconnect();                this.sshSession = null;                info("sshSession 连接已关闭!");            }        }    }    private void info(String msg) {        System.out.println("info: " + msg);    }    private void error(String msg) {        error(msg, null);    }    private void error(String msg, Throwable e) {        System.out.println("error: " + msg);        if(e != null) {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception {        SftpUploadTest uploadTest = new SftpUploadTest("root", "123", "10.40.10.17", 52766);        uploadTest.uploadFile("/tmp/" + "readme-" + System.currentTimeMillis() + ".txt", "C:\\Users\\xx\\Desktop\\sample\\readme.txt");        uploadTest.downloadFile("/tmp/readme.txt", "C:\\Users\\xx\\Desktop\\sample\\readme-" + System.currentTimeMillis() + ".txt");    }}

 

  使用时,可根据需要进行连接的适时释放!也可能为了安全需要,添加一些额外的安全指令!

  sftp文件操作的命令罗列如下参考:

sftp常用的为上传下载

# get# 从远程服务器上下载一个文件存放到本地,如下:# 先通过lcd切换到本地那个目录下,然后通过get file>> lcd d:\ #表示切换到本地的d盘下>> get ./test.sql   #这样就将当前文件下载本地的d盘下# put# 是将本地的文件上传到远程服务器上,如下:>> put #在windows下弹出选择文件的窗口# lcd# 先通过lcd切换到本地那个目录下>> lcd c:\ #表示切换到本地的c盘下# lls#显示当前目录下的所有文件>> pwd        #显示当前目录

 

转载地址:http://qxoum.baihongyu.com/

你可能感兴趣的文章
【总结整理】JQuery基础学习---动画
查看>>
A. Round House_数学问题
查看>>
LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
查看>>
0920编译原理词法分析
查看>>
浅谈数位DP的dfs写法
查看>>
Linux下的bash对拍
查看>>
C++ STL标准入门
查看>>
移动端页面制作2
查看>>
UE4中的常量材质实例和动态材质实例
查看>>
php中的字符串和正则表达式
查看>>
SharePoint获取UserProfile的信息
查看>>
[题解]第十一届北航程序设计竞赛预赛——I.神奇宝贝大师
查看>>
Python--关于连接符+
查看>>
office 所有后缀对应的 content-type
查看>>
Linux 第十四天
查看>>
第4章 基本TCP套接口编程
查看>>
Android Studio 1.1 使用介绍及导入 jar 包和第三方依赖库
查看>>
Mysql主键、外键和索引的区别
查看>>
struts基础
查看>>
块状链表 codevs 2333弹飞绵羊
查看>>