java代码获取linux路径下的图片文件并返回给前端展示
java代码获取linux路径下的图片文件并返回给前端展示
已知图片文件所在的linux目录,如何展示给前端;
// 后端代码
@PostMapping("/getPic")
public void getPic(String ip, HttpServletResponse response) {
response.setContentType("image/jpeg");
//连接linux服务器
Connection conn = new Connection(ip,22);
try {
conn.connect();
//输入连接密码
boolean isAuthenticated = conn.authenticateWithPassword("root", "*******");
//校验密码是否正确
if (isAuthenticated == false) {
System.err.println("authentication failed");
}
//scp执行代码
SCPClient client = new SCPClient(conn);
client.get("/usr/local/123.jpg",response.getOutputStream());
conn.close();
} catch (IOException ex) {
System.out.println("连接服务器失败");
}
}
// scp maven依赖
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
// 前端参考代码片段
//展示标签
<div class="imgbox" v-show="clsooff">
<span class="close" @click="close()"> X</span>
<img width="500" height="400" src="" id="imgs">
</div>
//调用部分
<td @click="getPic(item.ip)">{{item.***}}</td>
//方法区
methods:{
close(){
this.clsooff=false;
},
getPic(ip){
var that=this;
var xhr=new XMLHttpRequest();
xhr.open("post","http://localhost:8080/getPic?ip="+ip,true);
xhr.responseType ="blob";
xhr.onload =function () {
if (this.status==200){
var blob =this.response;
var img = document.getElementById("imgs");
img.onload =function (e) {
window.url.revokeObjectURL(img.src);
};
img.src=window.URL.createObjectURL(blob);
that.clsooff=true;
}
}
xhr.send();
},