首页 养生问答 疾病百科 养生资讯 女性养生 男性养生
您的当前位置:首页正文

springcloudfeign实现远程调用服务传输文件的方法

2023-11-15 来源:华佗健康网
springcloudfeign实现远程调⽤服务传输⽂件的⽅法

实践案例包括两个项⽬,服务提供者项⽬名:upload-service,调⽤服务项⽬名:upload-client,主要给出两个服务之间的调⽤过程,⽂件上传功能不提供

项⽬框架:spring-boot 2.0.1.RELEASE、spring-cloud Finchley.RELEASE依赖:

io.github.openfeign.form feign-form 3.0.3

io.github.openfeign.form feign-form-spring 3.0.3

commons-fileupload commons-fileupload 1.3.3

创建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 uploadFileFTP(@RequestPart(value = \"file\") MultipartFile file, @RequestParam(\"logId\") String logId) {

FtpApiResponse 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:{},耗时:{}\ } 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 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 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 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); } }}

以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容