实践案例包括两个项⽬,服务提供者项⽬名:upload-service,调⽤服务项⽬名:upload-client,主要给出两个服务之间的调⽤过程,⽂件上传功能不提供
项⽬框架:spring-boot 2.0.1.RELEASE、spring-cloud Finchley.RELEASE依赖:
创建FeignClient接⼝(⽤于指定远程调⽤的服务)
// 申明这是⼀个Feign客户端,并且指明服务id@FeignClient(value = \"com-spring-caclulate\") public interface CacluFeignClient {
// 这⾥定义了类似于SpringMVC⽤法的⽅法,就可以进⾏RESTful的调⽤了 @RequestMapping(value = \"/caclu/{num}\ public Item caclulate(@PathVariable(\"num\") Integer num);}
⼀.⽂件上传服务upload-service1.控制层
@Slf4j
@CrossOrigin@RestController
@RequestMapping(\"/ftp\")
@Api(description = \"⽂件上传控制\")public class FtpFileController { @Autowired
private FtpFileService ftpFileService;
/**
* FTP⽂件上传 *
* @return */
@PostMapping(value = \"/uploadFile\
public FtpApiResponse FtpApiResponse 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:{},耗时:{}\ } 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:{}\ } 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:{}\ 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\ throw new ServiceException(ErrorMsgEnum.SYSTEM_ERROR); } }} 3.服务写好后,需要把远程接⼝暴露出去 @FeignClient(value = \"upload-service\public interface UpDownFtpFacade { /** * FTP上传⽂件 * * @param file ⽂件 * @param logId ⽇志id * @return */ @PostMapping(value = \"/ftp/uploadFile\ FtpApiResponse * 引⽤配置类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 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 return response; } catch (Exception e) { throw new ManagerException(ErrorMsgEnum.FIlE_UPLOAD_FAILED); } }} 以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。 因篇幅问题不能全部显示,请点此查看更多更全内容