|
@@ -0,0 +1,229 @@
|
|
|
|
+package cn.com.qmth.examcloud.web.filestorage.impl;
|
|
|
|
+
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.DataInputStream;
|
|
|
|
+import java.io.DataOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import javax.activation.MimetypesFileTypeMap;
|
|
|
|
+
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
|
|
|
|
+import cn.com.qmth.examcloud.web.aliyun.AliYunAccount;
|
|
|
|
+import cn.com.qmth.examcloud.web.aliyun.AliyunSite;
|
|
|
|
+import cn.com.qmth.examcloud.web.aliyun.AliyunSiteManager;
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStorage;
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
|
|
|
|
+import cn.com.qmth.examcloud.web.filestorage.FileStorageUtil;
|
|
|
|
+
|
|
|
|
+@Service(value = "aliyunFileStorage")
|
|
|
|
+public class AliyunFileStorageImpl implements FileStorage {
|
|
|
|
+ private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
|
+
|
|
|
|
+ // 文件最大大小(byte)
|
|
|
|
+ private static int maxFileSize = 100 * 1024 * 1024;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String saveFile(File file, String path) {
|
|
|
|
+ try {
|
|
|
|
+ String siteId="transPath";
|
|
|
|
+ FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
|
|
|
|
+ env.setRelativePath(path);
|
|
|
|
+ String relativePath=postObject(siteId, env, file, null);
|
|
|
|
+ AliyunSite as=AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ return FileStorageType.ALIYUN+"-"+as.getAliyunId()+"://"+relativePath;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new StatusException("1001", "上传出错",e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String realPath(String path) {
|
|
|
|
+ String yunId=FileStorageUtil.getYunId(path);
|
|
|
|
+ AliYunAccount ac=AliyunSiteManager.getAliYunAccountByAliyunId(yunId);
|
|
|
|
+ return FileStorageUtil.getUrl(ac.getDomain(), path);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**表单上传
|
|
|
|
+ * @param siteId
|
|
|
|
+ * @param env
|
|
|
|
+ * @param file
|
|
|
|
+ * @param md5
|
|
|
|
+ * @return 不带域名的完整路径
|
|
|
|
+ * @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();
|
|
|
|
+ //阿里云文件路径
|
|
|
|
+ String path = FreeMarkerUtil.process(as.getPath(), env);
|
|
|
|
+ if (path.startsWith("/")) {
|
|
|
|
+ path = path.substring(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String filepath = file.getAbsolutePath();
|
|
|
|
+ String filename = path.substring(path.lastIndexOf("/") + 1);
|
|
|
|
+ String urlStr = ossEndpoint.replace("http://", "http://" + bucket + "."); // 提交表单的URL为bucket域名
|
|
|
|
+
|
|
|
|
+ LinkedHashMap<String, String> textMap = new LinkedHashMap<String, String>();
|
|
|
|
+ // key
|
|
|
|
+ textMap.put("key", path);
|
|
|
|
+ // Content-Disposition
|
|
|
|
+ textMap.put("Content-Disposition", "attachment;filename=" + filename);
|
|
|
|
+ // OSSAccessKeyId
|
|
|
|
+ textMap.put("OSSAccessKeyId", accessKeyId);
|
|
|
|
+ // policy
|
|
|
|
+ String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, "
|
|
|
|
+ + maxFileSize + "]]}";
|
|
|
|
+ String encodePolicy = java.util.Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
|
+ textMap.put("policy", encodePolicy);
|
|
|
|
+ // Signature
|
|
|
|
+ String signaturecom = com.aliyun.oss.common.auth.ServiceSignature.create().computeSignature(accessKeySecret,
|
|
|
|
+ encodePolicy);
|
|
|
|
+ textMap.put("Signature", signaturecom);
|
|
|
|
+
|
|
|
|
+ Map<String, String> fileMap = new HashMap<String, String>();
|
|
|
|
+ fileMap.put("file", filepath);
|
|
|
|
+
|
|
|
|
+ String ret = formUpload(urlStr, textMap, fileMap);
|
|
|
|
+ log.info("oss上传:"+ret);
|
|
|
|
+ return path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
|
+ private static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) throws IOException {
|
|
|
|
+ String res = "";
|
|
|
|
+ HttpURLConnection conn = null;
|
|
|
|
+ String BOUNDARY = "9431149156168";
|
|
|
|
+ try {
|
|
|
|
+ URL url = new URL(urlStr);
|
|
|
|
+ conn = (HttpURLConnection) url.openConnection();
|
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
|
+ conn.setReadTimeout(30000);
|
|
|
|
+ conn.setDoOutput(true);
|
|
|
|
+ conn.setDoInput(true);
|
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
|
|
|
|
+ conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
|
|
|
+
|
|
|
|
+ OutputStream out = new DataOutputStream(conn.getOutputStream());
|
|
|
|
+ // text
|
|
|
|
+ if (textMap != null) {
|
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
|
+ Iterator iter = textMap.entrySet().iterator();
|
|
|
|
+ int i = 0;
|
|
|
|
+ while (iter.hasNext()) {
|
|
|
|
+ Map.Entry entry = (Map.Entry) iter.next();
|
|
|
|
+ String inputName = (String) entry.getKey();
|
|
|
|
+ String inputValue = (String) entry.getValue();
|
|
|
|
+ if (inputValue == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (i == 0) {
|
|
|
|
+ strBuf.append("--").append(BOUNDARY).append("\r\n");
|
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
|
+ strBuf.append(inputValue);
|
|
|
|
+ } else {
|
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
|
|
|
|
+
|
|
|
|
+ strBuf.append(inputValue);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ i++;
|
|
|
|
+ }
|
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // file
|
|
|
|
+ if (fileMap != null) {
|
|
|
|
+ Iterator iter = fileMap.entrySet().iterator();
|
|
|
|
+ while (iter.hasNext()) {
|
|
|
|
+ Map.Entry entry = (Map.Entry) iter.next();
|
|
|
|
+ String inputName = (String) entry.getKey();
|
|
|
|
+ String inputValue = (String) entry.getValue();
|
|
|
|
+ if (inputValue == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ File file = new File(inputValue);
|
|
|
|
+ String filename = file.getName();
|
|
|
|
+ String contentType = new MimetypesFileTypeMap().getContentType(file);
|
|
|
|
+ if (contentType == null || contentType.equals("")) {
|
|
|
|
+ contentType = "application/octet-stream";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
|
+ strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
|
|
|
|
+ strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename
|
|
|
|
+ + "\"\r\n");
|
|
|
|
+ strBuf.append("Content-Type: " + contentType + "\r\n\r\n");
|
|
|
|
+
|
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
|
+
|
|
|
|
+ DataInputStream in = new DataInputStream(new FileInputStream(file));
|
|
|
|
+ int bytes = 0;
|
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
|
+ while ((bytes = in.read(bufferOut)) != -1) {
|
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
|
+ }
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
|
+ out.write(strBuf.toString().getBytes());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
|
|
|
|
+ out.write(endData);
|
|
|
|
+ out.flush();
|
|
|
|
+ out.close();
|
|
|
|
+
|
|
|
|
+ // 读取返回数据
|
|
|
|
+ StringBuffer strBuf = new StringBuffer();
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
+ String line = null;
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
+ strBuf.append(line).append("\n");
|
|
|
|
+ }
|
|
|
|
+ res = strBuf.toString();
|
|
|
|
+ reader.close();
|
|
|
|
+ reader = null;
|
|
|
|
+ } finally {
|
|
|
|
+ if (conn != null) {
|
|
|
|
+ conn.disconnect();
|
|
|
|
+ conn = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String saveFile(String siteId, FileStoragePathEnvInfo env, File file) {
|
|
|
|
+ try {
|
|
|
|
+ String relativePath=postObject(siteId, env, file, null);
|
|
|
|
+ AliyunSite as=AliyunSiteManager.getAliyunSite(siteId);
|
|
|
|
+ return FileStorageType.ALIYUN+"-"+as.getAliyunId()+"://"+relativePath;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new StatusException("5001", "上传出错",e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|