How To Download A File Directly From URL In Spring Boot
Posted By : Kaushlendra Kumar Pal | 18-Jun-2018
In this blog, we will discuss how to download a file directly from Server using direct download link URL using StreamingResponseBody in Spring boot. To download a file Using stream by StreamingResponseBody is possible. Here server will write data to OutputStream and at the same time browser will read data without holding up the Servlet container thread. So StreamingResponseBody writing and reading is possible to parallel. It is very useful for download large file from the server to the client.
Here we will see following three methods to download a file directly to the client easily:
1. Download File Using StreamingResponseBody
StreamingResponseBody is a functional interface. It can also be used as the assignment target for a method reference or a lambda expression. Here is Spring boot example to download a file but this code can also be used spring MVC as well.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@RestController // It is also possible to @Controller.
public class StreamingResponseBodyController {
@RequestMapping(value = "downloadTestFile", method = RequestMethod.GET)
public StreamingResponseBody getSteamingFile(HttpServletResponse response) throws IOException {
if(DownloadAllowCondition...){
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
InputStream inputStream = new FileInputStream(new File("C:\\test-file.pdf"));
return outputStream -> {
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
System.out.println("Writing some bytes of file...");
outputStream.write(data, 0, nRead);
}
};
} else {
response.sendRedirect("failed url...");
}
}
}
You can download a file directly by using direct download link URL. http://localhost:8080/downloadTestFile
2. Download File using InputStream to HttpServletResponse
To read files in java we can use Reader or Stream. But for text data Reader is the good option to use but for binary data, we should use Stream. To open the stream FileInputStream is used to read data from the file.
@RequestMapping(value = "downloadTestFile", method = RequestMethod.GET)
public void getSteamingFile1(HttpServletResponse response) throws IOException {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
InputStream inputStream = new FileInputStream(new File("C:\\test-file.pdf"));
int nRead;
while ((nRead = inputStream.read()) != -1) {
response.getWriter().write(nRead);
}
}
3. Download File using InputStreamResource
@RequestMapping(value = "downloadTestFile", method = RequestMethod.GET)
public InputStreamResource FileSystemResource (HttpServletResponse response) throws IOException {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"test.pdf\"");
InputStreamResource resource = new InputStreamResource(new FileInputStream("C:\\test-file.pdf"));
return resource;
}
I hope you guys learned something useful by me.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kaushlendra Kumar Pal
Kaushlendra is an experienced Java Developer with comprehensive mastery of all Java packages. Able to Write well designed, testable, efficient code as per the requirements and handle different types of issues as well as functions.