1

對不起我的英語不好:(圖像與Spring和假死客戶受損

我要上傳與假死客戶端的圖像文件,但圖像在服務器上的應用程序損壞。

// CLIENT APP 
@FeignClient(name = "media-client", url = "${api.base-path}/media") 
public interface MediaClient { 
    @PostMapping 
    String uploadMedia(@RequestPart("file") MultipartFile file); 
} 

// SERVER APP 
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
String uploadMedia(@RequestPart MultipartFile file) throws IOException { 
    Files.copy(file.getInputStream(), Paths.get("/home/m/Desktop").resolve(UUID.randomUUID().toString() + ".jpg")); 
    return null; 
} 

同一圖像保存客戶端應用程序和服務器應用程序,但結果如下:?

https://i.stack.imgur.com/xbiPS.png

什麼問題請大家幫我

+0

我猜你已經在本地測試過這個文件,它在上傳之前沒有損壞? – sniperd

+0

@sniperd是的,我已經在本地測試過文件。上傳後它已損壞。 – mkrgl

回答

0

我建議使用Feign編碼器用於多部分/表格數據格式。 步驟:

首先,包含這個依賴於你的pom.xml:

<dependency> 
    <groupId>io.github.openfeign.form</groupId> 
    <artifactId>feign-form</artifactId> 
    <version>2.2.1</version> 
</dependency> 

然後,添加該配置:

@Configuration 
public class FeignClientConfiguration { 
    @Bean 
    @Primary 
    @Scope("prototype") 
    public Encoder encoder() { 
     return new SpringFormEncoder(); 
    } 
} 

,改變你的註釋:

@FeignClient(name = "media-client", url = "${api.base-path}/media", configuration = FeignClientConfiguration.class) 
+0

Hi @Shchipunov。感謝您的回答。我已經添加了這些依賴關係。文件已上傳,但在服務器應用程序中已損壞。 – mkrgl