|
@@ -10,6 +10,7 @@ import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
|
+import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
@@ -17,11 +18,15 @@ import java.util.Map;
|
|
|
|
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
|
|
|
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
+import com.google.common.collect.Maps;
|
|
|
|
+
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
import cn.com.qmth.examcloud.web.aliyun.AliYunAccount;
|
|
import cn.com.qmth.examcloud.web.aliyun.AliYunAccount;
|
|
import cn.com.qmth.examcloud.web.aliyun.AliyunSite;
|
|
import cn.com.qmth.examcloud.web.aliyun.AliyunSite;
|
|
@@ -30,37 +35,39 @@ import cn.com.qmth.examcloud.web.filestorage.FileStorage;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
|
|
import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.YunHttpRequest;
|
|
|
|
|
|
@Service(value = "aliyunFileStorage")
|
|
@Service(value = "aliyunFileStorage")
|
|
public class AliyunFileStorageImpl implements FileStorage {
|
|
public class AliyunFileStorageImpl implements FileStorage {
|
|
private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
-
|
|
|
|
|
|
+
|
|
// 文件最大大小(byte)
|
|
// 文件最大大小(byte)
|
|
private static int maxFileSize = 100 * 1024 * 1024;
|
|
private static int maxFileSize = 100 * 1024 * 1024;
|
|
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
public String saveFile(File file, String path) {
|
|
public String saveFile(File file, String path) {
|
|
try {
|
|
try {
|
|
- String siteId="transPath";
|
|
|
|
|
|
+ String siteId = "transPath";
|
|
FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
env.setRelativePath(path);
|
|
env.setRelativePath(path);
|
|
- String relativePath=postObject(siteId, env, file, null);
|
|
|
|
- AliyunSite as=AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
- return FileStorageType.ALIYUN+"-"+as.getAliyunId()+"://"+relativePath;
|
|
|
|
|
|
+ String relativePath = postObject(siteId, env, file, null);
|
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ return FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath;
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- throw new StatusException("1001", "上传出错",e);
|
|
|
|
|
|
+ throw new StatusException("1001", "上传出错", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String realPath(String path) {
|
|
public String realPath(String path) {
|
|
- String yunId=FileStorageUtil.getYunId(path);
|
|
|
|
- AliYunAccount ac=AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
|
|
|
+ String yunId = FileStorageUtil.getYunId(path);
|
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
return FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
return FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
}
|
|
}
|
|
|
|
|
|
- /**表单上传
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 表单上传
|
|
|
|
+ *
|
|
* @param siteId
|
|
* @param siteId
|
|
* @param env
|
|
* @param env
|
|
* @param file
|
|
* @param file
|
|
@@ -68,19 +75,19 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
* @return 不带域名的完整路径
|
|
* @return 不带域名的完整路径
|
|
* @throws IOException
|
|
* @throws IOException
|
|
*/
|
|
*/
|
|
- private String postObject(String siteId, FileStoragePathEnvInfo env,File file,String md5) throws IOException {
|
|
|
|
- AliyunSite as=AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
- AliYunAccount ac=AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
|
- String ossEndpoint=ac.getOssEndpoint();
|
|
|
|
- String bucket=ac.getBucket();
|
|
|
|
- String accessKeyId=ac.getAccessKeyId();
|
|
|
|
- String accessKeySecret=ac.getAccessKeySecret();
|
|
|
|
- //阿里云文件路径
|
|
|
|
|
|
+ private String postObject(String siteId, FileStoragePathEnvInfo env, File file, String md5) throws IOException {
|
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
|
|
|
|
+ String ossEndpoint = ac.getOssEndpoint();
|
|
|
|
+ String bucket = ac.getBucket();
|
|
|
|
+ String accessKeyId = ac.getAccessKeyId();
|
|
|
|
+ String accessKeySecret = ac.getAccessKeySecret();
|
|
|
|
+ // 阿里云文件路径
|
|
String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
if (path.startsWith("/")) {
|
|
if (path.startsWith("/")) {
|
|
path = path.substring(1);
|
|
path = path.substring(1);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
String filepath = file.getAbsolutePath();
|
|
String filepath = file.getAbsolutePath();
|
|
String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
String urlStr = ossEndpoint.replace("http://", "http://" + bucket + "."); // 提交表单的URL为bucket域名
|
|
String urlStr = ossEndpoint.replace("http://", "http://" + bucket + "."); // 提交表单的URL为bucket域名
|
|
@@ -106,12 +113,13 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
fileMap.put("file", filepath);
|
|
fileMap.put("file", filepath);
|
|
|
|
|
|
String ret = formUpload(urlStr, textMap, fileMap);
|
|
String ret = formUpload(urlStr, textMap, fileMap);
|
|
- log.info("oss上传:"+ret);
|
|
|
|
|
|
+ log.info("oss上传:" + ret);
|
|
return path;
|
|
return path;
|
|
}
|
|
}
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
@SuppressWarnings("rawtypes")
|
|
- private static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) throws IOException {
|
|
|
|
|
|
+ private static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap)
|
|
|
|
+ throws IOException {
|
|
String res = "";
|
|
String res = "";
|
|
HttpURLConnection conn = null;
|
|
HttpURLConnection conn = null;
|
|
String BOUNDARY = "9431149156168";
|
|
String BOUNDARY = "9431149156168";
|
|
@@ -219,11 +227,53 @@ public class AliyunFileStorageImpl implements FileStorage {
|
|
@Override
|
|
@Override
|
|
public String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
|
|
public String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
|
|
try {
|
|
try {
|
|
- String relativePath=postObject(siteId, env, file, null);
|
|
|
|
- AliyunSite as=AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
- return FileStorageType.ALIYUN+"-"+as.getAliyunId()+"://"+relativePath;
|
|
|
|
|
|
+ String relativePath = postObject(siteId, env, file, null);
|
|
|
|
+ AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ return FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath;
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- throw new StatusException("5001", "上传出错",e);
|
|
|
|
|
|
+ throw new StatusException("5001", "上传出错", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public YunHttpRequest getSignature(String siteId, FileStoragePathEnvInfo env, String md5) {
|
|
|
|
+ AliyunSite site = AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(site.getAliyunId());
|
|
|
|
+ String ossEndpoint = ac.getOssEndpoint();
|
|
|
|
+ String bucket = ac.getBucket();
|
|
|
|
+ String accessKeyId = ac.getAccessKeyId();
|
|
|
|
+ String accessKeySecret = ac.getAccessKeySecret();
|
|
|
|
+ String urlStr = ossEndpoint.replace("http://", "http://" + bucket + ".");
|
|
|
|
+ env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
|
|
|
|
+
|
|
|
|
+ String path = FreeMarkerUtil.process(site.getPath(), env);
|
|
|
|
+ if (path.startsWith("/")) {
|
|
|
|
+ path = path.substring(1);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ String accessUrl = FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
|
|
+
|
|
|
|
+ Map<String, String> params = Maps.newHashMap();
|
|
|
|
+ // key
|
|
|
|
+ params.put("key", path);
|
|
|
|
+ // OSSAccessKeyId
|
|
|
|
+ params.put("OSSAccessKeyId", accessKeyId);
|
|
|
|
+ // policy
|
|
|
|
+ Date expiration = DateUtils.addSeconds(new Date(), 600);
|
|
|
|
+ String expirationStr = DateUtil.format(expiration, "yyyy-MM-dd'T'HH:mm:ss.SSS Z");
|
|
|
|
+ String policy = "{\"expiration\": \"" + expirationStr + "\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
|
+ + maxFileSize + "]]}";
|
|
|
|
+ String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
|
+ params.put("policy", encodePolicy);
|
|
|
|
+ // Signature
|
|
|
|
+ String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
|
+ encodePolicy);
|
|
|
|
+ params.put("Signature", signaturecom);
|
|
|
|
+
|
|
|
|
+ YunHttpRequest request = new YunHttpRequest();
|
|
|
|
+ request.setAccessUrl(accessUrl);
|
|
|
|
+ request.setFormParams(params);
|
|
|
|
+ request.setFormUrl(urlStr);
|
|
|
|
+ return request;
|
|
}
|
|
}
|
|
}
|
|
}
|