您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。