SpringBoot+Vue项目回显本地图片

这篇博客探讨了如何实现后端接口返回图片并由前端展示,以及在小程序中考虑使用Base64加密进行图片处理。后端通过HttpServletResponser读取并发送图片数据,前端使用axios以arraybuffer响应类型获取,并转换为Blob对象展示。同时,文章提及了前端展示时防止图片变形的技巧。
摘要由CSDN通过智能技术生成

记录公司项目回显二维码,这里只是浏览器展示,小程序可以考虑使用base64加密。
1.后端接口

@RequestMapping("/viewPic")
    public void viewPic(HttpServletResponse response) throws Exception{
        File file = new  File("d:"+ File.separator+"code"+ File.separator + "nowait.jpg");
        if (file.exists()){
            FileInputStream is = new FileInputStream(file);
            int i = is.available(); // 得到文件大小
            byte data[] = new byte[i];
            is.read(data); // 读数据
            is.close();
            response.setContentType("image/*"); // 设置返回的文件类型
            OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
            toClient.write(data); // 输出数据
            toClient.close();
        }
    }

2.前端请求

        data(){
            return{
                captchaImg:""
            };
        },
            getPic(){
                request({
                    api: "BAPI",
                    url: "/qrcode/viewPic",
                    method: "get",
                    responseType: 'arraybuffer',//二进制响应类型
                }).then(res=>{
                    console.log(res)
                    const blob = new Blob([res], {
                        type: 'application/png;charset=utf-8',
                    });
                    const url = window.URL.createObjectURL(blob);
                    this.captchaImg = url
                }).catch(err => {
                    console.log(err);
                });

            }

3.前端展示
这里为了不让图片变形只设置了宽度

<img :src="captchaImg" alt="未上传二维码" width="250px" style="margin-left: 50%;margin-top: 100px">
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值