最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring cloud feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用服務(wù)傳輸文件的方法

 更新時(shí)間:2018年09月03日 13:51:10   作者:藍(lán)之魚  
這篇文章主要介紹了spring cloud feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用服務(wù)傳輸文件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

實(shí)踐案例包括兩個(gè)項(xiàng)目,服務(wù)提供者項(xiàng)目名:upload-service,調(diào)用服務(wù)項(xiàng)目名:upload-client,主要給出兩個(gè)服務(wù)之間的調(diào)用過程,文件上傳功能不提供

項(xiàng)目框架:spring-boot 2.0.1.RELEASE、spring-cloud Finchley.RELEASE

依賴:

  <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>

// 申明這是一個(gè)Feign客戶端,并且指明服務(wù)id
@FeignClient(value = "com-spring-caclulate") 
public interface CacluFeignClient {

 // 這里定義了類似于SpringMVC用法的方法,就可以進(jìn)行RESTful的調(diào)用了
 @RequestMapping(value = "/caclu/{num}", method = RequestMethod.GET)
 public Item caclulate(@PathVariable("num") Integer num);

} 

一.文件上傳服務(wù)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("文件上傳結(jié)束 resDTO:{},耗時(shí):{}", 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.業(yè)務(wù)層

@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服務(wù)目錄
   FtpFileRecordDO ftpFileRecordDO = ftpFileManager.fileUploadToFtp(file);
   if (null == ftpFileRecordDO) {
    log.info("文件上傳至ftp服務(wù)目錄異常");
    throw new ServiceException(ErrorMsgEnum.FILE_UPLOAD_TO_FTP_EXCEPTION);
   }
   return ftpFileManager.addFileRecord(ftpFileRecordDO);
  } catch (Exception e) {
   log.error("業(yè)務(wù)異常,case", e);
   throw new ServiceException(ErrorMsgEnum.SYSTEM_ERROR);
  }
 }
}

3.服務(wù)寫好后,需要把遠(yuǎn)程接口暴露出去

@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.并且實(shí)例化
  */
 @Configuration
 class MultipartSupportConfig {
  @Bean
  public Encoder feignFormEncoder() {
   return new SpringFormEncoder();
  }
 }

}

二.文件上傳客戶端upload-client

@Slf4j
@Component
public class FileManager {
  @Autowired
  private UpDownFtpFacade upDownFtpFacade;
  
  /**
  * 調(diào)用遠(yuǎn)程上傳文件接口
  *
  * @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);
  }
  }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 代理(Proxy)的原理及應(yīng)用

    Java 代理(Proxy)的原理及應(yīng)用

    動(dòng)態(tài)代理技術(shù)就是用來產(chǎn)生一個(gè)對(duì)象的代理對(duì)象的。 我們?cè)陂_發(fā)中之所以要產(chǎn)生一個(gè)對(duì)象的代理對(duì)象,主要用于攔截對(duì)真實(shí)業(yè)務(wù)對(duì)象的訪問。本文主要介紹了Java 代理的使用,感興趣的可以了解一下
    2021-05-05
  • druid連接池的參數(shù)配置示例全面解析

    druid連接池的參數(shù)配置示例全面解析

    這篇文章主要為大家介紹了druid連接池的參數(shù)配置示例全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java實(shí)現(xiàn)大數(shù)運(yùn)算的實(shí)例代碼

    Java實(shí)現(xiàn)大數(shù)運(yùn)算的實(shí)例代碼

    這篇文章主要介紹了Java實(shí)現(xiàn)大數(shù)運(yùn)算的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • java線程之用Thread類創(chuàng)建線程的方法

    java線程之用Thread類創(chuàng)建線程的方法

    本篇文章介紹了,Thread類創(chuàng)建線程的方法。需要的朋友參考下
    2013-05-05
  • Eclipse快速添加get、set方法的操作技巧

    Eclipse快速添加get、set方法的操作技巧

    這篇文章主要介紹了Eclipse快速添加get、set方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • 淺談Springboot實(shí)現(xiàn)攔截器的兩種方式

    淺談Springboot實(shí)現(xiàn)攔截器的兩種方式

    本文詳細(xì)的介紹了Springboot攔截器的兩種方式實(shí)現(xiàn),一種就是用攔截器,一種就是過濾器,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • java如何讓帶T的時(shí)間格式化

    java如何讓帶T的時(shí)間格式化

    這篇文章主要介紹了java如何讓帶T的時(shí)間格式化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Android?Studio中創(chuàng)建java工程的完整步驟

    Android?Studio中創(chuàng)建java工程的完整步驟

    Android?Studio創(chuàng)建java工程是非常麻煩的,因?yàn)锳ndroid?Studio沒有提供直接創(chuàng)建java工程的方法,下面這篇文章主要給大家介紹了關(guān)于Android?Studio中創(chuàng)建java工程的完整步驟,需要的朋友可以參考下
    2024-01-01
  • Java實(shí)現(xiàn)多項(xiàng)式乘法代碼實(shí)例

    Java實(shí)現(xiàn)多項(xiàng)式乘法代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)多項(xiàng)式乘法代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 基于Spring Boot保護(hù)Web應(yīng)用程序

    基于Spring Boot保護(hù)Web應(yīng)用程序

    這篇文章主要介紹了基于Spring Boot保護(hù)Web應(yīng)用程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論

东阿县| 固原市| 渭源县| 盐城市| 伊金霍洛旗| 萨迦县| 新营市| 区。| 旅游| 乐山市| 汉寿县| 镇安县| 昌图县| 噶尔县| 大安市| 错那县| 三门县| 平顶山市| 西华县| 武定县| 吉水县| 南丰县| 辽阳市| 钦州市| 诏安县| 宁阳县| 宁安市| 天祝| 保亭| 寿阳县| 贵阳市| 花莲市| 宜兰县| 青龙| 抚州市| 察雅县| 麻阳| 佛坪县| 景洪市| 三江| 忻州市|