代码:
/*
* 根据参数路径获取图片并下载
* */
@GetMapping("/imgDownload")
public void getPic(String ip, HttpServletResponse response, String path) {
//服务器发送给浏览器的数据类型 (不写setHeadder直接打开)
response.setContentType("image/png");
//设置以下载方式打开文件
response.setHeader("content-disposition", "attachment;filename=back.png");
//连接linux服务器
Connection conn = new Connection("49.92.153.134", 22);
try {
conn.connect();
//输入连接密码
boolean isAuthenticated = conn.authenticateWithPassword("root", "G1123");
//校验密码是否正确
if (isAuthenticated == false) {
System.err.println("authentication failed");
}
//scp执行代码
SCPClient client = new SCPClient(conn);
client.get(path, response.getOutputStream());
conn.close();
} catch (IOException ex) {
System.out.println("连接服务器失败");
}
}
所需依赖:
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>