spring boot图片上传,存到数据库

/**
     *  图片上传
      */
    public Map uploading(MultipartFile file, String code) throws RuntimeException {
        System.out.println("请求进来了");
        Object obj = redisTemplate.opsForValue().get(code);
        UserInfo userInfo = JSON.parseObject(JSON.toJSONString(obj), UserInfo.class);
        String openid = userInfo.getOpenId();

        Map map = new HashMap<>();
    if (file == null || file.isEmpty()) {
        map.put("result",false);
        return map;
    }
//            openid="oLZ0d61_0pX8KsoyERE8X4uYeXSs";
        // 查询数据库以检查用户是否存在
        Student existingStudent = findByOpenid(openid);
            if (existingStudent == null) {
               map.put("result",false);
                return map;
            }

        ByteArrayOutputStream out = null;
        try {
            InputStream inputStream = file.getInputStream();
            out = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        } catch (IOException e) {
            map.put("result",false);
            return map;
        }
        byte[] imgByte = out.toByteArray();
        // 更新现有用户的img字段
        existingStudent.setImg(imgByte);
        boolean save = updateStudentInfo(existingStudent);
        map.put("result", save);
        return map;

    }

你可能感兴趣的:(数据库,spring,boot,java,图片上传)