java 文件流base64编码上传到数据库blob类型中

上传

 @PostMapping(value = "/savePhoto")
 public RestResult savePhoto(@RequestBody JSONObject data) {        
        BASE64Decoder decoder = new BASE64Decoder();
        Long id = data.getLong("id");
        String type = data.getString("type");
        try {
            if (type == "zd" || "zd".equals(type)) {
                String zdslt = data.getString("zdslt");
                String zdyxt = data.getString("zdyxt");

                zdslt = zdslt.replaceAll("data:image/png;base64,", "");
                zdyxt = zdyxt.replaceAll("data:image/png;base64,", "");
                byte[] zdsltByte = decoder.decodeBuffer(zdslt);
                byte[] zdyxtByte = decoder.decodeBuffer(zdyxt);
                zdService.saveZdslt(zdsltByte, zdyxtByte, id);
            } else if (type == "h" || "h".equals(type)) {
                String fcfht = data.getString("fcfht");
                fcfht = fcfht.replaceAll("data:image/png;base64,", "");
                byte[] fcfhtByte = decoder.decodeBuffer(fcfht);
                huService.saveZdslt(fcfhtByte, id);
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
            // e.printStackTrace();
        } finally {
        }
        return RestResult.success("上传成功");
    }

	 @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveZdslt(byte[] zdsltByte, byte[] zdyxtByte, Long id) throws Exception {
        Ctt_Zd byId = zdDao.findById(id);
        if (byId == null) {
            throw new BaseException("宗地数据为空,请检查业务数据!");
        }
        Blob zdslt = new SerialBlob(zdsltByte);
        Blob zdyxt = new SerialBlob(zdyxtByte);

        byId.setZdslt(zdslt);
        byId.setZdyxt(zdyxt);

        zdDao.save(byId);
    }

下载

 	@PostMapping(value = "/getPhoto")
    public RestResult getPhoto(@RequestBody JSONObject data) throws Exception {
        Long id = data.getLong("id");
        String type = data.getString("type");
        Object obj = null;
        if (type == "zd" || "zd".equals(type)) {
            obj = zdService.getZdslt(id);
        } else if (type == "h" || "h".equals(type)) {
            obj = huService.getZdslt(id);
        }
        return RestResult.success(obj, "获取成功");
    }

 @Override
    public Object getZdslt(Long id) throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        Ctt_Zd byId = zdDao.findById(id);
        Blob zdslt = byId.getZdslt();
        Blob zdyxt = byId.getZdyxt();
        RestResult res = new RestResult();
        String zdsltStr = ImageUtils.convertBlobToBase64String(zdslt);
        String zdyxtStr = ImageUtils.convertBlobToBase64String(zdyxt);
        zdyxtStr =  zdyxtStr.replaceAll("\r\n","");
        zdsltStr =  zdsltStr.replaceAll("\r\n","");
        Map jsonObject = new HashMap<>();
        jsonObject.put("id", id);
        jsonObject.put("type", "zd");
        jsonObject.put("zdyxt", zdyxtStr);
        jsonObject.put("zdsyt", zdsltStr);
        return jsonObject;
    }


convertBlobToBase64String 方法

 public static String convertBlobToBase64String(Blob blob) {
        String result = "";
        if (null != blob) {
            try {
                InputStream msgContent = blob.getBinaryStream();
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                byte[] buffer = new byte[100];
                int n = 0;
                while (-1 != (n = msgContent.read(buffer))) {
                    output.write(buffer, 0, n);
                }
                result = new BASE64Encoder().encode(output.toByteArray());
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } else {
            return null;
        }
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值