由于自己也是在網(wǎng)上幾經(jīng)一番折騰才找到部分博客并加以修改實(shí)現(xiàn); 話不多說直接上代碼;
1.1 文件上傳之服務(wù)端代碼(在此處以服務(wù)端內(nèi)部上傳OSS為例)
@PostMapping(value = "upload")
public BaseResult<String> test(MultipartFile file) {
String downloadUrl = fileService.upload(file);
Preconditions.checkArgument(StringUtils.isNotEmpty(downloadUrl), UPLOAD_FILE_ERROR);
return BaseResult.generateSuccessRestlt(downloadUrl);
}
@Override
public String upload(MultipartFile file) {
String filename = file.getOriginalFilename();
try (InputStream inputStream = file.getInputStream()) {
PutObjectRequest putObjectRequest = new PutObjectRequest(fileConfig.getBucketName(),
filename, inputStream);
ossClient.putObject(putObjectRequest);
} catch (IOException e) {
log.error("filename:[{}],獲取文件輸入流錯(cuò)誤!", filename, e);
return null;
} catch (OSSException | ClientException e) {
log.info("上傳文件失敗filename:[{}]", filename, e);
return null;
}
return filename;
}
1.2 文件下載之服務(wù)端代碼
@GetMapping(value = "download")
public void down(@RequestParam(value = "filename") String filename,
HttpServletResponse response) {
fileService.download(filename, response);
}
@Override
public void download(String filepath, HttpServletResponse response) {
response
.setContentType("application/x-download;charset=" + Charsets.UTF_8.displayName());
response.addHeader("Content-Disposition",
"attachment;filename=" + this.reWriteChinese(filepath));
// ossObject包含文件所在的存儲(chǔ)空間名稱、文件名稱闷愤、文件元信息以及一個(gè)輸入流。
OSSObject ossObject;
try {
ossObject = ossClient.getObject(fileConfig.getBucketName(), filepath);
} catch (OSSException | ClientException e) {
log.error("獲取OSS文件連接異常,filePath:[{}]", filepath, e);
return;
}
try (OutputStream outputStream = response.getOutputStream();
InputStream inputStream = ossObject.getObjectContent()) {
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
} catch (IOException e) {
log.error("下載文件異常 filepath:[{}]", filepath, e);
}
}
1.3 文件上傳和下載FeignClient調(diào)用方代碼
/*
* Copyright (c) 2020 daguimu.geek@gmail.com
* All rights reserved.
*
*/
package com.dagm.api.feignclient;
import feign.Response;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
/**
* @author Guimu
* @date 2020/01/08
*/
@FeignClient(value = "file-service", configuration = FileFeign.MultipartSupportConfig.class)
public interface FileFeign {
/**
* 調(diào)用feign 對(duì)文件進(jìn)行上傳
*
* @param file 待上傳的文件
* @return com.dagm.shorter.res.BaseResult<java.lang.String>
* @author Guimu
* @date 2020/1/8
*/
@PostMapping(value = "/inner/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
BaseResult<String> upload(@RequestPart("file") MultipartFile file);
/**
* 文件下載
*
* @return feign.Response
* @author Guimu
* @date 2020/1/8
*/
@GetMapping(value = "/inner/download")
Response download(@RequestParam("filename") String filename);
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
1.4 tip:
- 文件的上傳下載服務(wù)端代碼, 和一般的上傳下載一樣:
OpenFeign 文件上傳需要配置Encoder, 我這里用的是feign-form; 也可以選擇其他的;
feign-form 版本對(duì)應(yīng) 3.5版本之后的對(duì)應(yīng)OpenFeign 10.* , 3.5版本之前的對(duì)應(yīng)OpenFeign9.*
1.5 相關(guān)的pom 依賴
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.4.1</version>
</dependency>
1.6 附加一個(gè)調(diào)用方使用OpenFeign 上傳文件和下載并返回給調(diào)用方客戶端的demo
@PostMapping(value = "upload")
public BaseResult<String> test(MultipartFile file) {
return fileFeign.upload(file);
}
@GetMapping(value = "download")
public void download(@RequestParam(value = "filename") String filename,
HttpServletResponse httpServletResponse) {
httpServletResponse
.setContentType("application/x-download;charset=" + Charsets.UTF_8.displayName());
httpServletResponse.addHeader("Content-Disposition",
"attachment;filename=" + this.reWriteChinese(filename));
Response response = fileFeign.download(filename);
Response.Body body = response.body();
try (OutputStream outputStream = httpServletResponse.getOutputStream();
InputStream inputStream = body.asInputStream()) {
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
} catch (IOException e) {
log.error("下載文件異常 filepath:[{}]", filename, e);
}
}