aliyun / aliyun-oss-java-sdk

Aliyun OSS SDK for Java
1.22k stars 805 forks source link

带进度条的上传,使用FileInputStream,无法执行case RESPONSE_CONTENT_LENGTH_EVENT #124

Open hyhub opened 6 years ago

hyhub commented 6 years ago

带进度条的上传,使用FileInputStream,无法执行case RESPONSE_CONTENT_LENGTH_EVENT eg: public static void main(String[] args) {

    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    try {
        //File fh = createSampleFile();
        FileInputStream fh = new FileInputStream("uploadFile");
        // 带进度条的上传 
        client.putObject(new PutObjectRequest(bucketName, key, fh).
                <PutObjectRequest>withProgressListener(new PutObjectProgressListener()));

// // 带进度条的下载 // client.getObject(new GetObjectRequest(bucketName, key). // withProgressListener(new GetObjectProgressListener()), fh); // } catch (Exception e) { e.printStackTrace(); } }

ljluestc commented 1 year ago

import com.aliyun.oss.event.ProgressEvent; import com.aliyun.oss.event.ProgressEventType; import com.aliyun.oss.event.ProgressListener;

public class PutObjectProgressListener implements ProgressListener {

@Override
public void progressChanged(ProgressEvent progressEvent) {
    switch (progressEvent.getEventType()) {
        case TRANSFER_STARTED_EVENT:
            System.out.println("Upload started");
            break;
        case REQUEST_CONTENT_LENGTH_EVENT:
            System.out.println("Request content length: " + progressEvent.getBytes());
            break;
        case RESPONSE_CONTENT_LENGTH_EVENT:
            System.out.println("Response content length: " + progressEvent.getBytes());
            break;
        case TRANSFER_COMPLETED_EVENT:
            System.out.println("Upload completed");
            break;
        case TRANSFER_FAILED_EVENT:
            System.out.println("Upload failed");
            break;
        default:
            break;
    }
}

}