Feign与Dubbo文件上传
1.Feign
生产者
feign
/**
* 规则协议图片上传
*
* @param file
* @return
*/
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
RestResponse<OssUploadDTO> upload(@RequestPart(value = "file",required = true) MultipartFile file);
controller
/**
* 规则协议图片上传
*
* @param file
* @return
*/
@Override
@PostMapping("/upload")
public RestResponse<OssUploadDTO> upload(@RequestParam(value = "file",required = true) MultipartFile file) {
try {
OssUploadDTO dto = OssUploadUtils.uploadAndGenerateTmpUrl(file,"customer/images");
return RestResponse.success(dto);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return RestResponse.fail(e.getMessage());
}
}
uploadAndGenerateTmpUrl
/**
* 图片上传
*
* @author linfan.sun
* @description:
* @time: 2021/1/18 15:40
*/
@Slf4j
public class OssUploadUtils {
public static OssUploadDTO uploadAndGenerateTmpUrl(MultipartFile file, String fileDir) {
log.info("图片上传file.getResource():{}",file.getResource());
String fileName = null;
fileName = file.getResource().getFilename();
log.info("图片上传fileName:{}",fileName);
assert fileName != null;
String name = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
log.info("name:{}",name);
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败");
}
AliOSSCloudUtil util = new AliOSSCloudUtil();
//上传成功返回完整路径
OssUploadDTO dto = util.uploadFile2OSSResult(inputStream, name, fileDir + "/", "dyyy-static");
util.close();
return dto;
}
}
消费者
/**
* 规则协议图片上传
*
* @param file
* @return
*/
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
RestResponse<OssUploadDTO> upload(@RequestParam(value = "file") MultipartFile file){
return sfcAgreementClient.upload(file);
}
2.Dubbo
生产者
dubbo
/**
* 规则协议图片上传
*
* @param fileBytes
* @return
*/
OssUploadDTO uploadFile(byte[] fileBytes,String name) throws IOException;
dubbo实现
@Override
public OssUploadDTO uploadFile(byte[] fileBytes,String name) throws IOException {
InputStream inputStream = new ByteArrayInputStream(fileBytes);
MultipartFile file = new MockMultipartFile(name,null,ContentType.MULTIPART_FORM_DATA.toString(), inputStream);
OssUploadDTO dto = OssUploadFileUtils.uploadAndGenerateTmpUrl(file, "App-server/images/customer-service",name);
return dto;
}
uploadAndGenerateTmpUrl
public static OssUploadDTO uploadAndGenerateTmpUrl(MultipartFile file, String fileDir,String fileName) {
log.info("图片上传file.getResource():{}",file.getResource());
String name = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
log.info("name:{}",name);
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败");
}
AliOSSCloudUtil util = new AliOSSCloudUtil();
//上传成功返回完整路径
OssUploadDTO dto = util.uploadFile2OSSResult(inputStream, name, fileDir + "/", "dyyy-static");
util.close();
return dto;
}
controller
@PostMapping("/uploadFile")
public RestResponse<OssUploadDTO> uploadFile(byte[] bytes,String fileName) {
try {
OssUploadDTO dto = agreementService.uploadFile(bytes,fileName);
return RestResponse.success(dto);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return RestResponse.fail(e.getMessage());
}
}
消费者
@DubboReference(version = "1.0.0")
private AgreementService agreementService;
/**
* 协议图片上传
*
* @param file
* @return
*/
@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public RestResponse<OssUploadDTO> uploadFile(@RequestPart(value = "file") MultipartFile file) {
try {
byte[] bytes = file.getBytes();
String fileName = file.getResource().getFilename();
OssUploadDTO dto = agreementService.uploadFile(bytes,fileName);
return RestResponse.success(dto);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return RestResponse.fail(e.getMessage());
}
}