使用Feign怎么实现微服务间文件传输
使用Feign怎么实现微服务间文件传输?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联于2013年开始,先为扎兰屯等服务建站,扎兰屯等地企业,进行企业商务咨询服务。为扎兰屯企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
引用包
com.netflix.feign feign-core 8.17.0 com.netflix.feign feign-jackson 8.17.0 com.netflix.feign feign-slf4j 8.17.0
调用方式
@ApiOperation(value = "上传Excel", notes = "上传Excel") @RequestMapping(value = "/imExcel", method = RequestMethod.POST, produces = request_headers) public ActionResult imExcel(@RequestBody MultipartFile file,@RequestParam("operatorId") Integer operatorId){ if(file == null || file.isEmpty()|| operatorId==null) return new ActionResult<>(ResultType.BAD_REQUEST,"文件与操作用户ID都不能为空"); String fileName = file.getOriginalFilename(); if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { return new ActionResult<>(ResultType.BAD_REQUEST,"上传文件格式错误,请上传后缀为.xls或.xlsx的文件"); } Feign.Builder encoder = Feign.builder() .decoder(new JacksonDecoder()) .encoder(new FeignEncoder()); FileUpload complainFileUpload = encoder.target(FileUpload.class,LABEL_URL); return complainFileUpload.imComplainExcel(file,operatorId); }
文件Encode
import feign.RequestTemplate; import feign.codec.EncodeException; import feign.codec.Encoder; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Type; import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * @author lxl */ public class FeignEncoder implements Encoder { private final List> converters = new RestTemplate().getMessageConverters(); private final HttpHeaders multipartHeaders = new HttpHeaders(); private final HttpHeaders jsonHeaders = new HttpHeaders(); public static final Charset UTF_8 = Charset.forName("UTF-8"); public FeignEncoder() { multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); jsonHeaders.setContentType(MediaType.APPLICATION_JSON); } @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (isFormRequest(bodyType)) { encodeMultipartFormRequest((Map ) object, template); } else { encodeRequest(object, jsonHeaders, template); } } private void encodeMultipartFormRequest(Map formMap, RequestTemplate template) throws EncodeException { if (CollectionUtils.isEmpty(formMap)) { throw new EncodeException("参数不能为空."); } LinkedMultiValueMap map = new LinkedMultiValueMap<>(); for (Entry entry : formMap.entrySet()) { Object value = entry.getValue(); if (isMultipartFile(value)) { map.add(entry.getKey(), encodeMultipartFile((MultipartFile) value)); } else if (isMultipartFileArray(value)) { encodeMultipartFiles(map, entry.getKey(), Arrays.asList((MultipartFile[]) value)); } else { map.add(entry.getKey(), encodeJsonObject(value)); } } encodeRequest(map, multipartHeaders, template); } private boolean isMultipartFile(Object object) { return object instanceof MultipartFile; } private boolean isMultipartFileArray(Object o) { return o != null && o.getClass().isArray() && MultipartFile.class.isAssignableFrom(o.getClass().getComponentType()); } /** * 设置头 * @param file * @return */ private HttpEntity> encodeMultipartFile(MultipartFile file) { HttpHeaders filePartHeaders = new HttpHeaders(); filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); try { Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream()); return new HttpEntity<>(multipartFileResource, filePartHeaders); } catch (IOException ex) { throw new EncodeException("Cannot encode request.", ex); } } /** * 映射 * @param map * @param name * @param files */ private void encodeMultipartFiles(LinkedMultiValueMap map, String name, List extends MultipartFile> files) { HttpHeaders filePartHeaders = new HttpHeaders(); filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); try { for (MultipartFile file : files) { Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream()); map.add(name, new HttpEntity<>(multipartFileResource, filePartHeaders)); } } catch (IOException ex) { throw new EncodeException("Cannot encode request.", ex); } } /** * {@link HttpEntity} {@code Content-type} * {@code application/json} * * @param o * @return */ private HttpEntity> encodeJsonObject(Object o) { HttpHeaders jsonPartHeaders = new HttpHeaders(); jsonPartHeaders.setContentType(MediaType.APPLICATION_JSON); return new HttpEntity<>(o, jsonPartHeaders); } /** * {@link org.springframework.web.client.RestTemplate} * * @param value * @param requestHeaders * @param template * @throws EncodeException */ private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template) throws EncodeException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders); try { Class> requestType = value.getClass(); MediaType requestContentType = requestHeaders.getContentType(); for (HttpMessageConverter> messageConverter : converters) { if (messageConverter.canWrite(requestType, requestContentType)) { ((HttpMessageConverter
Feign调用接口
@RequestLine("POST /punish/imExcel") ActionResult> imPunishExcel(@Param("file") MultipartFile file, @Param("operatorId") Integer operatorId);
关于使用Feign怎么实现微服务间文件传输问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
文章标题:使用Feign怎么实现微服务间文件传输
链接地址:http://ybzwz.com/article/ggceih.html