html里面怎么展示绝对路径的图片
2017-10-11 17:12
591 查看
我这边遇到一个问题,spring boot 打jar包,图片路径会出问题
所以这边最后讨论出一个解决方案。图片设置到当前项目的一个路径下面,然后通过二进制流访问图片
为什么要这样子写呢?因为我们的图片地址是一个绝对路径展示,而在html上面,是不能展示绝对路径的。
所以我们通过二进制流来访问图片地址
页面中
所以这边最后讨论出一个解决方案。图片设置到当前项目的一个路径下面,然后通过二进制流访问图片
为什么要这样子写呢?因为我们的图片地址是一个绝对路径展示,而在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;' />";
相关文章推荐
- <input type="file" 上传图片不是绝对路径 而是相对路径 怎么解决?
- html<img>标签中src=“图片路径”,怎么用变量替换路径
- 怎么取到字符串里面图片路径
- [HTML] 图片标签、绝对路径 和 相对路径
- HTML:使用绝对路径插入图片后在浏览器中无法显示的问题
- [HTML] 图片标签、绝对路径 和 相对路径
- vc 加载绝对路径图片的程序怎么能在别人的机子上运行
- thinkphp,js文件里面怎么引入thinkphp系统变量.类似图片路径
- HTML 里面的图片怎么移动位置?
- html图片的绝对路径和相对路径
- html img动态读取图片,图片路径可以为服务器电脑上的绝对路径
- html图片的绝对路径和相对路径
- 在html页面中预览图片获取本地的绝对路径
- 使用Html.fromHtml()怎么加载Html中的图片
- HTML中的相对路径与绝对路径
- 解决上传图片预览时获取绝对路径问题
- 怎么删除eclipse启动时工作空间里面的路径选项(如何启动eclipse时提示选择工作空间||删除workspace空间的目录)
- 怎么在CAD里面添加图片
- 相对路径、绝对路径、部署路径(html/css/servlet/jsp)
- Android获取drawable目录下的图片绝对路径并转为Bitmap