yonge 5 anni fa
parent
commit
99e7fb5788

+ 0 - 36
mec-thirdpart/src/main/java/com/ym/mec/thirdparty/exception/ThirdpartyException.java

@@ -1,36 +0,0 @@
-package com.ym.mec.thirdpart.exception;
-
-import com.ym.mec.util.string.MessageFormatter;
-
-public class ThirdpartException extends RuntimeException {
-
-	/**
-	 * 
-	 */
-	private static final long serialVersionUID = -4562885563376566463L;
-
-	public ThirdpartException() {
-		super();
-	}
-
-	public ThirdpartException(String message) {
-		super(message);
-	}
-
-	public ThirdpartException(Throwable cause) {
-		super(cause);
-	}
-
-	public ThirdpartException(String message, Throwable cause) {
-		super(message, cause);
-	}
-
-	public ThirdpartException(String message, Object... arguments) {
-		super(MessageFormatter.arrayFormat(message, arguments));
-	}
-
-	public ThirdpartException(String message, Throwable cause, Object... args) {
-		super(MessageFormatter.arrayFormat(message, args), cause);
-	}
-
-}

+ 0 - 24
mec-thirdpart/src/main/java/com/ym/mec/thirdparty/storage/StoragePlugin.java

@@ -1,24 +0,0 @@
-package com.ym.mec.thirdpart.storage;
-
-import java.io.File;
-import java.io.IOException;
-
-public interface StoragePlugin {
-
-	/**
-	 * 上传文件
-	 * @param folderName 文件夹
-	 * @param file 需要上传的文件
-	 * @return 返回文件路径
-	 */
-	String uploadFile(String folderName, File file);
-
-	/**
-	 * 下载文件
-	 * @param folderName 文件夹
-	 * @param fileName 文件名称
-	 * @return 返回文件内容
-	 * @throws IOException
-	 */
-	byte[] getFile(String folderName, String fileName) throws IOException;
-}

+ 0 - 79
mec-thirdpart/src/main/java/com/ym/mec/thirdparty/storage/StoragePluginContext.java

@@ -1,79 +0,0 @@
-package com.ym.mec.thirdpart.storage;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
-import org.springframework.stereotype.Component;
-
-import com.ym.mec.thirdpart.exception.ThirdpartException;
-import com.ym.mec.thirdpart.storage.vendors.AliyunOssStoragePlugin;
-
-@Component
-public class StoragePluginContext implements ApplicationContextAware {
-
-	private ApplicationContext applicationContext;
-
-	private final Map<String, String> mapper = new HashMap<String, String>() {
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = -9071481806931421021L;
-
-		{
-			put("aliyun", StringUtils.uncapitalize(AliyunOssStoragePlugin.class.getSimpleName()));
-		}
-	};
-
-	private StoragePlugin storagePlugin;
-
-	@Override
-	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-		this.applicationContext = applicationContext;
-	}
-
-	/**
-	 * 上传文件
-	 * @param vendors 供应商
-	 * @param folderName 文件夹
-	 * @param file 需要上传的文件
-	 * @return 返回文件路径
-	 */
-	public String uploadFile(String vendors, String folderName, File file) {
-
-		storagePlugin = getStoragePlugin(vendors);
-
-		return storagePlugin.uploadFile(folderName, file);
-	}
-
-	/**
-	 * 下载文件
-	 * @param vendors 供应商
-	 * @param folderName 文件夹
-	 * @param fileName 文件名称
-	 * @return 返回文件内容
-	 * @throws IOException
-	 */
-	public byte[] getFile(String vendors, String folderName, String fileName) throws IOException {
-
-		storagePlugin = getStoragePlugin(vendors);
-
-		return storagePlugin.getFile(folderName, fileName);
-	}
-
-	private StoragePlugin getStoragePlugin(String vendors) {
-		String beanId = mapper.get(vendors);
-
-		if (StringUtils.isBlank(beanId)) {
-			throw new ThirdpartException("存储供应商不存在");
-		}
-
-		return applicationContext.getBean(beanId, StoragePlugin.class);
-	}
-
-}

+ 0 - 123
mec-thirdpart/src/main/java/com/ym/mec/thirdparty/storage/vendors/AliyunOssStoragePlugin.java

