SoFunction
Updated on 2025-04-06

Detailed explanation of how to use SpringBoot to download JSON files

Overview

Implement the file download function in Spring Boot, which can be returned to the client by returning a JSON string as file content. Here are the implementation steps and code examples:

Implementation steps

Define the interface: Create a REST controller that handles file download requests.

Set response header: Set response header through HttpServletResponse, specify the file type and download file name.

Returns file content: Writes a JSON string to the response output stream.

Code implementation

1. Create Spring Boot Controller

import ;
import ;
import ;
 
import ;
import ;
import ;
 
@RestController
@RequestMapping("/api")
public class FileDownloadController {
 
    @GetMapping("/download-json")
    public void downloadJsonFile(HttpServletResponse response) throws IOException {
        // 1. Prepare the JSON string        String jsonContent = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
 
        // 2. Set the response header        ("application/json"); // File type is JSON        ("Content-Disposition", "attachment; filename=\"\""); // Set the download file name 
        // 3. Write JSON string to the response output stream        ().write((StandardCharsets.UTF_8));
        ().flush();
    }
}

2. Run and test

Launch the Spring Boot app.

Access the interface address in the browser, for example: http://localhost:8080/api/download-json.

The browser will automatically download a file named , with the file contents as:

{"name":"John", "age":30, "city":"New York"}

Key points description

  • Used to set the response header and output stream.
  • setContentType("application/json"): Specify the file type to JSON.
  • setHeader("Content-Disposition", "attachment; filename=\"\""): Specify the file name and tell the browser to download it in attachment form.

2. Write file content:

  • Use ().write() to write a JSON string to the response output stream.
  • Make sure to use StandardCharsets.UTF_8 to specify character encoding to avoid garbled problems.

3. File name:

File names can be generated dynamically as needed, for example:

String fileName = "data_" + () + ".json";
("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

Extended features

Dynamically generate JSON content: You can obtain data from a database or other services and dynamically generate JSON strings.

Download compressed file: If you need to download compressed files (such as .zip), you can use ZipOutputStream to package multiple files and return them.

Large file download: For large files, you can use Transfer-Encoding: chunked to optimize performance.

Example: Dynamically generate JSON content

@GetMapping("/download-dynamic-json")
public void downloadDynamicJsonFile(HttpServletResponse response) throws IOException {
    // Dynamically generate JSON data    Map<String, Object> data = new HashMap<>();
    ("timestamp", ());
    ("message", "Hello, this is a dynamic JSON file!");
 
    // Convert Map to JSON string    String jsonContent = new ObjectMapper().writeValueAsString(data);
 
    // Set response header    ("application/json");
    ("Content-Disposition", "attachment; filename=\"dynamic_data.json\"");
 
    // Write response output stream    ().write((StandardCharsets.UTF_8));
    ().flush();
}

Visit http://localhost:8080/api/download-dynamic-json and a JSON file containing dynamically generated content will be downloaded.

This is the end of this article about how to use SpringBoot to download JSON files. For more related SpringBoot content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!