使用官方提供的ContentStreamUpdateRequest请求,虽然默认为POST请求,但是请求参数还是放在了url中,导致url太长,
    错误信息:

    1. URI is too large >8192

    解决办法:

    1. (治标不治本)修改jetty.xml的参数

      1. <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
      2. ...
      3. <Set name="requestHeaderSize"><Property name="solr.jetty.request.header.size" default="81920" /></Set>
      4. </New>
    2. 自己封装http接口请求

    原代码:

    1. package com.datago.microservice.kb.server.service.impl;
    2. import cn.hutool.core.io.FileUtil;
    3. import cn.hutool.core.util.ObjectUtil;
    4. import cn.hutool.http.HttpUtil;
    5. import com.datago.microservice.kb.common.utils.Func;
    6. import com.datago.microservice.kb.server.entity.Attach;
    7. import com.datago.microservice.kb.server.entity.DistributeAttach;
    8. import com.datago.microservice.kb.server.entity.KnowledgeBaseManager;
    9. import com.datago.microservice.kb.server.enums.ParamEnum;
    10. import com.datago.microservice.kb.server.enums.SolrQueryEnum;
    11. import com.datago.microservice.kb.server.service.IAttachSolrService;
    12. import com.datago.microservice.kb.server.utils.cache.ParamCache;
    13. import com.datago.microservice.kb.server.utils.common.SolrUtil;
    14. import com.datago.microservice.kb.server.utils.common.TypeUtil;
    15. import com.datago.microservice.kb.server.vo.KnowledgeBaseManagerVO;
    16. import lombok.SneakyThrows;
    17. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
    18. import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
    19. import org.apache.solr.common.util.ContentStreamBase;
    20. import org.springframework.beans.factory.annotation.Value;
    21. import org.springframework.scheduling.annotation.Async;
    22. import org.springframework.stereotype.Service;
    23. import org.springframework.web.multipart.MultipartFile;
    24. import java.io.File;
    25. import java.util.*;
    26. import java.util.stream.Collectors;
    27. /**
    28. * 附件
    29. *
    30. * @author xq
    31. * @date 2022/03/21 16:29
    32. * @describe 附件
    33. */
    34. @Service
    35. public class AttachSolrServiceImpl extends BaseSolrServiceImpl<Attach> implements IAttachSolrService {
    36. @Async
    37. @SneakyThrows
    38. @Override
    39. public void putFile(MultipartFile multipartFile, DistributeAttach attach) {
    40. // 文件上传路径
    41. ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
    42. // 复制文件
    43. File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
    44. FileUtil.writeFromStream(multipartFile.getInputStream(), file);
    45. // 获取文件类型
    46. String contentType = TypeUtil.getFileContentType(file.getName());
    47. // 加入文件
    48. up.addFile(file, contentType);
    49. // 添加参数
    50. if (ObjectUtil.isEmpty(attach)) {
    51. return;
    52. }
    53. if (ObjectUtil.isNotEmpty(attach.getId())) {
    54. up.setParam("literal.id", attach.getId().toString());
    55. }
    56. up.setParam("literal.attach_name", attach.getAttachName());
    57. up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());
    58. up.setParam("literal.attach_address", attach.getAttachAddress());
    59. up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());
    60. up.setParam("literal.extension", attach.getExtension());
    61. up.setParam("literal.create_time", attach.getCreateTime().toString());
    62. // up.setParam("literal.createUser", attach.getCreateUser());
    63. // up.setParam("literal.createDept", attach.getCreateDept());
    64. // up.setParam("literal.updateTime", attach.getUpdateTime().toString());
    65. // up.setParam("literal.updateUser", attach.getUpdateUser());
    66. // up.setParam("literal.status", attach.getStatus().toString());
    67. // up.setParam("literal.isDeleted", attach.getIsDeleted().toString());
    68. up.setParam("literal.size", String.valueOf(file.getTotalSpace()));
    69. up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    70. solrClient.request(up, getCore());
    71. }
    72. @Override
    73. public void putFile(List<KnowledgeBaseManagerVO> managerVO) {
    74. if (ObjectUtil.isEmpty(managerVO)) {
    75. return;
    76. }
    77. for (KnowledgeBaseManagerVO vo : managerVO) {
    78. if (ObjectUtil.isEmpty(vo)) {
    79. continue;
    80. }
    81. putFile(vo.getAttach(), vo);
    82. }
    83. }
    84. @Override
    85. public void putFile(List<DistributeAttach> attachList, KnowledgeBaseManagerVO managerVO) {
    86. if (ObjectUtil.isEmpty(managerVO) || ObjectUtil.isEmpty(attachList)) {
    87. return;
    88. }
    89. for (DistributeAttach attach : attachList) {
    90. putFile(attach, managerVO);
    91. }
    92. }
    93. @SneakyThrows
    94. @Override
    95. public void putFile(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {
    96. // 文件上传路径
    97. ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
    98. // 复制文件
    99. File file = new File(attach.getAttachAddress());
    100. // 获取文件类型
    101. String contentType = TypeUtil.getFileContentType(file.getName());
    102. // 加入文件
    103. up.addFile(file, contentType);
    104. // 添加参数
    105. if (ObjectUtil.isEmpty(attach)) {
    106. return;
    107. }
    108. if (ObjectUtil.isNotEmpty(attach.getId())) {
    109. up.setParam("literal.id", attach.getId().toString());
    110. }
    111. up.setParam("literal.attach_name", attach.getAttachName());
    112. up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());
    113. up.setParam("literal.attach_address", attach.getAttachAddress());
    114. up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());
    115. up.setParam("literal.extension", attach.getExtension());
    116. up.setParam("literal.create_time", attach.getCreateTime().toString());
    117. if (ObjectUtil.isNotEmpty(managerVO)) {
    118. up.setParam("literal.kb_id", managerVO.getId().toString());
    119. up.setParam("literal.category_id", managerVO.getCategoryId().toString());
    120. up.setParam("literal.category_name", managerVO.getCategoryName());
    121. up.setParam("literal.category_ancestors", managerVO.getCategoryAncestors());
    122. up.setParam("literal.title", managerVO.getTitle());
    123. up.setParam("literal.content_text", managerVO.getContentText());
    124. up.setParam("literal.label_name", managerVO.getLabelName());
    125. if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {
    126. up.setParam("literal.label_ids", managerVO.getLabelIds());
    127. }
    128. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {
    129. up.setParam("literal.association_module", String.valueOf(managerVO.getAssociationModule()));
    130. }
    131. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {
    132. up.setParam("literal.association_module_name", managerVO.getAssociationModuleName());
    133. }
    134. up.setParam("literal.create_user_name", managerVO.getCreateUserName());
    135. up.setParam("literal.create_user", managerVO.getCreateUser());
    136. up.setParam("literal.create_dept_name", managerVO.getCreateDeptName());
    137. up.setParam("literal.create_dept", managerVO.getCreateDept());
    138. up.setParam("literal.is_deleted", managerVO.getIsDeleted().toString());
    139. }
    140. up.setParam("literal.size", String.valueOf(file.getTotalSpace()));
    141. up.addContentStream(new ContentStreamBase.StringStream(""));
    142. up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    143. solrClient.request(up, getCore());
    144. }
    145. @Override
    146. public String getCore() {
    147. String value = ParamCache.getValue(ParamEnum.ATTACH.getKey());
    148. return ObjectUtil.isEmpty(value) ? ParamEnum.ATTACH.getValue() : value;
    149. }
    150. }

    后修改为下方代码:

    1. package com.datago.microservice.kb.server.service.impl;
    2. import cn.hutool.core.io.FileUtil;
    3. import cn.hutool.core.util.ObjectUtil;
    4. import cn.hutool.http.HttpUtil;
    5. import com.datago.microservice.kb.common.utils.Func;
    6. import com.datago.microservice.kb.server.entity.Attach;
    7. import com.datago.microservice.kb.server.entity.DistributeAttach;
    8. import com.datago.microservice.kb.server.entity.KnowledgeBaseManager;
    9. import com.datago.microservice.kb.server.enums.ParamEnum;
    10. import com.datago.microservice.kb.server.enums.SolrQueryEnum;
    11. import com.datago.microservice.kb.server.service.IAttachSolrService;
    12. import com.datago.microservice.kb.server.utils.cache.ParamCache;
    13. import com.datago.microservice.kb.server.utils.common.SolrUtil;
    14. import com.datago.microservice.kb.server.utils.common.TypeUtil;
    15. import com.datago.microservice.kb.server.vo.KnowledgeBaseManagerVO;
    16. import lombok.SneakyThrows;
    17. import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
    18. import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
    19. import org.apache.solr.common.util.ContentStreamBase;
    20. import org.springframework.beans.factory.annotation.Value;
    21. import org.springframework.scheduling.annotation.Async;
    22. import org.springframework.stereotype.Service;
    23. import org.springframework.web.multipart.MultipartFile;
    24. import java.io.File;
    25. import java.util.*;
    26. import java.util.stream.Collectors;
    27. /**
    28. * 附件
    29. *
    30. * @author xq
    31. * @date 2022/03/21 16:29
    32. * @describe 附件
    33. */
    34. @Service
    35. public class AttachSolrServiceImpl extends BaseSolrServiceImpl<Attach> implements IAttachSolrService {
    36. @Value("${spring.data.solr.host}")
    37. private String host;
    38. @Async
    39. @SneakyThrows
    40. @Override
    41. public void putFile(MultipartFile multipartFile, DistributeAttach attach) {
    42. // 文件上传路径
    43. ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
    44. // 复制文件
    45. File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
    46. FileUtil.writeFromStream(multipartFile.getInputStream(), file);
    47. // 获取文件类型
    48. String contentType = TypeUtil.getFileContentType(file.getName());
    49. // 加入文件
    50. up.addFile(file, contentType);
    51. // 添加参数
    52. if (ObjectUtil.isEmpty(attach)) {
    53. return;
    54. }
    55. if (ObjectUtil.isNotEmpty(attach.getId())) {
    56. up.setParam("literal.id", attach.getId().toString());
    57. }
    58. up.setParam("literal.attach_name", attach.getAttachName());
    59. up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());
    60. up.setParam("literal.attach_address", attach.getAttachAddress());
    61. up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());
    62. up.setParam("literal.extension", attach.getExtension());
    63. up.setParam("literal.create_time", attach.getCreateTime().toString());
    64. // up.setParam("literal.createUser", attach.getCreateUser());
    65. // up.setParam("literal.createDept", attach.getCreateDept());
    66. // up.setParam("literal.updateTime", attach.getUpdateTime().toString());
    67. // up.setParam("literal.updateUser", attach.getUpdateUser());
    68. // up.setParam("literal.status", attach.getStatus().toString());
    69. // up.setParam("literal.isDeleted", attach.getIsDeleted().toString());
    70. up.setParam("literal.size", String.valueOf(file.getTotalSpace()));
    71. up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    72. solrClient.request(up, getCore());
    73. }
    74. @Override
    75. public void putFile(List<KnowledgeBaseManagerVO> managerVO) {
    76. if (ObjectUtil.isEmpty(managerVO)) {
    77. return;
    78. }
    79. for (KnowledgeBaseManagerVO vo : managerVO) {
    80. if (ObjectUtil.isEmpty(vo)) {
    81. continue;
    82. }
    83. putFile(vo.getAttach(), vo);
    84. }
    85. }
    86. @Override
    87. public void putFile(List<DistributeAttach> attachList, KnowledgeBaseManagerVO managerVO) {
    88. if (ObjectUtil.isEmpty(managerVO) || ObjectUtil.isEmpty(attachList)) {
    89. return;
    90. }
    91. for (DistributeAttach attach : attachList) {
    92. putFileUseHttp(attach, managerVO);
    93. }
    94. }
    95. @SneakyThrows
    96. @Override
    97. public void putFile(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {
    98. // 文件上传路径
    99. ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
    100. // 复制文件
    101. File file = new File(attach.getAttachAddress());
    102. // 获取文件类型
    103. String contentType = TypeUtil.getFileContentType(file.getName());
    104. // 加入文件
    105. up.addFile(file, contentType);
    106. // 添加参数
    107. if (ObjectUtil.isEmpty(attach)) {
    108. return;
    109. }
    110. if (ObjectUtil.isNotEmpty(attach.getId())) {
    111. up.setParam("literal.id", attach.getId().toString());
    112. }
    113. up.setParam("literal.attach_name", attach.getAttachName());
    114. up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());
    115. up.setParam("literal.attach_address", attach.getAttachAddress());
    116. up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());
    117. up.setParam("literal.extension", attach.getExtension());
    118. up.setParam("literal.create_time", attach.getCreateTime().toString());
    119. if (ObjectUtil.isNotEmpty(managerVO)) {
    120. up.setParam("literal.kb_id", managerVO.getId().toString());
    121. up.setParam("literal.category_id", managerVO.getCategoryId().toString());
    122. up.setParam("literal.category_name", managerVO.getCategoryName());
    123. up.setParam("literal.category_ancestors", managerVO.getCategoryAncestors());
    124. up.setParam("literal.title", managerVO.getTitle());
    125. up.setParam("literal.content_text", managerVO.getContentText());
    126. up.setParam("literal.label_name", managerVO.getLabelName());
    127. if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {
    128. up.setParam("literal.label_ids", managerVO.getLabelIds());
    129. }
    130. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {
    131. up.setParam("literal.association_module", String.valueOf(managerVO.getAssociationModule()));
    132. }
    133. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {
    134. up.setParam("literal.association_module_name", managerVO.getAssociationModuleName());
    135. }
    136. up.setParam("literal.create_user_name", managerVO.getCreateUserName());
    137. up.setParam("literal.create_user", managerVO.getCreateUser());
    138. up.setParam("literal.create_dept_name", managerVO.getCreateDeptName());
    139. up.setParam("literal.create_dept", managerVO.getCreateDept());
    140. up.setParam("literal.is_deleted", managerVO.getIsDeleted().toString());
    141. }
    142. up.setParam("literal.size", String.valueOf(file.getTotalSpace()));
    143. up.addContentStream(new ContentStreamBase.StringStream(""));
    144. up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    145. solrClient.request(up, getCore());
    146. }
    147. @Override
    148. public String getCore() {
    149. String value = ParamCache.getValue(ParamEnum.ATTACH.getKey());
    150. return ObjectUtil.isEmpty(value) ? ParamEnum.ATTACH.getValue() : value;
    151. }
    152. @Override
    153. public String getUrl() {
    154. if (ObjectUtil.isEmpty(host)) {
    155. return null;
    156. }
    157. return host.concat(getCore().replace("/", "")).concat("/update/extract");
    158. }
    159. @Override
    160. public void putFileUseHttp(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {
    161. // 文件上传路径
    162. String url = getUrl();
    163. System.err.println("请求路径:" + url);
    164. if (ObjectUtil.isEmpty(url)) {
    165. return;
    166. }
    167. // 复制文件
    168. File file = new File(attach.getAttachAddress());
    169. // 添加参数
    170. if (ObjectUtil.isEmpty(attach)) {
    171. return;
    172. }
    173. Map<String, Object> map = new HashMap<>(16);
    174. if (ObjectUtil.isNotEmpty(attach.getId())) {
    175. map.put("literal.id", attach.getId().toString());
    176. }
    177. map.put("literal.attach_name", attach.getAttachName());
    178. map.put("literal.attach_originial_address", attach.getAttachOriginialAddress());
    179. map.put("literal.attach_address", attach.getAttachAddress());
    180. map.put("literal.attach_http_address", attach.getAttachHttpAddress());
    181. map.put("literal.extension", attach.getExtension());
    182. map.put("literal.create_time", attach.getCreateTime().toString());
    183. if (ObjectUtil.isNotEmpty(managerVO)) {
    184. map.put("literal.kb_id", managerVO.getId().toString());
    185. map.put("literal.category_id", managerVO.getCategoryId().toString());
    186. map.put("literal.category_name", managerVO.getCategoryName());
    187. map.put("literal.category_ancestors", managerVO.getCategoryAncestors());
    188. map.put("literal.title", managerVO.getTitle());
    189. map.put("literal.content_text", managerVO.getContentText());
    190. map.put("literal.label_name", managerVO.getLabelName());
    191. if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {
    192. map.put("literal.label_ids", managerVO.getLabelIds());
    193. }
    194. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {
    195. map.put("literal.association_module", String.valueOf(managerVO.getAssociationModule()));
    196. }
    197. if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {
    198. map.put("literal.association_module_name", managerVO.getAssociationModuleName());
    199. }
    200. map.put("literal.create_user_name", managerVO.getCreateUserName());
    201. map.put("literal.create_user", managerVO.getCreateUser());
    202. map.put("literal.create_dept_name", managerVO.getCreateDeptName());
    203. map.put("literal.create_dept", managerVO.getCreateDept());
    204. map.put("literal.is_deleted", managerVO.getIsDeleted().toString());
    205. }
    206. map.put("commitWithin", 1000);
    207. map.put("overwrite", true);
    208. map.put("file", file);
    209. String res = HttpUtil.post(url, map);
    210. System.err.println("响应:" + res);
    211. }
    212. }