直播app系统源码中加载图片的三种方式
直播APP系统源码中,加载图片可以很好的提高用户体验,图片预先加载出来,可以方便用户更好的去观看,避免很长的等待时间,让用户更加快速冲浪,本文将为大家分享三个直播app系统源码中加载图片的方式。
方式一:直播app系统源码中src指向图像的位置
最常用的一种方式,无需搭配后端代码
1 | <img src= "img/boat.gif" alt= "Big Boat" > |
方式二:直播app系统源码中src执行后台路径,获取图片的字节数组
前端代码
1 | <img src= "/getImage" alt= "Big Boat" > |
后端代码
1 2 3 4 5 6 7 | @GetMapping ( "getImage" ) public void image(HttpServletResponse response) throws IOException { try (InputStream input = new FileInputStream( "D:\\个人资料\\图片\\Picture\\lf.jpg" ); OutputStream output = response.getOutputStream()){ output.write(input.readAllBytes()); } } |
方式三:直播app系统源码中获取图片字节数组的base64编码(字符串)
前端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" > <title>Title</title> <script src= "/static/js/jquery.js" ></script> <style> .main{ display: flex; } .imgdiv{ text-align: center; margin: 5px; } .imgdiv p{ font-weight: bold; font-size: 22px; } img{ width: 200px; height: 250px; } </style> </head> <body> <div class = "main" > <div class = "imgdiv" > <img id= "img1" src= "" > </div> <div class = "imgdiv" > <img id= "img2" src= "" > </div> </div> </body> <script> $(document).ready(function () { var params = { "img1" : "dog.jpg" , "img2" : "cat.jpg" }; fetch( "/getImage" , { method: 'POST' , headers: { 'Content-Type' : 'application/json;charset=UTF-8' }, body: JSON.stringify(params) }).then(res => res.json()).then(res => { for (var p in res) { $( "#" + p)[ 0 ].src = "data:image/jpg;base64," + res[p]; } }) }); </script> </html> |
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @PostMapping ( "getImage" ) public Map<String, String> image( @RequestBody Map<String, String> map) throws Exception { String baseImgUrl = "D:\\个人资料\\图片\\Picture\\" ; Map<String, String> imageMap = new HashMap<>(); for (Map.Entry<String, String> entry : map.entrySet()) { String imageId = entry.getKey(); // 图片名称 String imageName = entry.getValue(); // 最终图片路径 String imgUrl = baseImgUrl + imageName; try (InputStream input = new FileInputStream(imgUrl)) { String b64encoded = Base64.getEncoder().encodeToString(input.readAllBytes()); imageMap.put(imageId, b64encoded); } } return imageMap; } |
以上就是 直播app系统源码中加载图片的三种方式,更多内容欢迎关注之后的文章。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现