1.获取jar包(没有maven的坐标)
http://www.sauronsoftware.it/projects/jave/index.php
2.视频上传的工具类,注意:这个是上传到阿里云的oos上的工具类,
package com.ad.admin.utils.oss;
import com.ad.admin.common.AdAllianceProperties;
import com.ad.admin.common.exception.BusinessException;
import com.aliyun.oss.OSSClient;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class VideoOssClientUtil extends OSSUtil {
public VideoOssClientUtil(AdAllianceProperties adAllianceProperties) {
super(adAllianceProperties);
// 视频的文件目录
fileDir = adAllianceProperties.getOssConfigure().getVideoDir();
ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
}
public String uploadVideo(MultipartFile file) throws Exception {
if (file.getSize() > 100 * 1024 * 1024) {
throw new BusinessException("上传视频大小不能超过100MB!");
}
String substring = getFileSuffix(file);
String name = getRandomFileName(substring);
uploadFileToOss(file.getInputStream(), name);
return "http://" + baseUrl + fileDir + name ;
}
/**
* 判断上传文件是否为视频
*
* @param fileNames 文件名
**/
public static boolean isVideo(String fileNames) {
String suffix = fileNames.split("\\.")[fileNames.split("\\.").length - 1].toLowerCase();
if ("mp4".equals(suffix)) {
return true;
}
if ("rmvb".equals(suffix)) {
return true;
}
if ("rm".equals(suffix)) {
return true;
}
if ("asf".equals(suffix)) {
return true;
}
if ("divx".equals(suffix)) {
return true;
}
if ("mpg".equals(suffix)) {
return true;
}
if ("mpeg".equals(suffix)) {
return true;
}
if ("mpe".equals(suffix)) {
return true;
}
if ("wmv".equals(suffix)) {
return true;
}
if ("avi".equals(suffix)) {
return true;
}
return false;
}
//获取视频的时长(单位秒)
public Long getVideoDuration(MultipartFile file) throws Exception {
File newFile = new File(file.getOriginalFilename());
transformToFile(file.getInputStream(),newFile);
Encoder encoder = new Encoder();
MultimediaInfo multimediaInfo = encoder.getInfo(newFile);
deleteFile(newFile);
return multimediaInfo.getDuration() / 1000;
}
//将InputStream转换成File
private void transformToFile(InputStream inputStream, File file){
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 1024 * 10];
while ((bytesRead = inputStream.read(buffer, 0, 1024 * 1024 * 10)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteFile(File file){
File newFile = new File(file.toURI());
System.out.println("删除临时文件=================uri:"+file.toURI());
newFile.delete();
}
}
package com.ad.admin.utils.oss;
import com.ad.admin.common.AdAllianceProperties;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class OSSUtil {
protected String endPoint;
protected String accessKeyId;
protected String accessKeySecret;
protected String bucketName;
protected String fileDir;
protected String baseUrl;
protected OSSClient ossClient = null;
public OSSUtil(AdAllianceProperties adAllianceProperties) {
endPoint = adAllianceProperties.getOssConfigure().getEndPoint();
accessKeyId = adAllianceProperties.getOssConfigure().getAccessKeyId();
accessKeySecret = adAllianceProperties.getOssConfigure().getAccessKeySecret();
bucketName = adAllianceProperties.getOssConfigure().getBucketName();
baseUrl = adAllianceProperties.getOssConfigure().getBaseUrl();
}
//以当前时间重命名文件
public String getRandomFileName(String suffix){
Random random = new Random();
String format = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+"666" + random.nextInt(1000000);
return "xmeal" + format + suffix;
}
/**
* Description: 判断OSS服务文件上传时文件的contentType
*
* @param FilenameExtension 文件后缀
* @return String
*/
public String getContentType(String FilenameExtension) {
if (FilenameExtension.equalsIgnoreCase("bmp")) {
return "image/bmp";
}
if (FilenameExtension.equalsIgnoreCase("gif")) {
return "image/gif";
}
if (FilenameExtension.equalsIgnoreCase("jpeg") ||
FilenameExtension.equalsIgnoreCase("jpg") ||
FilenameExtension.equalsIgnoreCase("png")) {
return "image/jpeg";
}
if (FilenameExtension.equalsIgnoreCase("html")) {
return "text/html";
}
if (FilenameExtension.equalsIgnoreCase("txt")) {
return "text/plain";
}
if (FilenameExtension.equalsIgnoreCase("vsd")) {
return "application/vnd.visio";
}
if (FilenameExtension.equalsIgnoreCase("pptx") ||
FilenameExtension.equalsIgnoreCase("ppt")) {
return "application/vnd.ms-powerpoint";
}
if (FilenameExtension.equalsIgnoreCase("docx") ||
FilenameExtension.equalsIgnoreCase("doc")) {
return "application/msword";
}
if (FilenameExtension.equalsIgnoreCase("xml")) {
return "text/xml";
}
if(FilenameExtension.equalsIgnoreCase("mp4")){
return "void/mp4";
}
if(FilenameExtension.equalsIgnoreCase("avi")){
return "void/avi";
}
return "image/jpeg";
}
/**
* 上传到OSS服务器 如果同名文件会覆盖服务器上的
* @param ins 文件流
* @param fileName 文件名称 包括后缀名
* @return 出错返回"" ,唯一MD5数字签名
*/
public String uploadFileToOss(InputStream ins, String fileName) throws Exception {
String result = "";
//创建上传Object的Metadate
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(ins.available());
objectMetadata.setCacheControl("no-cache");
objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
objectMetadata.setContentDisposition("inline;filename=" + fileName);
//上传
PutObjectResult por = ossClient.putObject(bucketName, fileDir + fileName, ins, objectMetadata);
result = por.getETag();
if(!"".equalsIgnoreCase(result)) {
result = "http://" + baseUrl + fileDir + fileName;
}
try {
if(ins != null)
ins.close();
} catch (IOException io) {
throw io;
} finally {
return result;
}
}
public String getFileSuffix(MultipartFile file){
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
return suffix;
}
//删除文件
public void deleteResources(String url) {
boolean found = ossClient.doesObjectExist(bucketName, "resources"+ url);
if(found){
this.ossClient.deleteObject(bucketName,"resources"+url);
}
}
}