集群环境下载日志文件

1.可以用ch.ethz.ssh2.SCPClient进行linux操作,核心是利用linux的命令进行文件操作。举个例子:

Connection conn = new Connection("IP","端口");

conn.connect();

boolean isAuthenticated = conn.authenticateWithPassword("用户名","密码");
    if (isAuthenticated == false) {
                  System.err.println("authentication failed");

              }

ch.ethz.ssh2.Session sess = conn.openSession();
sess.execCommand("ls "+"文件路径");
InputStream stdout = new StreamGobbler(sess.getStdout());  

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

2.可以用com.jcraft.jsch.ChannelSftp用sftp进行下载,举个例子:

com.jcraft.jsch.Session session = null;

com.jcraft.jsch.ChannelSftp channelSftp = null;

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

session.setPassword(passward); //设置密码    
Properties config=new Properties();    
config.put("StrictHostKeyChecking", "no");    
session.setConfig(config);//为Session对象设置properties    
session.setTimeout(timeout);//设置超时    
session.connect();//通过Session建立连接 
channelSftp=(ChannelSftp) session.openChannel("sftp");
channelSftp.connect();//sftp连接
channelSftp.get(src,dst); //src源文件 dst可以是目标路径也可以是IO流
channelSftp.quit();  //关闭连接
session.disconnect(); //关闭连接

3.上面两种方法都需要知道LINUX用户名密码端口IP,但其实我们也开放一个对外接口出来,定位此接口CLASS文件的位置,然后截取和拼接出LOGS文件的路径出来,通过IO流进行获取下载。

//拼接logs路径

String path = this.getClass().getClassLoader().getResource(".").getPath();

String websPath = StringUtils.substringBeforeLast(path, "webapps");

String logsPath = websPath+ "logs/";

然后用IO流进行文件读取,注意设置HttpServletResponse.setCharacterEncoding()编码格式以及IO流读取格式

读取后用response.getOutputStream()进行输出;

然后用httpcilent访问接口地址,注意HttpServletResponse设置:

HttpServletResponse.setContentType("application/octet-stream;charset=UTF-8");

HttpServletResponse.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode(fileName, "UTF-8"));

HttpServletResponse.getOutputStream().write();进行输出内容

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值