上传视频到阿里云服务器

2021-02-10 11:18

阅读:339

标签:owa   size   version   大小   org   scl   上传   multi   服务   

######上传视频到阿里云服务器工具类
1.maven坐标

com.aliyun
		aliyun-java-sdk-sts
		2.1.6com.aliyun
		aliyun-java-sdk-core
		2.2.0com.aliyun
		aliyun-java-sdk-ecs
		2.2.0com.aliyun
		aliyun-java-sdk-mts
		2.1.6com.aliyun.oss
		aliyun-sdk-oss
		2.5.0org.apache.poi
		poi
		3.14

  2.工具类

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.AddTemplateRequest;
import com.aliyuncs.mts.model.v20140618.AddTemplateResponse;
import com.aliyuncs.mts.model.v20140618.DeleteTemplateRequest;
import com.aliyuncs.mts.model.v20140618.DeleteTemplateResponse;
import com.aliyuncs.mts.model.v20140618.QueryJobListRequest;
import com.aliyuncs.mts.model.v20140618.QueryJobListResponse;
import com.aliyuncs.mts.model.v20140618.SearchPipelineRequest;
import com.aliyuncs.mts.model.v20140618.SearchPipelineResponse;
import com.aliyuncs.mts.model.v20140618.SubmitJobsRequest;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse.JobResult;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse.JobResult.Job;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse.JobResult.Job.Output;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse.JobResult.Job.Output.OutputFile;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.mailsend.UUIDUtil;
import com.rest.entity.Parament;
public class VedioUtil {
	  /**
	   * 断点上传文件
	   * @param endpoint 
	   * @param accessKeyId
	   * @param accessKeySecret
	   * @param fileurl  文件路径
	   * @param bucket 传入的bucket名称
	   * @param filename  文件名称
	   * @param location 传入的bucket服务区
	   * @throws Throwable 
	   */
	 public String uploadFile(String endpoint,String accessKeyId,String accessKeySecret,String fileurl,String bucket,String filename ,String location,String templateId) throws Throwable{
		 filename=UUIDUtil.uuid();
		 // 创建OSSClient实例
		 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		 // 设置断点续传请求
		 UploadFileRequest uploadFileRequest = new UploadFileRequest(bucket, filename);
		 // 指定上传的本地文件
		 uploadFileRequest.setUploadFile(fileurl);
		 // 指定上传并发线程数
		 uploadFileRequest.setTaskNum(5);
		 // 指定上传的分片大小
		 uploadFileRequest.setPartSize(1 * 1024 * 1024);
		 // 开启断点续传
		 uploadFileRequest.setEnableCheckpoint(true);
		 System.out.println("开始上传视频.....");
		 // 断点续传上传
		 ossClient.uploadFile(uploadFileRequest);
		 System.out.println("上传成功!");
		 System.out.println("开始转码...");
		 String url=transcoding(bucket, location, filename, accessKeyId, accessKeySecret,templateId);
		 System.out.println("转码后播放地址:"+url);
		 //删除指定对象
		  ossClient.deleteObject(bucket, filename);
		 // 关闭client
		 ossClient.shutdown();
		 return url;
	 }
	/**
	 * 视频转码
	 * @param bucket 输入bucket名称
	 * @param location 输入服务区
	 * @param filename 文件名
	 * @param accessKeyId
	 * @param accessKeySecret
	 * @return 转码后播放地址
	 * @throws ClientException
	 */
	  public String transcoding(String bucket,String location,String filename,String accessKeyId,String accessKeySecret,String templateId) throws ClientException{
		  System.out.println(bucket+"--"+location+"--"+templateId);
		  String url="";
		  if(location.equals("oss-cn-beijing")){
			  //华北2
			  DefaultProfile.addEndpoint("cn-beijing",
		              "cn-beijing",
		              "Mts",
		              "mts.cn-beijing.aliyuncs.com");
		  }else if(location.equals("oss-cn-shanghai")){
			//华东2
			  DefaultProfile.addEndpoint("cn-shanghai",
		              "cn-shanghai",
		              "Mts",
		              "mts.cn-shanghai.aliyuncs.com");
		  }else if(location.equals("oss-cn-hangzhou")){
			  //华东1
			  DefaultProfile.addEndpoint("cn-hangzhou",
		              "cn-hangzhou",
		              "Mts",
		              "mts.cn-hangzhou.aliyuncs.com");
		  }else if(location.equals("oss-cn-shenzhen")){
			//华南1
			  DefaultProfile.addEndpoint("cn-shenzhen",
		              "cn-shenzhen",
		              "Mts",
		              "mts.cn-shenzhen.aliyuncs.com");
		  }
	 
	  //开始转码
	   SubmitJobsRequest submitjobs=new SubmitJobsRequest();
	   String input="{\"Bucket\":\""+bucket+"\",\"Location\":\""+location+"\",\"Object\":\""+filename+"\"}";
	   String outputs="[{\"OutputObject\":\"_"+filename+"\",\"TemplateId\":\""+templateId+"\"}]";
	   String outputbucket=bucket;
	   String OutputLocation=location;
	   submitjobs.setOutputLocation(OutputLocation);
	   submitjobs.setInput(input);
	   submitjobs.setOutputs(outputs);
	   submitjobs.setOutputBucket(outputbucket);
	   //创建IClientProfile
	   IClientProfile profile=DefaultProfile.getProfile(location.substring(4), accessKeyId, accessKeySecret);
	   IAcsClient client=new DefaultAcsClient(profile);
	   try {
		   //获取管道
		   SearchPipelineRequest searchPipeline=new SearchPipelineRequest();
		   SearchPipelineResponse sarchPipelineResponse=client.getAcsResponse(searchPipeline);
		   String pipelineid=sarchPipelineResponse.getPipelineList().get(0).getId();
		   System.out.println("管道id="+pipelineid);
		   //给管道赋值
		   submitjobs.setPipelineId(pipelineid);
		   SubmitJobsResponse response=client.getAcsResponse(submitjobs);
		   List list= response.getJobResultList();
		   JobResult jobResult=list.get(0);
		   Job job=jobResult.getJob();
		   Output output=job.getOutput();
		   OutputFile outputfile=output.getOutputFile();
		   url="http://"+outputfile.getBucket()+"."+outputfile.getLocation()+".aliyuncs.com/"+outputfile.getObject();
		   String jobid=job.getJobId();
		   System.out.println("转码任务id="+jobid);
		   QueryJobListRequest queryjoblist=new QueryJobListRequest();
		   queryjoblist.setJobIds(jobid);
		   for(int i=0;i

  

上传视频到阿里云服务器

标签:owa   size   version   大小   org   scl   上传   multi   服务   

原文地址:https://www.cnblogs.com/QuickSlow/p/13051450.html


评论


亲,登录后才可以留言!