[JAVA] Download with Spring Boot



I want to download without screen transition, but I was a little disappointed to use the HttpResponseHeader class, so I implemented it using only the Spring package class. Note that the client-side implementation is likely to be forgotten.



public ResponseEntity<byte[]> download() {
    byte[] data = xxx; // xxx:byte[]Format object
    // ResponseHeader
    HttpHeaders header = new HttpHeaders();
    header.add("Content-Type", "yyy"); // yyy:Any Content-Type
    header.add("Content-Disposition", "attachment; filename*=utf-8''" + URLEncoder.encode("zzz", "UTF-8")); // zzz:Arbitrary file name
    header.add("Set-Cookie", "fileDownload=true; path=/");

    return new ResponseEntity<byte[]>(data, header, HttpStatus.OK);


//Add event when submitting form
$("#myform").submit(function() {
  startLoading(); //Round and round method
  const COOKIE_KEY_FILEDOWNLOAD = 'fileDownload=';
  var isFileDownload = false;
  //Repeat until download is complete
  var intervalId = setInterval(function() {
    //Get fileDownload from cookie
    const COOKIES = document.cookie;
    var position = COOKIES.indexOf(COOKIE_KEY_FILEDOWNLOAD);
    if (position >= 0) {
      var startIdx = position + COOKIE_KEY_FILEDOWNLOAD.length;
      var endIdx = COOKIES.indexOf(';', startIdx);
      if (endIdx < 0) {
        endIdx = COOKIES.length;
      isFileDownload = decodeURIComponent(COOKIES.substring(startIdx, endIdx)) == 'true';
    //If fileDownload is true, it ends repeatedly
    if (isFileDownload) {
      var date = new Date();
      date.setTime(date.getTime() - 1);
      document.cookie = COOKIE_KEY_FILEDOWNLOAD + 'false; path=/; max-age=0';
      stopLoading(); //A method to stop round and round
  }, 500);


Recommended Posts

Download with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
Implement file download with Spring MVC
(Intellij) Hello World with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Local file download memorandum in Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Start web application development with Spring Boot
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Spring Boot environment construction with Docker (January 2021 version)
Create a website with Spring Boot + Gradle (jdk1.8.x)
Configure Spring Boot application with maven multi module
SPRING BOOT learning record 01
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Spring Boot + Heroku Postgres
Create a simple search app with Spring Boot