Module bus.http

Class FileCallback

java.lang.Object
org.miaixz.bus.http.plugin.httpz.FileCallback
All Implemented Interfaces:
Callback

public abstract class FileCallback extends Object implements Callback
An abstract Callback implementation for handling responses that should be saved to a file. It processes the response body and streams it to a file or provides an InputStream to the appropriate callback.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • FileCallback

      public FileCallback()
      Default constructor. When used, the response body will be delivered as an InputStream to the onSuccess(NewCall, InputStream, String) callback.
    • FileCallback

      public FileCallback(String fileAbsolutePath)
      Constructor that specifies a destination file path. When used, the response body will be saved to this file, and the onSuccess(NewCall, File, String) callback will be invoked.
      Parameters:
      fileAbsolutePath - The absolute path of the file to save the response body to.
  • Method Details

    • onResponse

      public void onResponse(NewCall call, Response response, String id)
      Handles the successful HTTP response. This method either saves the response body to the specified file or provides it as an InputStream to the appropriate onSuccess callback.
      Specified by:
      onResponse in interface Callback
      Parameters:
      call - The NewCall that resulted in this response.
      response - The HTTP Response.
      id - The unique ID of the request.
    • onSuccess

      public void onSuccess(NewCall call, File file, String id)
      Callback invoked when the response body has been successfully saved to a file. Subclasses should override this method to handle the downloaded file.
      Parameters:
      call - The NewCall that resulted in this response.
      file - The File where the response body was saved.
      id - The unique ID of the request.
    • onSuccess

      public void onSuccess(NewCall call, InputStream fileStream, String id)
      Callback invoked when no destination file is specified. The response body is provided as an InputStream. Subclasses should override this method to process the stream. Note: The caller is responsible for closing the stream.
      Parameters:
      call - The NewCall that resulted in this response.
      fileStream - An InputStream of the response body.
      id - The unique ID of the request.