应公司需求所以百度后完成自己的功能。功能实现的成果参考我的博客:https://blog.csdn.net/qq_39136928/article/details/81981210

实现连接linux服务器的还有ssh2 jar包的session方法操作Linux命令实现下载,但是依赖我下不下,所以用了jsch。如果大家的公司有这个依赖,请参考这篇文章https://blog.csdn.net/hezhihuahzh/article/details/79056455

jsch依赖:

   <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.51</version>
        </dependency>

    大家只需改下配置,也就是ip,username,password。src请求的绝对路径

        

1. 设计一个SshConfiguration类,如下:

/**
 * $Id: SshConfiguration.java,v 1.0 2018/8/15 20:07 潘 Exp $
 * <p>
 * Copyright 2016 Asiainfo Technologies(China),Inc. All rights reserved.
 */
package com.asiainfo.emos.sec.util.TestDownload;

/**
 * @author 潘
 * @version $Id: SshConfiguration.java,v 1.1 2018/8/15 20:07 潘 Exp $
 * Created on 2018/8/15 20:07
 */
public class SshConfiguration {

    private String host;

    private String username;

    private String password;

    private int port;

    public String getHost() {

        return host;

    }

    public void setHost(String host) {

        this.host = host;

    }

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    public int getPort() {

        return port;

    }

    public void setPort(int port) {

        this.port = port;

    }

}

2. 获取linux服务端文件代码如下:

TranscodingAnalyzerDemo:
/**
 * $Id: TranscodingAnalyzerDemo.java,v 1.0 2018/8/15 20:08 潘 Exp $
 * <p>
 * Copyright 2016 Asiainfo Technologies(China),Inc. All rights reserved.
 */
package com.asiainfo.emos.sec.util.TestDownload;

/**
 * @author 潘
 * @version $Id: TranscodingAnalyzerDemo.java,v 1.1 2018/8/15 20:08 潘 Exp $
 * Created on 2018/8/15 20:08
 */

import com.jcraft.jsch.*;

import java.io.*;
import java.util.*;

public class TranscodingAnalyzerDemo {

    private ChannelSftp channelSftp;

    private Session session = null;

    private int timeout = 60000;

    public TranscodingAnalyzerDemo(SshConfiguration conf) throws JSchException {

        System.out.println("try connect to  " + conf.getHost() + ",username: " + conf.getUsername() + ",password: " + conf.getPassword() + ",port: " + conf.getPort());

        //创建JSch对象
        JSch jSch = new JSch();

        //根据用户名,主机ip和端口获取一个Session对象
        session = jSch.getSession(conf.getUsername(), conf.getHost(), conf.getPort());

        //设置密码
        session.setPassword(conf.getPassword());

        Properties config = new Properties();

        config.put("StrictHostKeyChecking", "no");

        //为Session对象设置properties
        session.setConfig(config);

        //设置超时
        session.setTimeout(timeout);

        //通过Session建立连接
        session.connect();

    }

    /**
     * @param src 要下载服务器原路径
     * @param dst 目的路径
     * @throws JSchException
     * @throws SftpException
     * @throws InterruptedException
     * @throws IOException
     */
    public String download(String src, String dst) throws JSchException, SftpException, InterruptedException, IOException {

        //通道
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        //连接通道
        channelSftp.connect();
        //返回文件filename
        String filename = null;
        try {
            String fileName = src.substring(src.lastIndexOf("/") + 1);

                //无论是文件夹还是文件打包下载
                //打开通道,设置通道类型,和执行的命令
                Channel channel = session.openChannel("exec");
                ChannelExec channelExec = (ChannelExec) channel;

                String srcPath = src.substring(0, src.lastIndexOf("/"));

                filename = fileName + "_" + UUID.randomUUID().toString();


                //将"/" 的文件夹打包成名为/run/user/download/filename.zip
                String cmdGet = "zip -q -rj  " + "/run/user/download/" + filename + ".zip " + src;
                channelExec.setCommand("cd " + srcPath + ";" + cmdGet);

                channelExec.setInputStream(null);
                BufferedReader input = new BufferedReader(new InputStreamReader
                        (channelExec.getInputStream()));

                channelExec.connect();
                String line = "";
                while ((line = input.readLine()) != null) {
                    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + line);
            }

                channelSftp.get("/run/user/download/" + filename + ".zip", dst);

                // 关闭通道
                channelExec.disconnect();

                input.close();

                //关闭session
                session.disconnect();


            System.out.println(">>>>>>>>>>  File:" + src + " has downloaded success!");

            channelSftp.quit();

        } catch (Exception e) {

                throw e;
        }


        return filename + ".zip";
    }

    public void close() {

        session.disconnect();

    }

 }

实现的方法,我这里就不贴出来了,毕竟每个公司的业务不一样或者说获取参数的格式不一样,譬如我的公司是获取的是json串,而且要完成客户端下载,如果大家满足这些的话,可以参考我发的第一个地址。

这里我放一个main方法,大家改下参数就行

public static void main(String[] args)

	{

		SshConfiguration configuration=new SshConfiguration();  

	        configuration.setHost("192.168.25.128");  

	        configuration.setUsername("root");  

	        configuration.setPassword("123456");  

	        configuration.setPort(22);//默认端口  

	        try{  

	        
                TranscodingAnalyzerDemo sshUtil=new TranscodingAnalyzerDemo(configuration);

                //下载项目中位置
                String dst = "D:\\yaxin\\work\\emos\\emos-html\\master\\download";

                //我这里返回参数filename,目的是为了客户端下载,如果大家不需要的话,可以将
                //download方法改为void
              
                 String filename = sshUtil.download(src, dst);
	            sshUtil.close();  

	        }catch(Exception e){  

	            e.printStackTrace();  

	        }      

	}

由于拷贝代码问题,可能出现括号少的问题。   注意这里只是完成了打包到指定位置,并不是客户端下载,客户端下载可以参考我的第一个提到的提示。写了个测试页面:大家参考下。

Logo

更多推荐