Multipart File Upload Client Using Spring REST Template

In this example I am going to show you how to test file upload using Spring Boot Rest Template API. You can always use any REST based testing tools, such as, Postman, Talend, etc. but when you do not have any access to such REST based client tools, for example, your organization has blocked access to such REST client tools.

When you do not have access to such tools or installation for such tools is not allowed in your system then you can use RestTemplate to test the file upload functionality.

Prerequisites

Java 19, Spring Boot 3.1.0, Maven 3.8.5

You can go through the tutorial on Multiple Files Upload Using Spring REST API.

Project Setup

You can create a maven based project in your favorite tool or IDE. The following pom.xml file can be used for the project reference.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>spring-rest-multiple-files-upload-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.0</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

File Upload Client

The file upload client will test the file upload functionality using Spring’s RestTemplate API.

public static void testMultipleFilesUpload() throws IOException {
	LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
	map.add("multipleFiles",
			new FileSystemResource(new File("additional.properties")));
	map.add("multipleFiles", new FileSystemResource(
			new File("recommended pre-requisites.docx")));

	HttpHeaders headers = new HttpHeaders();
	headers.setContentType(MediaType.MULTIPART_FORM_DATA);

	HttpEntity<LinkedMultiValueMap<String, Object>> reqEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
			map, headers);

	RestTemplate restTemplate = new RestTemplate();

	ResponseEntity<String> resE = restTemplate.exchange("http://localhost:8080/upload/multiple/files",
			HttpMethod.POST, reqEntity, String.class);

	System.out.println(resE);
}

In the above method I have put the multiple files in the multipleFiles key of the map object. You will also have such key for file upload functionality in the server side code.

You need to configure the http header with multipart form data. Then you are going to call the REST API of file upload functionality using RestTemplate object.

Testing Files Upload Functionality

Once you run the client Java class for uploading files, you will see the similar kind of response as shown below:

<200 OK OK,File Name: additional.properties
File Name: recommended pre-requisites.docx
,[Content-Type:"text/plain;charset=UTF-8", Content-Length:"117", Date:"07:19:31 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

Hope you got an idea how to test multiple files upload functionality using Rest Template API.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *