Tencent Cloud VOD uploads video files to solve the path problem

Youmans_station2022-08-06 07:54:23

Tencent Cloud VOD needs,视频的路径.
思路一,MultipartFileGet the file path from the cache-失败
思路二,Store the horseshoe pat file stream locally,Then set the local path up.
废话少说,直接上代码.

 //上传视频
@Override
public String uploadVideo(MultipartFile file) {

try {

VodUploadClient client =
new VodUploadClient(ConstantPropertiesUtil.ACCESS_KEY_ID,
ConstantPropertiesUtil.ACCESS_KEY_SECRET);
VodUploadRequest request = new VodUploadRequest();
// String fileName = UUID.randomUUID() + "." + originalFilename.split("\\.")[originalFilename.split("\\.").length - 1];
//Get the compiled file location
// File f = new File(this.getClass().getResource("/").getPath());
// String str = f.getPath() ;387702304156452066
// System.err.println(str);
// String bb= str.substring(0,str.lastIndexOf("target")) ;
//copyFile(inputStream,bb);
String fileName = file.getOriginalFilename();
fileName = UUID.randomUUID() + "." + fileName.split("\\.")[fileName.split("\\.").length - 1];
// Get local video location
this.tempVideoPath = File.listRoots()[1].getPath()+File.separator+"data"+File.separator;
File ff=new File(this.tempVideoPath);
if(!ff.exists()){

ff.mkdirs() ;
}
file.transferTo(new File(this.tempVideoPath + fileName));
String filePath = this.tempVideoPath+fileName;
System.out.println(this.tempVideoPath+fileName);
//视频本地地址
request.setMediaFilePath(filePath);
//指定任务流
request.setProcedure("LongVideoPreset");
//调用上传方法,传入接入点地域及上传请求.
VodUploadResponse response = client.upload("ap-guangzhou", request);
//返回文件id保存到业务表,用于控制视频播放
String fileId = response.getFileId();
System.out.println("Upload FileId = {}"+response.getFileId());
new File(filePath).delete();
return fileId;
} catch (Exception e) {

System.out.println(e.toString());
}
return null;
}

thank
Similar articles