参考阿里oss api自行扩展使用。
import com.aliyun.oss.OSSClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class AliOssConfig { private static final Logger logger = LoggerFactory.getLogger(AliOssConfig.class); private String endpoint; private String bucketName; private String accessKeyId; private String accessKeySecret; OSSClient ossClient = null; public AliOssConfig(String propertiesFile) { Properties properties = new Properties(); InputStream is = AliOssConfig.class.getClassLoader().getResourceAsStream(propertiesFile); try { properties.load(is); } catch (IOException e) { logger.error("AliOssConfig", e); } endpoint = properties.getProperty("endpoint"); bucketName = properties.getProperty("bucketName"); accessKeyId = properties.getProperty("accessKeyId"); accessKeySecret = properties.getProperty("accessKeySecret"); ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); } public OSSClient getOssClient() { return ossClient; } public void setOssClient(OSSClient ossClient) { this.ossClient = ossClient; } public String getEndpoint() { return endpoint; } public void setEndpoint(String endpoint) { this.endpoint = endpoint; } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public String getAccessKeySecret() { return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } }
/** * Created by Administrator on 2017/9/22. */ public class AliOssConstant { /** * 阿里云oss存储文件夹 */ public static final String OSS_KEY_ACTIVITY = ""; public static final String OSS_KEY_COMM_ACTIVITY = ""; public static final String OSS_KEY_COMM_SHOPS = ""; public static final String OSS_KEY_COMM_THEMES = ""; public static final String OSS_KEY_COMMENT = ""; public static final String OSS_KEY_FEATTURE = ""; public static final String OSS_KEY_INDEX_CAROUSELS = ""; public static final String OSS_KEY_MARKET = ""; public static final String OSS_KEY_MARKETLOGO = ""; public static final String OSS_KEY_RAIDERS = ""; public static final String OSS_KEY_SECTION = ""; public static final String OSS_KEY_SERVICE = "service"; public static final String OSS_KEY_SHOP = "shop"; public static final String OSS_KEY_SHOPLOGO = "shopLogo"; public static final String OSS_KEY_SHOP_LOGO = "shop_logo"; public static final String OSS_KEY_THEMES = "themes"; public static final String OSS_KEY_TRADING_AREA_ATLAS = "trading_area_atlas"; public static final String OSS_KEY_TYPELIBS = "typelibs"; public static final String OSS_KEY_USER = "user"; public static final String OSS_KEY_VIDEO = "video"; public static final String OSS_KEY_VIDEOIMGS = "videoImgs"; public static final String OSS_KEY_VIDEOLOGS = "videoLogos"; public static final String OSS_KEY_AREA = "area"; /*****-OSS图片样式-*****/ public static final String OSS_STYLE_RESIZE = "image/resize,p_50"; }
import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Date; import java.util.List; import java.util.UUID; /** * Created with IntelliJ IDEA. * * @author zsq * @version V1.0.0 * @date */ public class AliOssKit { private static final Logger logger = LoggerFactory.getLogger(AliOssKit.class); static AliOssConfig mConfig; static void init(AliOssConfig config) { mConfig = config; } static void destory() { if (mConfig.getOssClient() != null) mConfig.getOssClient().shutdown(); } /*****-管理Bucket-*****/ /** * 创建 存储空间 * * @param bucketName * @return */ public static Bucket createBucket(String bucketName) { try { CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName); // 设置bucket权限为公共读,默认是私有读写 createBucketRequest.setCannedACL(CannedAccessControlList.Private); // 设置bucket存储类型为低频访问类型,默认是标准类型 createBucketRequest.setStorageClass(StorageClass.Standard); return mConfig.getOssClient().createBucket(createBucketRequest); } catch (Exception e) { logger.error("创建存储空间失败", e); } return null; } /** * 获取 前100个Bucket * * @return */ public static List<Bucket> getBucketList() { try { ListBucketsRequest listBucketsRequest = new ListBucketsRequest(); // 设定过滤前缀,不设定不过滤 //listBucketsRequest.setPrefix(""); // 设定结果从marker之后按字母排序的第一个开始返回,默认从头开始 //listBucketsRequest.setMarker(""); // 设定此次返回的bucket的最大数,默认为100,不能大于1000 //listBucketsRequest.setMaxKeys(100); return mConfig.getOssClient().listBuckets(); } catch (Exception e) { logger.error("获取Bucket列表失败", e); } return null; } /** * 删除Bucket * * @return */ public static boolean deleteBucket(String bucketName) { try { if (doesBucketExist(bucketName)) mConfig.getOssClient().deleteBucket(bucketName); return true; } catch (Exception e) { logger.error("删除Bucket失败", e); } return false; } public static boolean doesBucketExist(String bucketName) { try { return mConfig.getOssClient().doesBucketExist(bucketName); } catch (Exception e) { logger.error("验证失败", e); } return false; } /** * 设置存储空间权限 * * @param bucketName * @param acl */ public static void setBucketAcl(String bucketName, CannedAccessControlList acl) { mConfig.getOssClient().setBucketAcl(bucketName, acl); } /** * 获取空间权限 * * @param bucketName * @return */ public static String getBucketAcl(String bucketName) { if (doesBucketExist(bucketName)) return mConfig.getOssClient().getBucketAcl(bucketName).getCannedACL().toString(); return null; } /** * 获取区域 * * @param bucketName * @return */ public static String getBucketLocation(String bucketName) { if (doesBucketExist(bucketName)) return mConfig.getOssClient().getBucketLocation(bucketName); return null; } /** * 获取Bucket Info * * @param bucketName * @return */ public static BucketInfo getBucketInfo(String bucketName) { if (doesBucketExist(bucketName)) return mConfig.getOssClient().getBucketInfo(bucketName); return null; } /*****-上传文件-默认上传至配置文件指定空间*****/ /** * 上传字符串 * * @param key * @param content * @return */ public static PutObjectResult upload(String key, String content) { try { if (doesBucketExist(mConfig.getBucketName())) return mConfig.getOssClient().putObject(mConfig.getBucketName(), key, new ByteArrayInputStream(content.getBytes())); } catch (Exception e) { logger.error("上传字符串失败", e); } return null; } /** * 上传byte数组 * * @param key * @param content * @return */ public static PutObjectResult upload(String key, byte[] content) { try { if (doesBucketExist(mConfig.getBucketName())) return mConfig.getOssClient().putObject(mConfig.getBucketName(), key, new ByteArrayInputStream(content)); } catch (Exception e) { logger.error("上传byte数组失败", e); } return null; } /** * 上传网络流 * * @param key * @param urlPath * @return */ public static PutObjectResult uploadURL(String key, String urlPath) { InputStream is = null; try { is = new URL(urlPath).openStream(); return mConfig.getOssClient().putObject(mConfig.getBucketName(), key, is); } catch (Exception e) { } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 上传文件 * * @param file * @param key * @return */ public static PutObjectResult upload(File file, String key) { try { return mConfig.getOssClient().putObject(mConfig.getBucketName(), key, file); } catch (Exception e) { logger.error("上传失败", e); } return null; } /** * 获得url链接 * * @param key * @return */ public static String getUrl(String key) { // 设置URL过期时间为100年 3600l* 1000*24*365*100 Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100 * 100); // 生成URL URL url = mConfig.getOssClient().generatePresignedUrl(mConfig.getBucketName(), key, expiration); if (url != null) { return url.toString(); } return null; } /** * 重新定义文件名,使用UUID保证不重复 * * @param action * @param fileName * @return */ public static String getKey(String action, String fileName) { return action + "/" + UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); } public static boolean deleteFile(String key) { try { mConfig.getOssClient().deleteObject(mConfig.getBucketName(), key); return true; } catch (Exception e) { } return false; } public static OSSClient getOssClient() { return mConfig.getOssClient(); } public static AliOssConfig getmConfig() { return mConfig; } }
import com.jfinal.plugin.IPlugin; /** * author:zsq */ public class AliOssPlugin implements IPlugin { private String propFile = "alioss.properties"; public AliOssPlugin() { } public AliOssPlugin(String propFile) { this.propFile = propFile; } @Override public boolean start() { AliOssKit.init(new AliOssConfig(propFile)); return true; } @Override public boolean stop() { AliOssKit.destory(); return true; } }