If you have a large object to download or it is time-consuming to download the entire object at a time, you can use streaming download. Streaming download enables you to download a portion of the object content each time until you download the entire object.

If you do not close the ossObject object after it is used, connection leaks may occur. Consequently, no connection is available and an exception occurs. The following code provides an example on how to close an ossObject object:

OSSObject ossObject = ossClient.getObject(bucketName, objectName);

The following code provides an example on how to download an object by using streaming download:

// This example uses the endpoint of the China(Hangzhou) region. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
// <yourObjectName> indicates the complete path of the object you want to download from OSS. The path must include the file extension of the object. For example, set <yourObjectName> to abc/efg/123.jpg.
String objectName = "<yourObjectName>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// ossObject includes the bucket name, object name, object metadata, and an input stream.
OSSObject ossObject = ossClient.getObject(bucketName, objectName);

// Read the object content.
System.out.println("Object content:");
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
while (true) {
    String line = reader.readLine();
    if (line == null) break;

    System.out.println("\n" + line);
// You must close the stream obtained by using ossClient.getObject after the object is read. Otherwise, connection leaks may occur. Consequently, no connections are available and an exception occurs.

// Shut down the OSSClient instance.

For the complete code of streaming download, see GitHub.