[英]How to pass Multipart file from one service to another service in spring boot?
我想將多部分文件從一個服務傳遞到另一個服務。
客戶端 --> 服務 1 --> 服務 2
當我將文件從 Service1 傳遞到 Service2 時,這會顯示錯誤“500 內部服務器錯誤,當前請求不是多部分請求”
客戶端 --> Service2 當我直接發送文件時它的工作但不是通過 Service1
我想知道可能是什么原因,我想在將多部分文件作為參數傳遞時我遺漏了一些標題部分。
服務2
@PostMapping(path="/upload")
public ResponseEntity<Properties> upload(@RequestParam("file") MultipartFile multiPart) {
return saveFile(multiPart);
}
Service2-client
@FeignClient
(name="${feign.upload.serverId}", configuration = UploadServiceClientConfiguration.class, decode404 = true)
public interface UploadServiceClient {
@PostMapping(path="/upload")
ResponseEntity<Properties> upload(@RequestParam("file") MultipartFile multiPart);
class UploadServiceClientConfiguration {
@Value(value="${feign.upload.user}")
String user;
@Value(value="${feign.upload.password}")
String password;
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor(user, password);
}
}
}
服務1
@Autowired
UploadServiceClient uploadSvcClient;
@PostMapping(path="/upload")
public ResponseEntity<Properties> uploadAttachment(@RequestParam("file") MultipartFile file) {
return uploadSvcClient.upload(file);
}
最后能夠使用post文件上傳spring cloud feign客戶端解決與另一個服務的通信問題
我已將 FeignClient 參數類型從
@RequestParam("file") MultipartFile mFile
到
@RequestPart("file") MultiValueMap 文件。
FeignClient 簽名
@PostMapping(value="/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<Properties> upload(@RequestHeader(name=UID,required=false) String uid, @RequestPart("file") MultiValueMap<String, Object> file);
Service1 實現
@PostMapping(path="/upload")
public ResponseEntity<Properties> uploadAttachment(@RequestHeader(IRSConsts.UID) String uid, @RequestParam("file") MultipartFile mFile) {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
ByteArrayResource contentsAsResource = null;
try {
contentsAsResource = new ByteArrayResource(mFile.getBytes()) {
@Override
public String getFilename() {
return mFile.getOriginalFilename();
}
};
} catch (IOException e) {
e.printStackTrace();
}
multiValueMap.add("file", contentsAsResource);
return transSvcClient.upload(uid, multiValueMap);
}
Service2 實現
@PostMapping(path = "/upload")
@Headers("Content-Type: multipart/form-data")
public ResponseEntity<Properties> upload(@RequestHeader(name = UID, required = false) String uid,
@RequestPart("file") MultipartFile multiPart) {
//Save Attachment.
}
也許您需要一個標頭,表明您的請求是多部分的。 這個答案能解決你的問題嗎? 需要的是標題注釋:
@PostMapping(path="/upload")
@Headers("Content-Type: multipart/form-data")
ResponseEntity<Properties> upload(@Param("file") MultipartFile multiPart);
這只是一個假設。 請試一試,讓我知道結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.