解决图片转base64编码后,查询太慢的问题

文章介绍了如何解决图片转base64编码后查询效率低下的问题。通过将文件ID置于base64编码中,结合文件下载接口直接展示图片,并利用Redis缓存来加速查询。当图片数据不在Redis中时,从数据库获取并存储到Redis,以减少后续请求的延迟。
摘要由CSDN通过智能技术生成


解决图片转base64编码后,查询太慢的问题

解决思路:可以把文件id放在base64编码的位置,通过文件下载的方式进行字符串拼接,在img的src属性直接展示,会去调用下载接口,直接展示图片。(最终效果会是文字内容很快显示,图片会之后挨个显示):
还想再加快速度,可以使用redis

public String getTbFileInmage(@RequestParam("ufId") String tbWjFile, HttpServletResponse response) throws IOException {
        if (redisTemplate.opsForValue().get(redisPrefix + tbWjFile) == null ? true : false) {
                List<TbAppFiles> list = allStaticPageService.selectTbAppFilesList(tbWjFile);
            if (list != null && list.size() != 0) {
                //设置文件路径
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + list.get(0).getFilename());// 设置文件名
                try {
                    String ufBase64 = list.get(0).getFilecontent();
                    OutputStream os = response.getOutputStream();
                    byte[] decode = Base64.getMimeDecoder().decode(ufBase64);
                    os.write(decode);
                    redisTemplate.opsForValue().set(redisPrefix + tbWjFile, ufBase64);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else {
            //设置文件路径
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + tbWjFile);// 设置文件名
            String ufBase64 = (String) redisTemplate.opsForValue().get(redisPrefix + tbWjFile);
            OutputStream os = response.getOutputStream();
            byte[] decode = Base64.getMimeDecoder().decode(ufBase64);
            os.write(decode);

        }
        return "下载失败";
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值