上传
@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;
}
}