• 19.对图片进行base64编码,存入数据库


    1.工具类

    package com.atguigu.yygh.common.util;
    
    import org.apache.commons.codec.binary.Base64;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    public class ImageBase64Util {
    
        public static void main(String[] args) {
            String imageFile= "G:\0722test\111.png";// 待处理的图片
            System.out.println(getImageString(imageFile));
        }
    
        public static String getImageString(String imageFile){
            InputStream is = null;
            try {
                byte[] data = null;
                is = new FileInputStream(new File(imageFile));
                data = new byte[is.available()];
                is.read(data);
                return new String(Base64.encodeBase64(data));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != is) {
                    try {
                        is.close();
                        is = null;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return "";
        }
    }

    测试

    <img src="data:image/png;base64,iVBORw0..>,改文件名后缀

    4.接口

     @ApiOperation(value = "上传医院")
        @PostMapping("saveHospital")
        public Result saveHospital(HttpServletRequest request) throws IOException {
            JSONObject json= HttpRequestPost.getRequestJsonObject(request);
            Map<String, Object> params = JSONObject.parseObject(json.toJSONString(), new TypeReference<Map<String, Object>>(){});
    //必须参数校验 略
            String hoscode = (String)params.get("hoscode");
            if(StringUtils.isEmpty(hoscode)) {
                throw new YyghException(ResultCodeEnum.PARAM_ERROR);
            }
    //传输过程中“+”转换为了“ ”,因此我们要转换回来
            String logoDataString = (String)params.get("logoData");
            if(!StringUtils.isEmpty(logoDataString)) {
                String logoData = logoDataString.replaceAll("", "+");
                params.put("logoData", logoData);
            }
    
    
    
    //签名校验
            if(!HttpRequestHelper.isSignEquals(params, hospitalSetService.getSignKey(hoscode))) {
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
          //  Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
    
            hospitalService.save(params);
            return Result.ok();
        }
  • 相关阅读:
    mongodb数据类型
    Pycharm2020.1 破解教程
    酱茄主题(资讯/社区WordPress主题)正式发布
    WordPress社区商城小程序“酱茄pro小程序”V1.7.8发布
    SpringBoot 的@Value注解真是太强了,谁用谁说爽!
    python3_String复习
    Core Data的简单实用
    git submodule
    区间修改主席树
    快速数论变换ntt
  • 原文地址:https://www.cnblogs.com/javakangkang/p/15098551.html
Copyright © 2020-2023  润新知