阿里云文件上传(二)

阿里云文件上传(二)

追加上传(文件)

在这里插入图片描述

public static  String appendObjectFileFirst(String bucketName,String objectPath,String contentType,String localPath){
   
        //contentType 常用值如下
        //纯文本:Content-Type text/plain
        // JPG:image/jpeg  gif:image/gif png:image/png  word:application/msword
        //jsp:text/html mp3:audio/mp3 mp4:video/mpeg4 ppt:application/vnd.ms-powerpoint
        //localPath:所选择本地文件的路径
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(contentType);

        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName,objectPath,new File(localPath),metadata);

        /*
         通过AppendObjectRequest设置单个参数。
         设置Bucket名称。
        appendObjectRequest.setBucketName("<yourBucketName>");
         设置Object名称。即不包含Bucket名称在内的Object的完整路径,例如example/test.txt。
        appendObjectRequest.setKey("<yourObjectName>");
         设置待追加的内容。有两种可选类型:InputStream类型和File类型。这里为InputStream类型。
        appendObjectRequest.setInputStream(new ByteArrayInputStream(content1.getBytes()));
         设置待追加的内容。有两种可选类型:InputStream类型和File类型。这里为File类型。
        appendObjectRequest.setFile(new File("<yourLocalFile>"));
         指定文件的元信息,第一次追加时有效。
        appendObjectRequest.setMetadata(meta);
         第一次追加。
         设置文件的追加位置。
        */

        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        // 文件的64位CRC值。此值根据ECMA-182标准计算得出。
        System.out.println(appendObjectResult.getObjectCRC());

        String position = appendObjectResult.getNextPosition().toString();

        System.out.println(position);


        // 关闭OSSClient。
        ossClient.shutdown();

        return position;
    }

在这里插入图片描述

//追加上传文件
    public static  String appendObjectFile(String bucketName,String objectPath,String contentType,String localPath,String givenPosition){
   

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

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(contentType);

        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName,objectPath,new File(localPath),metadata);

        // 通过AppendObjectRequest设置单个参数。
        // 设置Bucket名称。
        //appendObjectRequest.setBucketName("<yourBucketName>");
        // 设置Object名称。即不包含Bucket名称在内的Object的完整路径,例如example/test.txt。
        //appendObjectRequest.setKey("<yourObjectName>");
        // 设置待追加的内容。有两种可选类型:InputStream类型和File类型。这里为InputStream类型。
        //appendObjectRequest.setInputStream(new ByteArrayInputStream(content1.getBytes()));
        // 设置待追加的内容。有两种可选类型:InputStream类型和File类型。这里为File类型。
        //appendObjectRequest.setFile(new File("<yourLocalFile>"));
        // 指定文件的元信息,第一次追加时有效。
        //appendObjectRequest.setMetadata(meta);


        appendObjectRequest.setPosition(Long.parseLong(givenPosition));
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        // 文件的64位CRC值。此值根据ECMA-182标准计算得出。
        System.out.println(appendObjectResult.getObjectCRC());

        String position = appendObjectResult.getNextPosition().toString();

        System.out.println(position);


        // 第二次追加。
        // nextPosition指明下一次请求中应当提供的Position,即文件当前的长度。
//        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
//        appendObjectRequest.setInputStream(new ByteArrayInputStream(content2.getBytes()));
//        appendObjectResult = ossClient.appendObject(appendObjectRequest);
//        // 第三次追加。
//        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
//        appendObjectRequest.setInputStream(new ByteArrayInputStream(content3.getBytes()));
//        appendObjectResult = ossClient.appendObject(appendObjectRequest);

        // 关闭OSSClient。
        ossClient.shutdown();

        return position;
    }

断点续传上传

在这里插入图片描述


    public static String checkPointUpload(String bucketName, String objectPath, String localFilePath, String contentType,int taskNum,int partSize) throws Throwable {
   
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(contentType);

        // 文件上传时设置访问权限ACL。
        // meta.setObjectAcl(CannedAccessControlList.Private);

        // 通过UploadFileRequest设置多个参数。
        // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName,objectPath);

        // 通过UploadFileRequest设置单个参数。
        // 填写Bucket名称。
        //uploadFileRequest.setBucketName("examplebucket");
        // 填写Object完整路径。Object完整路径中不能包含Bucket名称。
        //uploadFileRequest.setKey(objectPath);
        // 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值