SoFunction
Updated on 2025-04-06

How to download files to local via http request using RestTemplate

background

Recently, a live, purely physical repetitive work was arranged to download the designated resources for developing a project through the existing download interface.

Ideas

Because there is no batch download interface provided, and the downloaded resources need to be filtered by themselves, thinking that manual processing is particularly troublesome and personal progress has not made much progress, I just want to write a piece of code to process it. This will be accurate and efficient. If you have similar tasks in the future, you can change this code and continue to use it.

1. Filter

Filter out the id of the required download file. This can be adjusted according to the business, not necessarily the id. The specific implementation is implemented according to the business logic;

2. Download

There are two ways to download. One is to build parameters in advance and provide files with parameters written in batches through tools such as postman or apifox, which is just a rough idea that it can be implemented without coding; the other is to initiate http requests with the help of back-end code and write the downloaded resources into the local file. The second type is briefly introduced below, and you can download files in batches when needed.

Implementation method

  • Resource download method
public static void downloadFile(String url, HttpHeaders headers, RequestParams requestParams, String outputPath) throws IOException {


        // Create a request entity        HttpEntity<RequestParams> entity = new HttpEntity<>(requestParams, headers);
        // Initialize RestTemplate, you can also build the configuration class yourself.        final RestTemplate restTemplate = new RestTemplate();
        // Request configuration        ResponseEntity<Resource> response = (
                (url),
                ,
                entity,
                
        );

        // Request for successful verification        if (().is2xxSuccessful() && () != null) {
            // Get resources            Resource resource = ();

            // Make sure the directory exists            File outputFile = new File(outputPath);
            File parentDir = ();
            // File exists to verify            if (parentDir != null && !()) {
                if (!()) {
                    throw new IOException("Failed to create directory: " + parentDir);
                }
            }
            //Get stream            try (InputStream inputStream = ();
                 OutputStream outputStream = (())) {
                (inputStream, outputStream);
            }
        } else {
            throw new IOException("Failed to download file: " + ());
        }
    }
  • Entity class (can be defined by yourself)
@Data
public class RequestParams implements Serializable {
    private Long id;
}
  • Function Calls
    public static void downloadPerPaper(PaperInfo paperInfos) throws IOException {
		//url
        String url="";
        //Request header        HttpHeaders headers = new HttpHeaders();
        ("xxx","xxxxx");
        //Request parameters        final RequestParams requestParams = new RequestParams();
        (1L);
        //There is a storage address        String path="xx/xx/";
        //Call the download method        downloadFile(url,headers,requestParams,path);
    }

Exception handling

Exception in thread "main" : xx/xx/xx

This kind of error occurs because the specific file must be specified, not a folder. At the same time, try to ensure that the folder where the files are stored must exist.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.