上传下载Linux系统指定目录的文件

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPInputStream;
import ch.ethz.ssh2.SCPOutputStream;

一、上传文件到linux服务器

/**
     * 上传文件到Linux服务器
     *
     * @param hostName              服务器IP地址
     * @param username              服务器用户名
     * @param password              服务器密码
     * @param port                  服务器端口号
     * @param f                     文件对象
     * @param length                文件大小
     * @param remoteTargetDirectory 服务器上传文件的所在路径
     * @param mode                  默认为null
     */
    public static void uploadFile(String hostName, String username, String password, int port, File f, long length, String remoteTargetDirectory, String mode) {
        Connection conn = new Connection(hostName, port);
        SCPOutputStream os = null;
        FileInputStream fis = null;
        try {
            // 连接到主机
            conn.connect();
            // 校验用户名密码
            boolean isAuthenticated = conn.authenticateWithPassword(username,password);
            if(!isAuthenticated){
                logger.error("建立连接失败,用户名或者密码不正确。");
                return ;
            }
            SCPClient scpClient = new SCPClient(conn);
            os = scpClient.put(f.getName(),length,remoteTargetDirectory,mode);
            byte[] b = new byte[4096];
            fis = new FileInputStream(f);
            int i;
            while ((i = fis.read(b)) != -1) {
                os.write(b, 0, i);
            }
            os.flush();
            logger.info("上传文件成功。文件名:" + f.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            conn.close();
        }
    }

二、从linux上下载文件到本地

/**
     * 下载Linux文件到本地
     *
     * @param hostName              服务器IP地址
     * @param username              服务器用户名
     * @param password              服务器密码
     * @param port                  服务器端口号
     * @param remoteFile            服务器上的文件名
     * @param remoteTargetDirectory 服务器上文件的所在路径
     * @param newPath               下载文件的路径
     */
    public static void downloadFile(String hostName, String username, String password, int port, String remoteFile, String remoteTargetDirectory,String newPath){
        Connection conn = new Connection(hostName, port);
        SCPInputStream sis = null;
        FileOutputStream fos = null;
        try {
            // 连接到主机
            conn.connect();
            // 校验用户名密码
            boolean isAuthenticated = conn.authenticateWithPassword(username,password);
            if(!isAuthenticated){
                logger.error("建立连接失败,用户名或者密码不正确。");
                return ;
            }
            SCPClient scpClient = conn.createSCPClient();
            sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);
            File f = new File(newPath);
            if(!f.exists()){
                f.mkdirs();
            }
            File newFile = new File(newPath + remoteFile);
            fos = new FileOutputStream(newFile);
            byte[] b = new byte[4096];
            int i;
            while ((i = sis.read(b)) != -1){
                fos.write(b,0, i);
            }
            fos.flush();
            logger.info("下载文件成功。文件名:" + remoteFile);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                sis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            conn.close();
        }
    }

应用实例:

    public static void main(String[] args) {
        String path = "/usr/temp/test";
        String hostName = "174.22.70.33";
        int port = 22;
        String username = "root";
        String password = "password";
//        downloadFile(hostName,username,password,port,"a",path,"E:/");
        
        File f = new File("E:/new.xml");
        uploadFile(hostName,username,password,port,f,f.length(),path,null);
        
    }

 

2.第三方工具 SecureCRT 点击下方按钮进行拖拽上传

3.在界面使用下载命令和上传命令

前提需要设置好:上传目录和下载目录。

下载:sz xx文件;上传:rz xx文件

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值