base64编码后的图片如何获取它的宽和高

具体获取宽和高的方法:
在这里插入图片描述

前端传过来的是base64编码后的图片
在这里插入图片描述
在这里插入图片描述
具体代码如下:(不适合直接copy,可以简单看一下,很简单)

public com.tfiir.srl.framework.ResponseEntity<UploadBase64Response> upload(@RequestBody UploadBase64Request request) {
        UploadBase64Response uploadBase64Response = new UploadBase64Response();
        try {

            String base64Pic = request.getBase64();
            String data = base64Pic.substring(base64Pic.indexOf(",")+1);
            System.out.println(data);
            //文件存放按日期文件夹存放
            Date time=new Date();
            String dateUrl=new SimpleDateFormat("YYYY").format(time)+"/"+new SimpleDateFormat("MM").format(time)+"/"+new SimpleDateFormat("dd").format(time)+"/";
//            data = "JXU4RkQ5JXU5MUNDJXU2NjJGJXU4OTgxJXU1MkEwJXU1QkM2JXU3Njg0JXU1MTg1JXU1QkI5JXVGRjAxc2FkZmE=";


            byte[] strBase64 = new BASE64Decoder().decodeBuffer(data);
            try {
                InputStream is = new ByteArrayInputStream(strBase64);
                BufferedImage image = ImageIO.read(is);
                System.out.println(image.getHeight());
                System.out.println(image.getWidth());
            }catch (Exception e){
                e.printStackTrace();
            }
            FileOutputStream fis = null;
            BufferedInputStream bis = null;


            String fileType = "";
            if (request.getFileType() != null){
                fileType = "." + request.getFileType();
            }

            String filename =UUID.randomUUID().toString() + fileType;
            logger.info(filename);
            File file = new File("/home/site/file/" +dateUrl+ filename);

            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }


            try {
                fis = new FileOutputStream(file);
                fis.write(strBase64, 0, strBase64.length);
                uploadBase64Response.setFileUrl(fileUrl+dateUrl+filename);
                System.out.println("success");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            return com.tfiir.srl.framework.ResponseEntity.success(uploadBase64Response);
        } catch (Exception e) {
            return ResponseEntity.error(500,e.getMessage());
        }
    }
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值