spring cloud Feign實現文件傳輸/上傳


今天在實現后台上傳功能時,在feign傳輸文件時一直出問題,故此記錄下實現過程,需特別注意標紅代碼地方。

此功能實現后台代碼分為服務端、客戶端:

  前台頁面調用客戶端接口,客戶端再調用服務端接口

1.服務端FileController接口:

@RequestMapping(value = "/uploadImageSingle", method = RequestMethod.POST)
    public BaseDataResp uploadImage(@RequestParam(value = "file") MultipartFile file) {
        BaseDataResp resp = new BaseDataResp();
        try {
            if(file.isEmpty()){
                return BaseDataResp.fail(IGowResultCode.Commons.ERROR_PARAMETER);
            }
            ArrayList<StorePath> list = new ArrayList();
            Set<MetaData> metaDataSet = new HashSet();
            StorePath storePath = this.client.uploadImage(new FastImageFile(file.getInputStream(), file.getSize(), FileUtil.getSuffix(file.getOriginalFilename()), metaDataSet));
            resp.setData(storePath);
        } catch (Exception e) {
            ExceptionLogUtil.debugOrError(log, e, e.getMessage() + e.getMessage());
        }
        resp.setCode(IGowResultCode.Commons.SUCCESS);
        return resp;
    }

 

2.客戶端接口主要實現代碼:

 

private TSystemFiles uploadFile(CommonsMultipartFile file) throws Exception {
  // 需對傳進來的File進行轉換,直接傳參會報400異常
  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);

        // 上傳文件
    BaseDataResp resp = fileClient.uploadFile(multi);
  if(!IGowResultCode.Commons.SUCCESS.equals(resp.getCode())){
  throw new UserException(IGowResultCode.Commons.ERROR_UNKNOWN);
  }

FileClient:
@PostMapping(value = "/file/server/uploadImageSingle", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE} , produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    BaseDataResp uploadFile(@RequestPart(value = "file",required = false) MultipartFile file);

 

WebConfig:
@Bean
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
            @Override
            public HttpMessageConverters getObject() throws BeansException {
                return new HttpMessageConverters(new RestTemplate().getMessageConverters()); } }));
    }

 

maven jar 引用:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM