This topic describes how to download an object to a local file.

The following code provides an example on how to download an object to a specified local file:

// Download the object. GetObjectRequest get = new GetObjectRequest("BucketName”, "objectKey”);

oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
// Read the object data.
        long length = result.getContentLength();
        byte[] buffer = new byte[(int) length];
        int readCount = 0;
        while (readCount < length) {
            try{
                readCount += result.getObjectContent().read(buffer, readCount, (int) length - readCount);
            }catch (Exception e){
                OSSLog.logInfo(e.toString());
            }
        }
// Store the downloaded local file in the special path.
        try {
            FileOutputStream fout = new FileOutputStream("download_filePath");
            fout.write(buffer);
            fout.close();
        } catch (Exception e) {
            OSSLog.logInfo(e.toString());
        }
    }

    @Override
    public void onFailure(GetObjectRequest request, ClientException clientException,
                          ServiceException serviceException)  {

    }
});