DataClob.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package jnpf.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.sql.Clob;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. /**
  8. * 在线详情编辑工具类
  9. *
  10. * @author JNPF开发平台组
  11. * @version V3.2
  12. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  13. * @date 2021/10/27
  14. */
  15. @Slf4j
  16. public class DataClob {
  17. public static List<Map<String, Object>> swapClob(List<Map<String, Object>> list) {
  18. if (list == null || list.isEmpty()) {
  19. return list;
  20. }
  21. List<Map<String, Object>> result = new ArrayList<>();
  22. for (int i = 0; i < list.size(); i++) {
  23. Map<String, Object> map = list.get(i);
  24. for (String key : map.keySet()) {
  25. swapClob(map, key);
  26. }
  27. result.add(map);
  28. }
  29. return result;
  30. }
  31. public static void swapClob(Map<String, Object> map, String key) {
  32. if (map != null && map.get(key) != null && map.get(key) instanceof Clob) {
  33. Clob clob = (Clob) map.get(key);
  34. StringBuilder sb = new StringBuilder();
  35. // 获取CLOB字段的内容长度
  36. int length = 0;
  37. // 以流的形式读取CLOB字段的内容
  38. try (java.io.Reader reader = clob.getCharacterStream()) {
  39. length = (int) clob.length();
  40. char[] buffer = new char[length];
  41. int bytesRead;
  42. // 逐个字符读取并添加到字符串构建器中
  43. while ((bytesRead = reader.read(buffer)) != -1) {
  44. sb.append(buffer, 0, bytesRead);
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. map.put(key, sb.toString());
  50. }
  51. }
  52. }