您的位置:首页 > Web前端 > HTML

html里面怎么展示绝对路径的图片

2017-10-11 17:12 591 查看
我这边遇到一个问题,spring boot 打jar包,图片路径会出问题

所以这边最后讨论出一个解决方案。图片设置到当前项目的一个路径下面,然后通过二进制流访问图片

为什么要这样子写呢?因为我们的图片地址是一个绝对路径展示,而在html上面,是不能展示绝对路径的。

所以我们通过二进制流来访问图片地址

@RequestMapping("/getImage")
public void getImage(HttpServletRequest request,HttpServletResponse response) {
//获取图片地址 如aa/aa.jpg这样子
String image = request.getParameter("image") == null ? "" : request.getParameter("image");
String path=getPathNo();
String filePath=path+image;
FileInputStream fileInputStream=null;
OutputStream outputStream =null;
try{
File file=new File(filePath);
if(file.exists()){
fileInputStream =new FileInputStream(file);
outputStream= new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024 * 8];
int count=0;
while ((count = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
outputStream.flush();
}
}
}catch (Exception e){

}finally {
try {
fileInputStream.close();
outputStream.close();
response.flushBuffer();
}catch (Exception e){

}
}
}
//获取当前项目的路径 拼成一个绝对路径
public    String getPathNo(){
String path=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if(path.toUpperCase().indexOf(".jar".toUpperCase())!=-1){
String StrPath=path.substring(0, path.toUpperCase().indexOf(".jar".toUpperCase()));
path=StrPath.substring(0,StrPath.lastIndexOf("/")+1).replaceAll("file:/", "").replaceAll("%20", " ").trim() ;
}else{
path=path.substring(0,path.lastIndexOf("/")+1).replaceAll("file:/", "").replaceAll("%20", " ").trim() ;
}
return path;
}


页面中

return "<img src='"+sy()+"/getImage?image="+value+"'  style='width: 40px;height: 40px;' />";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html