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对象
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();进行输出内容