@@ -1,123 +0,0 @@
-package com.ym.mec.thirdpart.storage.vendors;
-
-import java.io.File;
-import java.io.IOException;
-
-import org.apache.commons.io.IOUtils;
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
-import com.aliyun.oss.ClientConfiguration;
-import com.aliyun.oss.OSSClient;
-import com.aliyun.oss.model.OSSObject;
-import com.ym.mec.thirdpart.exception.ThirdpartException;
-import com.ym.mec.thirdpart.storage.StoragePlugin;
-
-@Component
-public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
-
-	@Value("${storage.oss.endpoint:oss-cn-beijing.aliyuncs.com}")
-	private String endpoint;
-
-	@Value("${storage.oss.accessKeyId:LTAIwZW9XqrfsZ4r}")
-	private String accessKeyId;
-
-	@Value("${storage.oss.accessKeySecret:5uDsNZmHMxcnxav8w9byII4zcPpu5G}")
-	private String accessKeySecret;
-
-	@Value("${storage.oss.bucketName:yooma-test}")
-	private String bucketName;
-
-	private OSSClient ossClient;
-
-	@Override
-	public void afterPropertiesSet() throws Exception {
-		// 创建ClientConfiguration。ClientConfiguration是OSSClient的配置类,可配置代理、连接超时、最大连接数等参数。
-		ClientConfiguration conf = new ClientConfiguration();
-
-		// 设置OSSClient允许打开的最大HTTP连接数,默认为1024个。
-		conf.setMaxConnections(200);
-		// 设置Socket层传输数据的超时时间,默认为50000毫秒。
-		conf.setSocketTimeout(10000);
-		// 设置建立连接的超时时间,默认为50000毫秒。
-		conf.setConnectionTimeout(10000);
-		// 设置从连接池中获取连接的超时时间(单位:毫秒),默认不超时。
-		conf.setConnectionRequestTimeout(1000);
-		// 设置连接空闲超时时间。超时则关闭连接,默认为60000毫秒。
-		conf.setIdleConnectionTime(10000);
-		// 设置失败请求重试次数,默认为3次。
-		conf.setMaxErrorRetry(5);
-		// 设置是否支持将自定义域名作为Endpoint,默认支持。
-		conf.setSupportCname(true);
-
-		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
-	}
-
-	@Override
-	public String uploadFile(String folderName, File file) {
-		if (!file.exists()) {
-			throw new ThirdpartException("需要上传的文件[{}]不存在", file.getAbsolutePath());
-		}
-
-		if (folderName.endsWith("/")) {
-			folderName = folderName.substring(0, folderName.length() - 1);
-		}
-
-		ossClient.putObject(bucketName, folderName + "/" + file.getName(), file);
-
-		return "https://" + bucketName + "." + endpoint + "/" + folderName + "/" + file.getName();
-	}
-
-	@Override
-	public byte[] getFile(String folderName, String fileName) throws IOException {
-		OSSObject ossObject = ossClient.getObject(bucketName, folderName + "/" + fileName);
-		try {
-			return IOUtils.toByteArray(ossObject.getObjectContent());
-		} finally {
-			if (ossObject != null) {
-				ossObject.close();
-			}
-		}
-	}
-
-	@Override
-	public void destroy() throws Exception {
-		if (ossClient != null) {
-			ossClient.shutdown();
-		}
-	}
-
-	public void setEndpoint(String endpoint) {
-		this.endpoint = endpoint;
-	}
-
-	public void setAccessKeyId(String accessKeyId) {
-		this.accessKeyId = accessKeyId;
-	}
-
-	public void setAccessKeySecret(String accessKeySecret) {
-		this.accessKeySecret = accessKeySecret;
-	}
-
-	public void setBucketName(String bucketName) {
-		this.bucketName = bucketName;
-	}
-
-	public static void main(String[] args) throws Exception {
-		AliyunOssStoragePlugin aliyunOssStorageService = new AliyunOssStoragePlugin();
-		aliyunOssStorageService.setAccessKeyId("LTAIwZW9XqrfsZ4r");
-		aliyunOssStorageService.setAccessKeySecret("5uDsNZmHMxcnxav8w9byII4zcPpu5G");
-		aliyunOssStorageService.setBucketName("yooma-test");
-		aliyunOssStorageService.setEndpoint("oss-cn-beijing.aliyuncs.com");
-		aliyunOssStorageService.afterPropertiesSet();
-
-		File file = new File("e:/var/2.jpg");
-		System.out.println(aliyunOssStorageService.uploadFile("aaa", file));
-
-		System.err.println("***********" + aliyunOssStorageService.getFile("aaa", file.getName()).length);
-
-		aliyunOssStorageService.destroy();
-	}
-}