sftp下载服务器文件

标题 sftp下载服务器文件

package com.sinosoft.ireport.jdbcMysql;

import com.jcraft.jsch.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * @program: Report
 * @description: 连接sftp
 * @author: XWJ
 * @create: 2021-03-18 16:09
 **/
public class SftpUtil {

	//手动替换
    private static String host = "XXX";

    private static int port = XX;

    private static String userName = "XXX";

    private static String password = "XXX";

    private static Session sshSession;

    public static ChannelSftp sftpConnection(String host, int port, String userName, String password) {
        JSch jsch = new JSch();
        ChannelSftp channelSftp = new ChannelSftp();
        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();
            channelSftp = (ChannelSftp) channel;
            System.out.println("Sftp服务器登录成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Sftp服务器登录异常!");
        }
        return channelSftp;
    }


    /**
     * @return
     * @description 退出Sftp服务器登录
     **/
    public static void sftpClose(ChannelSftp channelSftp) {
        if (channelSftp != null) {
            if (channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
        }
    }

    /**
     * 关闭session
     */
    public static void sessionClose() {
        if (sshSession != null) {
            if (sshSession.isConnected()) {
                sshSession.disconnect();
                sshSession = null;
            }
        }
    }


    /**
     * 下载sftp文件
     *
     * @param
     * @param newFileName 新文件名称
     * @param path        文件路径
     * @param fileName    文件名称
     * @param downUrl     下载到本地的路径
     * @throws Exception
     */
    public static void downSftpFile(String newFileName, String path, String fileName, String downUrl) {
        ChannelSftp channelSftp = new ChannelSftp();
        try {
            channelSftp = sftpConnection(host, port, userName, password);

            downSftpFile(channelSftp, newFileName, path, fileName, downUrl);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sftpClose(channelSftp);
            sessionClose();
        }
    }

    private static void downSftpFile(ChannelSftp sftp, String newFileName, String path, String fileName, String downUrl) throws Exception {

        OutputStream os = null;
        try {
            File localFile = new File(downUrl + "/" + newFileName);
            if (!localFile.getParentFile().exists()) {
                localFile.getParentFile().mkdirs();
                localFile.createNewFile();
            }

            if (path != null && !"".equals(path)) {
                sftp.cd(path);//进入所在路径
            }
            os = new FileOutputStream(localFile);
            sftp.get(path + fileName, os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     *
     * @param directory  要删除文件所在目录,例如 /home
     * @param deleteFile 要删除的文件,例如 码海无际.txt
     */
    public static boolean delete(String directory, String deleteFile) {
        boolean result = false;
        ChannelSftp sftp = sftpConnection(host, port, userName, password);
        try {
            sftp.cd(directory);
            sftp.rm(deleteFile);
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            sftpClose(sftp);
        }
        result = true;
        return result;
    }

	//测试
    public static void main(String[] args) {
        host = "";
        port = ;
        userName = "";
        password = ";
        ChannelSftp channelSftp = sftpConnection(host, port, userName, password);
        String newFileName = "hahah2021031815305001.xls";
        String downUrl = "C:\\\\111";
        String path = "/u01/report/result/";
        String fileName = "2021031815305001.xls";
        try {
            downSftpFile(channelSftp, newFileName, path, fileName, downUrl);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sftpClose(channelSftp);
            sessionClose();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值