Spring Boot + GZip compression (2024)
In this tutorial, we'll enable GZip compression in a Spring Boot project and show how, while downloading a file, gzip compression compresses the file and returns the response.
What is GZip compression?
Gzip is a powerful file compression technique that can compress a web page to some extent. It is used to reduce loading time and enhancing user performance. When a user requests a file, gzip, when enabled within the spring boot, helps in reducing the size of the file.
GZip compression is a quick and easy solution to reduce bandwidth and boost the website speed.
It reduces the response time of our website simply compressing the resources before sending them to clients. It saves at least 50% of the bandwidth.
Take a look at our suggested posts:
Let's start developing Spring Boot application.
Project Structure
Maven Dependency
Add dependencies for Spring Boot project.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>SpringBootGzipComp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootGzipComp</name>
<description>Spring Boot Gzip Compression</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<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>
application.properties
Spring Boot by default disables GZip compression. Add the below properties to application.properties
to enable it.
server.compression.enabled=true
server.compression.mime-types=application/json, application/xml,text/html,image/jpeg
Rest Controller
Create the rest controller class to provide file download rest endpoint for testing Gzip compression.
package com.techgeeknext.controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
@RestController
public class SpringBootGzipCompController {
private static final String FILES_STORAGE_PATH = "D:/files/";
@RequestMapping("/file/{fileName:.+}")
public void downloadFile(HttpServletRequest request, HttpServletResponse response,
@PathVariable("fileName") String fileName) throws IOException {
File file = new File(FILES_STORAGE_PATH + fileName);
if (file.exists()) {
String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if (mimeType == null) {
//in case mimetype not available, set to default mimetype
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
// set downloaded file as inline file
response.setHeader("Content-Disposition",
String.format("inline; filename=\"" + file.getName() + "\""));
//set downloaded file as attachment
// response.setHeader("Content-Disposition",
// String.format("attachment; filename=\"" + file.getName() + "\""));
response.setContentLength((int) file.length());
//FileCopyUtils.copy utility methods for file and stream copying.
//All copy methods use a block size of 4096 bytes, and close all affected streams when done.
FileCopyUtils.copy(new BufferedInputStream(new FileInputStream(file)),
response.getOutputStream());
}
}
}
Test
Execute mvn spring-boot run
command to start spring boot application.
Once application has started, use the rest end point with image path http://localhost:8080/file/techgeeknext.jpg stored
at D:/files
folder.
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot + GZip compression Example