File Transfer Application with Java and Grpc Spring services Part: 1
By: Münir Karslı
Tags: Grpc, Spring, Java

We will tell about how to transfer any file from server to client using java and grpc in this article.

Simple Project Implementation

I used https://start.spring.io/ for create a new project using the following dependencies:

Spring Web

Lombok

Configure The Project

We will investigate our pom.xml file that contains basic libraries.

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>



<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>



<!-- gRPC Dependencies -->

<dependency>

<groupId>io.grpc</groupId>

<artifactId>grpc-netty</artifactId>

<version>1.16.1</version>

</dependency>

<dependency>

<groupId>io.grpc</groupId>

<artifactId>grpc-protobuf</artifactId>

<version>1.16.1</version>

</dependency>

<dependency>

<groupId>io.grpc</groupId>

<artifactId>grpc-stub</artifactId>

<version>1.16.1</version>

</dependency>



<!-- grpc-spring-boot-starter -->

<dependency>

<groupId>io.github.lognet</groupId>

<artifactId>grpc-spring-boot-starter</artifactId>

<version>3.1.0</version>

</dependency>



</dependencies>

I used especially this plugin for generating the GRPC code based on the file defined in src/main/proto/

<plugin>

<groupId>org.xolstice.maven.plugins</groupId>

<artifactId>protobuf-maven-plugin</artifactId>

<version>0.6.1</version>

<configuration>

<protocArtifact>

com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}

</protocArtifact>

<pluginId>grpc-java</pluginId>

<pluginArtifact>

io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}

</pluginArtifact>

</configuration>

<executions>

<execution>

<goals>

<goal>compile</goal>

<goal>compile-custom</goal>

</goals>

</execution>

</executions>

</plugin>

end of the configurations on the application.properties.

grpc.port=7000



fileservice.grpc.host=localhost

fileservice.grpc.port=7000

Project Structure

We have really easy project structure.

We have one “.proto” file that contains following code.

Project Import

Just run “mvn clean package” and “mvn clean install”. “mvn clean package” is important for us because it generates the GRPC code(target/generated-sources/protobuf/) based on the file defined in the proto file (src/main/proto/)

Service Implementation

we just take a file and convert it byte array for sending the data to client.

Client Implementation

we consume the grpc service. We have 3 important methods;

onNext

2. onError

3. onCompleted

I think these are really clear.

Let’s Test

just give file path and execute the downloadFile()on the FileServiceApplicationTests.java

What We Did and Will

Actually this is basically file transfer structure with java 8, grpc and Spring. We just chose 1 file and wanted to it from server. In this part, we assume that we know file location and name in the server. The next part we will ask server what do you have and choose files from them.

for all source code

For Part 2

https://munirkarsli.medium.com/file-transfer-application-with-java-and-grpc-spring-services-part-2-8826573a0fe6