本篇文章给大家分享的是有关怎么在spring cloud feign中实现远程调用服务传输文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
依赖:
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
创建FeignClient接口(用于指定远程调用的服务)
// 申明这是一个Feign客户端,并且指明服务id @FeignClient(value = "com-spring-caclulate") public interface CacluFeignClient { // 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful的调用了 @RequestMapping(value = "/caclu/{num}", method = RequestMethod.GET) public Item caclulate(@PathVariable("num") Integer num); }
一.文件上传服务upload-service
1.控制层
@Slf4j @CrossOrigin @RestController @RequestMapping("/ftp") @Api(description = "文件上传控制") public class FtpFileController { @Autowired private FtpFileService ftpFileService; /** * FTP文件上传 * * @return */ @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public FtpApiResponse<FtpUploadResDTO> uploadFileFTP(@RequestPart(value = "file") MultipartFile file, @RequestParam("logId") String logId) { FtpApiResponse<FtpUploadResDTO> result = new FtpApiResponse<>(); LogUtil.updateLogId(logId); try { log.info("文件上传开始!}"); Long startTime = System.currentTimeMillis(); FtpUploadResDTO resDTO = ftpFileService.uploadFile(file); result.setData(resDTO); result.setSuccess(true); result.setTimeInMillis(System.currentTimeMillis() - startTime); log.info("文件上传结束 resDTO:{},耗时:{}", resDTO, (System.currentTimeMillis() - startTime)); } catch (ServiceException e){ result.setSuccess(false); result.setErrorCode(ErrorMsgEnum.FILE_UPLOAD_EXCEPTION.getCode()); result.setErrorMsg(ErrorMsgEnum.FILE_UPLOAD_EXCEPTION.getMsg()); } catch (Exception e) { result.setSuccess(false); result.setErrorCode(ErrorMsgEnum.SYSTEM_ERROR.getCode()); result.setErrorMsg(ErrorMsgEnum.SYSTEM_ERROR.getMsg()); log.info("文件上传失败 Exception:{}", Throwables.getStackTraceAsString(e)); } return result; } }
2.业务层
@Service @Slf4j public class FtpFileService { @Autowired private FtpFileManager ftpFileManager; /** * 上传文件 * * @param file * @return */ public FtpUploadResDTO uploadFile(MultipartFile file) { try { //判断上传文件是否为空 if (null == file || file.isEmpty() || file.getSize() == 0) { log.info("传入的文件为空,file:{}", file); throw new ServiceException(ErrorMsgEnum.EMPTY_FILE); } //文件上传至ftp服务目录 FtpFileRecordDO ftpFileRecordDO = ftpFileManager.fileUploadToFtp(file); if (null == ftpFileRecordDO) { log.info("文件上传至ftp服务目录异常"); throw new ServiceException(ErrorMsgEnum.FILE_UPLOAD_TO_FTP_EXCEPTION); } return ftpFileManager.addFileRecord(ftpFileRecordDO); } catch (Exception e) { log.error("业务异常,case", e); throw new ServiceException(ErrorMsgEnum.SYSTEM_ERROR); } } }
3.服务写好后,需要把远程接口暴露出去
@FeignClient(value = "upload-service", configuration = UpDownFtpFacade.MultipartSupportConfig.class) public interface UpDownFtpFacade { /** * FTP上传文件 * * @param file 文件 * @param logId 日志id * @return */ @PostMapping(value = "/ftp/uploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) FtpApiResponse<FtpUploadResDTO> uploadFileFTP(@RequestPart(value = "file") MultipartFile file, @RequestParam("logId") String logId); /** * 引用配置类MultipartSupportConfig.并且实例化 */ @Configuration class MultipartSupportConfig { @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(); } } }
二.文件上传客户端upload-client
@Slf4j @Component public class FileManager { @Autowired private UpDownFtpFacade upDownFtpFacade; /** * 调用远程上传文件接口 * * @param file 待上传的文件 * @return 下载路径 **/ public FtpApiResponse<FtpUploadResDTO> requestFtpFacade(MultipartFile file) { try { DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file", MediaType.ALL_VALUE, true, file.getOriginalFilename()); InputStream input = file.getInputStream(); OutputStream os = fileItem.getOutputStream(); IOUtils.copy(input, os); MultipartFile multi = new CommonsMultipartFile(fileItem); FtpApiResponse<FtpUploadResDTO> response = upDownFtpFacade.uploadFileFTP(multi, LogUtil.getLogId()); if (null == response || !response.getSuccess() || null == response.getData()) { throw new ManagerException(ErrorMsgEnum.FIlE_UPLOAD_FAILED); } return response; } catch (Exception e) { throw new ManagerException(ErrorMsgEnum.FIlE_UPLOAD_FAILED); } } }
以上就是怎么在spring cloud feign中实现远程调用服务传输文件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。