How to Enable File Upload with Spring Cloud Feign: A Step‑by‑Step Guide
This article explains how to add multipart file‑upload support to Spring Cloud Feign by extending the client with feign‑form, configuring the server side with a Spring MVC controller, and providing a complete Java example with dependencies, client configuration, and a JUnit test.
Service Provider (File Receiver)
Implementation uses standard Spring MVC annotations to accept multipart/form‑data requests.
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@RestController
public class UploadController {
@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
return file.getName();
}
}Service Consumer (File Sender)
Add the following Maven dependencies to enable Feign multipart support.
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>Define the Feign client and a configuration class that supplies a multipart‑capable encoder.
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@FeignClient(value = "upload-server", configuration = UploadService.MultipartSupportConfig.class)
public interface UploadService {
@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
}
@Configuration
public class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}Test Case
A JUnit test creates a MultipartFile from a local file and invokes the Feign client.
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UploadTester {
@Autowired
private UploadService uploadService;
@Test
@SneakyThrows
public void testHandleFileUpload() {
File file = new File("upload.txt");
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory()
.createItem("file", MediaType.TEXT_PLAIN_VALUE, true, file.getName());
try (InputStream input = new FileInputStream(file);
OutputStream os = fileItem.getOutputStream()) {
IOUtils.copy(input, os);
}
MultipartFile multipart = new CommonsMultipartFile(fileItem);
log.info(uploadService.handleFileUpload(multipart));
}
}Full Example
The complete source code can be cloned from the following repositories:
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
