第三方插件的引用(2) : 阿里云OSS文件上工具类

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.wanpro.mp.utils.DateUtil;
import com.wanpro.mp.utils.UUIDUtils;

public class OssClientUtil {

	private static String endpoint = "xxx";
	private static String accessKeyId = "xxx";
	private static String accessKeySecret = "xxx";
	private static String bucket = "xxx";

	private static OssClientUtil ossClientUtil;

	private OssClientUtil() {
		super();
	}

	public static OssClientUtil newInstance() {
		if (ossClientUtil == null) {
			ossClientUtil = new OssClientUtil();
		}
		return ossClientUtil;
	}

	/**
	 * 文件流上传
	 * 
	 * @Title uploadPic
	 * @param @param
	 *            file
	 * @param @param
	 *            keys
	 * @param @return
	 * @return String
	 * @Explain
	 */
	public static String uploadPic(File file, String keys) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
		ossClient.putObject(bucket, keys, file);
		String url = String.valueOf(getUrl(keys));
		ossClient.shutdown();
		return url;
	}
	
	/**
	 * MultipartFile 文件流上传
	 * @param file
	 * @param request
	 * @param type
	 * @return
	 */
	public static String uploadPic(MultipartFile file, HttpServletRequest request, Integer type) {
		try {
			String fileName = file.getOriginalFilename().toString();
			String ossFileName = UUIDUtils.getUUID2() + "_" + fileName;
            String[] strings = DateUtil.format(new Date(), "yyyy-MM-dd").split("-");
     		String ossWholeName = "video/"+strings[0]+"/"+strings[1]+"/"+strings[2]+"/"+ossFileName;
			
			// MultipartFile转file
			File tempFile = null;
			if(!file.isEmpty()) {
			    String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
			    File dir = new File(filePath);
			    if(! dir.exists()) {
			        dir.mkdir();
			    }

			    String path = filePath + file.getOriginalFilename();
			    //save to the /upload path
			    tempFile =  new File(path);

			    file.transferTo(tempFile);
			}      
			
			OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
			
			ossClient.putObject(bucket, ossWholeName, tempFile);
			tempFile.delete();
			ossClient.shutdown();
			if(type == 1) {
				return ossWholeName;
			}else {
				return String.valueOf(getUrl(ossWholeName));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 文件流上传2
	 * 
	 * @Title uploadPic
	 * @param @param
	 *            file
	 * @param @param
	 *            keys
	 * @param @return
	 * @return String
	 * @Explain
	 */
	public static String uploadPicLongTime(File file, String keys) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
		ossClient.putObject(bucket, keys, file);
		String url = String.valueOf(getUrlLongTime(keys));
		ossClient.shutdown();
		return splitUrl(url);
	}

	/**
	 * 字节数组上传
	 * 
	 * @Title uploadPic
	 * @param @param
	 *            content
	 * @param @param
	 *            keys
	 * @param @return
	 * @return String
	 * @Explain
	 */
	public String uploadPic(byte[] content, String keys) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
		ossClient.putObject(bucket, keys, new ByteArrayInputStream(content));
		String url = String.valueOf(getUrl(keys));
		ossClient.shutdown();
		return splitUrl(url);
	}

	/**
	 * 获取文件URL
	 * 
	 * @Title getUrl
	 * @param @param
	 *            key
	 * @param @return
	 * @return URL
	 */
	public static String getUrl(String key) {
		if (key.indexOf("http") == -1) {
			OSSClient server = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
			// 设置URL过期时间为10年 3600l* 1000*24*365*10
			Date expirations = new Date(new Date().getTime() + 1000 * 60 * 5);// url超时时间
			// 生成URL
			URL url = server.generatePresignedUrl(bucket, key, expirations);
			String strUrl = String.valueOf(url);
			// 关闭client
			server.shutdown();
			return splitUrl(strUrl);
		} else {
			return splitUrl(key);
		}
	}

	/**
	 * 获取文件URL2
	 * 
	 * @Title getUrl
	 * @param @param
	 *            key
	 * @param @return
	 * @return URL
	 */
	public static String getUrlOneYeay(String key) {
		if (key.indexOf("http") == -1) {
			OSSClient server = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
			// 设置URL过期时间为1年 3600l* 1000*24*365
			Date expirations = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365);// url超时时间

			// 生成URL
			URL url = server.generatePresignedUrl(bucket, key, expirations);
			String strUrl = String.valueOf(url);
			// 关闭client
			server.shutdown();
			return splitUrl(strUrl);
		} else {
			return splitUrl(key);
		}
	}

	/**
	 * 获取文件URL3
	 * 
	 * @Title getUrl
	 * @param @param
	 *            key
	 * @param @return
	 * @return URL
	 */
	public static String getUrlLongTime(String key) {
		if (key.indexOf("http") == -1) {
			OSSClient server = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 连接oss云存储服务器
			// 设置URL过期时间为100年 3600l* 1000*24*365*100
			Date expirations = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 100);// url超时时间

			// 生成URL
			URL url = server.generatePresignedUrl(bucket, key, expirations);
			String strUrl = String.valueOf(url);
			// 关闭client
			server.shutdown();
			return splitUrl(strUrl);
		} else {
			return splitUrl(key);
		}
	}

	/**
	 * 切割url,注意,需设置了公共读url才有效
	 * 
	 * @param url
	 * @return
	 */
	public static String splitUrl(String url) {
		if (url.indexOf("?Expires") != -1) {
			return url.split("Expires")[0].substring(0,url.split("Expires")[0].length() -1 );
		}
		return url;
	}

}

你可能感兴趣的:(第三方插件的引用)