概述
bucketName ---oss上bucket的名字
key ---文件所在的文件夹加文件名
例如:我想把ceshi.txt 存放在bucket叫aa的里面叫bb的文件夹下。那么我的bucketName传的值就是 aa,key值是bb/ceshi.txt
public String multipartUploadObject(String bucketName, String key,File partFile) {
String tag = null;
String uploadid = null;
int j=0;
// 初始化一个OSSClient
OSSClient client = this.createOSSClient();
ListMultipartUploadsRequest lmur = new ListMultipartUploadsRequest(bucketName);
// 获取Bucket内所有上传事件
MultipartUploadListing listing = client.listMultipartUploads(lmur);
// 新建一个List保存每个分块上传后的ETag和PartNumber
List partETags = new ArrayList();
// 遍历所有上传事件 设置UploadId
for (MultipartUpload multipartUpload : listing.getMultipartUploads()) {
if (multipartUpload.getKey().equals(key)) {
uploadid=multipartUpload.getUploadId();
break;
}
}
if(StringUtils.isEmpty(uploadid)){
// 开始Multipart Upload,InitiateMultipartUploadRequest 来指定上传Object的名字和所属Bucke
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
uploadid=initiateMultipartUploadResult.getUploadId();
}else{
ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName,key, uploadid);
//listParts 方法获取某个上传事件所有已上传的块
PartListing partListing = client.listParts(listPartsRequest);
// 遍历所有Part
for (PartSummary part : partListing.getParts()) {
partETags.add(new PartETag(part.getPartNumber(),part.getETag()));
j++;
}
}
// 设置每块为 5M(最小支持5M)
final int partSize = 1024 * 1024 * 5;
// 计算分块数目
int partCount = (int) (partFile.length() / partSize);
if (partFile.length() % partSize != 0) {
partCount++;
}
try {
for (int i=j ; i < partCount; i++) {
// 获取文件流
FileInputStream fis;
fis = new FileInputStream(partFile);
// 跳到每个分块的开头
long skipBytes = partSize * i;
fis.skip(skipBytes);
// 计算每个分块的大小
long size = partSize < partFile.length() - skipBytes ? partSize: partFile.length() - skipBytes;
// 创建UploadPartRequest,上传分块
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(key);
uploadPartRequest.setUploadId(uploadid);
uploadPartRequest.setInputStream(fis);
uploadPartRequest.setPartSize(size);
uploadPartRequest.setPartNumber(i + 1);
UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
// 将返回的PartETag保存到List中。
partETags.add(uploadPartResult.getPartETag());
// 关闭文件
fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadid, partETags);
// 完成分块上传
CompleteMultipartUploadResult completeMultipartUploadResult = client.completeMultipartUpload(completeMultipartUploadRequest);
// 打印Object的ETag(返回的ETag不是md5.具体是什么不详)
tag = completeMultipartUploadResult.getETag();
return tag;
}
最后
以上就是无心棉花糖为你收集整理的java oss 断点上传文件_java实现oss断点续传的全部内容,希望文章能够帮你解决java oss 断点上传文件_java实现oss断点续传所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复