如果仅需要文件中的部分数据,您可以使用范围下载,下载指定范围内的数据。
//objectKey等同于objectName,表示下载文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>"); // 设置范围 get.setRange(new Range(0, 99)); // 下载0到99字节共100个字节,文件范围从0开始计算 // get.setRange(new Range(100, Range.INFINITE)); // 下载从100个字节到结尾 OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() { @Override public void onSuccess(GetObjectRequest request, GetObjectResult result) { // 请求成功 InputStream inputStream = result.getObjectContent(); byte[] buffer = new byte[2048]; int len; try { while ((len = inputStream.read(buffer)) != -1) { // 处理下载的数据 } } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) { // 请求异常 if (clientExcepion != null) { // 本地异常,如网络异常等 clientExcepion.printStackTrace(); } if (serviceException != null) { // 服务异常 Log.e("ErrorCode", serviceException.getErrorCode()); Log.e("RequestId", serviceException.getRequestId()); Log.e("HostId", serviceException.getHostId()); Log.e("RawMessage", serviceException.getRawMessage()); } } });