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