Spring Boot File Download - Hello World Example
- The Controller return type is of type void and add HttpServletResponse as an argument to the method.
- In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally. Set the Content-Disposition accordingly.
-
Find the MIME Type of the file to be downloaded. This can be
application/pdf, text/html,application/xml etc.
If the mimetype is of unknown type, then it is set to application/octet-stream. In this case the browser displays the Save As - Copy bytes to the response OutputStream from the InputStream.
Video
This tutorial is explained in the below Youtube Video.Lets Begin-
Create the Maven Project as follows -The pom.xml will be as follows -
<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.javainuse</groupId> <artifactId>boot-filedownload</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>