package cn.eva.utils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;
/**
* @作者:恒
* @创建时间: 2017年8月10日 下午4:49:13
* 功能:阿里云上传文件类
*/
public class OSSClientUtil {
private final Logger logger = LogManager.getLogger(OSSClientUtil.class);
// 域名
private String endpoint=EvaWebUtils.getProperties("endpoint");
// 密钥
private String accessKeyId=EvaWebUtils.getProperties("accessKeyId");
//密匙
private String accessKeySecret=EvaWebUtils.getProperties("accessKeySecret");
//空间
private String bucketName=EvaWebUtils.getProperties("bucketName");
//目录1
private String filedir1 = "filedir1 /";
private OSSClient ossClient;
public OSSClientUtil() {
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
//初始化
public void init() {
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
//销毁
public void destory() {
ossClient.shutdown();
}
/**
* @功能:上传图片文件
* @param file
* @return
* @throws Exception
* @作者: 恒
* @创建时间: 2017年8月10日下午5:26:17
*/
public String uploadPhotoOss(MultipartFile file) throws Exception {
String originalFilename = file.getOriginalFilename();
String type = ".png";
if(originalFilename.contains("."))
type = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Random random = new Random();
String name = filedir1+sdf.format(new Date()) + "/" + random.nextInt(10000) + System.currentTimeMillis() + type;
try {
InputStream inputStream = file.getInputStream();
uploadFileOSS(inputStream,name);
return name;
} catch (Exception e) {
throw new Exception("图片上传失败");
}
}
/**
* @功能:上传到OSS服务器 如果同名文件会覆盖服务器上的
* @param instream 文件流
* @param fileName 文件名称 包括后缀名
* @return 出错返回"" ,唯一MD5数字签名
* @作者: 恒
* @创建时间: 2017年8月10日下午5:23:00
*/
public String uploadFileOSS(InputStream instream, String fileName) {
String ret = "";
try {
//创建上传Object的Metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(instream.available());
objectMetadata.setCacheControl("no-cache");
objectMetadata.setHeader("Pragma", "no-cache");
objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
objectMetadata.setContentDisposition("inline;filename=" + fileName);
//上传文件
PutObjectResult putResult = ossClient.putObject(bucketName,fileName, instream, objectMetadata);
ret = putResult.getETag();
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
try {
if (instream != null) {
instream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
/**
* @功能:删除文件
* @作者: 恒
* @创建时间: 2017年8月11日上午10:16:33
*/
public void delFileOSS(String fileName){
if(fileName!=null&&fileName.length()>0)
ossClient.deleteObject(bucketName,fileName);
}
}
网友评论