使用官方提供的ContentStreamUpdateRequest请求,虽然默认为POST请求,但是请求参数还是放在了url中,导致url太长,
错误信息:
URI is too large >8192
解决办法:
(治标不治本)修改jetty.xml的参数
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">...<Set name="requestHeaderSize"><Property name="solr.jetty.request.header.size" default="81920" /></Set></New>
自己封装http接口请求
原代码:
package com.datago.microservice.kb.server.service.impl;import cn.hutool.core.io.FileUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.http.HttpUtil;import com.datago.microservice.kb.common.utils.Func;import com.datago.microservice.kb.server.entity.Attach;import com.datago.microservice.kb.server.entity.DistributeAttach;import com.datago.microservice.kb.server.entity.KnowledgeBaseManager;import com.datago.microservice.kb.server.enums.ParamEnum;import com.datago.microservice.kb.server.enums.SolrQueryEnum;import com.datago.microservice.kb.server.service.IAttachSolrService;import com.datago.microservice.kb.server.utils.cache.ParamCache;import com.datago.microservice.kb.server.utils.common.SolrUtil;import com.datago.microservice.kb.server.utils.common.TypeUtil;import com.datago.microservice.kb.server.vo.KnowledgeBaseManagerVO;import lombok.SneakyThrows;import org.apache.solr.client.solrj.request.AbstractUpdateRequest;import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;import org.apache.solr.common.util.ContentStreamBase;import org.springframework.beans.factory.annotation.Value;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.io.File;import java.util.*;import java.util.stream.Collectors;/*** 附件** @author xq* @date 2022/03/21 16:29* @describe 附件*/@Servicepublic class AttachSolrServiceImpl extends BaseSolrServiceImpl<Attach> implements IAttachSolrService {@Async@SneakyThrows@Overridepublic void putFile(MultipartFile multipartFile, DistributeAttach attach) {// 文件上传路径ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");// 复制文件File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));FileUtil.writeFromStream(multipartFile.getInputStream(), file);// 获取文件类型String contentType = TypeUtil.getFileContentType(file.getName());// 加入文件up.addFile(file, contentType);// 添加参数if (ObjectUtil.isEmpty(attach)) {return;}if (ObjectUtil.isNotEmpty(attach.getId())) {up.setParam("literal.id", attach.getId().toString());}up.setParam("literal.attach_name", attach.getAttachName());up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());up.setParam("literal.attach_address", attach.getAttachAddress());up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());up.setParam("literal.extension", attach.getExtension());up.setParam("literal.create_time", attach.getCreateTime().toString());// up.setParam("literal.createUser", attach.getCreateUser());// up.setParam("literal.createDept", attach.getCreateDept());// up.setParam("literal.updateTime", attach.getUpdateTime().toString());// up.setParam("literal.updateUser", attach.getUpdateUser());// up.setParam("literal.status", attach.getStatus().toString());// up.setParam("literal.isDeleted", attach.getIsDeleted().toString());up.setParam("literal.size", String.valueOf(file.getTotalSpace()));up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);solrClient.request(up, getCore());}@Overridepublic void putFile(List<KnowledgeBaseManagerVO> managerVO) {if (ObjectUtil.isEmpty(managerVO)) {return;}for (KnowledgeBaseManagerVO vo : managerVO) {if (ObjectUtil.isEmpty(vo)) {continue;}putFile(vo.getAttach(), vo);}}@Overridepublic void putFile(List<DistributeAttach> attachList, KnowledgeBaseManagerVO managerVO) {if (ObjectUtil.isEmpty(managerVO) || ObjectUtil.isEmpty(attachList)) {return;}for (DistributeAttach attach : attachList) {putFile(attach, managerVO);}}@SneakyThrows@Overridepublic void putFile(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {// 文件上传路径ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");// 复制文件File file = new File(attach.getAttachAddress());// 获取文件类型String contentType = TypeUtil.getFileContentType(file.getName());// 加入文件up.addFile(file, contentType);// 添加参数if (ObjectUtil.isEmpty(attach)) {return;}if (ObjectUtil.isNotEmpty(attach.getId())) {up.setParam("literal.id", attach.getId().toString());}up.setParam("literal.attach_name", attach.getAttachName());up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());up.setParam("literal.attach_address", attach.getAttachAddress());up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());up.setParam("literal.extension", attach.getExtension());up.setParam("literal.create_time", attach.getCreateTime().toString());if (ObjectUtil.isNotEmpty(managerVO)) {up.setParam("literal.kb_id", managerVO.getId().toString());up.setParam("literal.category_id", managerVO.getCategoryId().toString());up.setParam("literal.category_name", managerVO.getCategoryName());up.setParam("literal.category_ancestors", managerVO.getCategoryAncestors());up.setParam("literal.title", managerVO.getTitle());up.setParam("literal.content_text", managerVO.getContentText());up.setParam("literal.label_name", managerVO.getLabelName());if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {up.setParam("literal.label_ids", managerVO.getLabelIds());}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {up.setParam("literal.association_module", String.valueOf(managerVO.getAssociationModule()));}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {up.setParam("literal.association_module_name", managerVO.getAssociationModuleName());}up.setParam("literal.create_user_name", managerVO.getCreateUserName());up.setParam("literal.create_user", managerVO.getCreateUser());up.setParam("literal.create_dept_name", managerVO.getCreateDeptName());up.setParam("literal.create_dept", managerVO.getCreateDept());up.setParam("literal.is_deleted", managerVO.getIsDeleted().toString());}up.setParam("literal.size", String.valueOf(file.getTotalSpace()));up.addContentStream(new ContentStreamBase.StringStream(""));up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);solrClient.request(up, getCore());}@Overridepublic String getCore() {String value = ParamCache.getValue(ParamEnum.ATTACH.getKey());return ObjectUtil.isEmpty(value) ? ParamEnum.ATTACH.getValue() : value;}}
后修改为下方代码:
package com.datago.microservice.kb.server.service.impl;import cn.hutool.core.io.FileUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.http.HttpUtil;import com.datago.microservice.kb.common.utils.Func;import com.datago.microservice.kb.server.entity.Attach;import com.datago.microservice.kb.server.entity.DistributeAttach;import com.datago.microservice.kb.server.entity.KnowledgeBaseManager;import com.datago.microservice.kb.server.enums.ParamEnum;import com.datago.microservice.kb.server.enums.SolrQueryEnum;import com.datago.microservice.kb.server.service.IAttachSolrService;import com.datago.microservice.kb.server.utils.cache.ParamCache;import com.datago.microservice.kb.server.utils.common.SolrUtil;import com.datago.microservice.kb.server.utils.common.TypeUtil;import com.datago.microservice.kb.server.vo.KnowledgeBaseManagerVO;import lombok.SneakyThrows;import org.apache.solr.client.solrj.request.AbstractUpdateRequest;import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;import org.apache.solr.common.util.ContentStreamBase;import org.springframework.beans.factory.annotation.Value;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.io.File;import java.util.*;import java.util.stream.Collectors;/*** 附件** @author xq* @date 2022/03/21 16:29* @describe 附件*/@Servicepublic class AttachSolrServiceImpl extends BaseSolrServiceImpl<Attach> implements IAttachSolrService {@Value("${spring.data.solr.host}")private String host;@Async@SneakyThrows@Overridepublic void putFile(MultipartFile multipartFile, DistributeAttach attach) {// 文件上传路径ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");// 复制文件File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));FileUtil.writeFromStream(multipartFile.getInputStream(), file);// 获取文件类型String contentType = TypeUtil.getFileContentType(file.getName());// 加入文件up.addFile(file, contentType);// 添加参数if (ObjectUtil.isEmpty(attach)) {return;}if (ObjectUtil.isNotEmpty(attach.getId())) {up.setParam("literal.id", attach.getId().toString());}up.setParam("literal.attach_name", attach.getAttachName());up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());up.setParam("literal.attach_address", attach.getAttachAddress());up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());up.setParam("literal.extension", attach.getExtension());up.setParam("literal.create_time", attach.getCreateTime().toString());// up.setParam("literal.createUser", attach.getCreateUser());// up.setParam("literal.createDept", attach.getCreateDept());// up.setParam("literal.updateTime", attach.getUpdateTime().toString());// up.setParam("literal.updateUser", attach.getUpdateUser());// up.setParam("literal.status", attach.getStatus().toString());// up.setParam("literal.isDeleted", attach.getIsDeleted().toString());up.setParam("literal.size", String.valueOf(file.getTotalSpace()));up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);solrClient.request(up, getCore());}@Overridepublic void putFile(List<KnowledgeBaseManagerVO> managerVO) {if (ObjectUtil.isEmpty(managerVO)) {return;}for (KnowledgeBaseManagerVO vo : managerVO) {if (ObjectUtil.isEmpty(vo)) {continue;}putFile(vo.getAttach(), vo);}}@Overridepublic void putFile(List<DistributeAttach> attachList, KnowledgeBaseManagerVO managerVO) {if (ObjectUtil.isEmpty(managerVO) || ObjectUtil.isEmpty(attachList)) {return;}for (DistributeAttach attach : attachList) {putFileUseHttp(attach, managerVO);}}@SneakyThrows@Overridepublic void putFile(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {// 文件上传路径ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");// 复制文件File file = new File(attach.getAttachAddress());// 获取文件类型String contentType = TypeUtil.getFileContentType(file.getName());// 加入文件up.addFile(file, contentType);// 添加参数if (ObjectUtil.isEmpty(attach)) {return;}if (ObjectUtil.isNotEmpty(attach.getId())) {up.setParam("literal.id", attach.getId().toString());}up.setParam("literal.attach_name", attach.getAttachName());up.setParam("literal.attach_originial_address", attach.getAttachOriginialAddress());up.setParam("literal.attach_address", attach.getAttachAddress());up.setParam("literal.attach_http_address", attach.getAttachHttpAddress());up.setParam("literal.extension", attach.getExtension());up.setParam("literal.create_time", attach.getCreateTime().toString());if (ObjectUtil.isNotEmpty(managerVO)) {up.setParam("literal.kb_id", managerVO.getId().toString());up.setParam("literal.category_id", managerVO.getCategoryId().toString());up.setParam("literal.category_name", managerVO.getCategoryName());up.setParam("literal.category_ancestors", managerVO.getCategoryAncestors());up.setParam("literal.title", managerVO.getTitle());up.setParam("literal.content_text", managerVO.getContentText());up.setParam("literal.label_name", managerVO.getLabelName());if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {up.setParam("literal.label_ids", managerVO.getLabelIds());}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {up.setParam("literal.association_module", String.valueOf(managerVO.getAssociationModule()));}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {up.setParam("literal.association_module_name", managerVO.getAssociationModuleName());}up.setParam("literal.create_user_name", managerVO.getCreateUserName());up.setParam("literal.create_user", managerVO.getCreateUser());up.setParam("literal.create_dept_name", managerVO.getCreateDeptName());up.setParam("literal.create_dept", managerVO.getCreateDept());up.setParam("literal.is_deleted", managerVO.getIsDeleted().toString());}up.setParam("literal.size", String.valueOf(file.getTotalSpace()));up.addContentStream(new ContentStreamBase.StringStream(""));up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);solrClient.request(up, getCore());}@Overridepublic String getCore() {String value = ParamCache.getValue(ParamEnum.ATTACH.getKey());return ObjectUtil.isEmpty(value) ? ParamEnum.ATTACH.getValue() : value;}@Overridepublic String getUrl() {if (ObjectUtil.isEmpty(host)) {return null;}return host.concat(getCore().replace("/", "")).concat("/update/extract");}@Overridepublic void putFileUseHttp(DistributeAttach attach, KnowledgeBaseManagerVO managerVO) {// 文件上传路径String url = getUrl();System.err.println("请求路径:" + url);if (ObjectUtil.isEmpty(url)) {return;}// 复制文件File file = new File(attach.getAttachAddress());// 添加参数if (ObjectUtil.isEmpty(attach)) {return;}Map<String, Object> map = new HashMap<>(16);if (ObjectUtil.isNotEmpty(attach.getId())) {map.put("literal.id", attach.getId().toString());}map.put("literal.attach_name", attach.getAttachName());map.put("literal.attach_originial_address", attach.getAttachOriginialAddress());map.put("literal.attach_address", attach.getAttachAddress());map.put("literal.attach_http_address", attach.getAttachHttpAddress());map.put("literal.extension", attach.getExtension());map.put("literal.create_time", attach.getCreateTime().toString());if (ObjectUtil.isNotEmpty(managerVO)) {map.put("literal.kb_id", managerVO.getId().toString());map.put("literal.category_id", managerVO.getCategoryId().toString());map.put("literal.category_name", managerVO.getCategoryName());map.put("literal.category_ancestors", managerVO.getCategoryAncestors());map.put("literal.title", managerVO.getTitle());map.put("literal.content_text", managerVO.getContentText());map.put("literal.label_name", managerVO.getLabelName());if (ObjectUtil.isNotEmpty(managerVO.getLabelIds())) {map.put("literal.label_ids", managerVO.getLabelIds());}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModule())) {map.put("literal.association_module", String.valueOf(managerVO.getAssociationModule()));}if (ObjectUtil.isNotEmpty(managerVO.getAssociationModuleName())) {map.put("literal.association_module_name", managerVO.getAssociationModuleName());}map.put("literal.create_user_name", managerVO.getCreateUserName());map.put("literal.create_user", managerVO.getCreateUser());map.put("literal.create_dept_name", managerVO.getCreateDeptName());map.put("literal.create_dept", managerVO.getCreateDept());map.put("literal.is_deleted", managerVO.getIsDeleted().toString());}map.put("commitWithin", 1000);map.put("overwrite", true);map.put("file", file);String res = HttpUtil.post(url, map);System.err.println("响应:" + res);}}
