xiatian 5 роки тому
батько
коміт
71205bd9c1

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/web/filestorage/FileStorageUtil.java

@@ -385,7 +385,7 @@ public class FileStorageUtil {
 		if(path.indexOf(connector)==-1) {
 			throw new StatusException("9001", "文件路径格式错误:"+path);
 		}
-		String rpath = path.substring(path.indexOf(connector) + 4);
+		String rpath = path.substring(path.indexOf(connector) + 3);
 		if(StringUtils.isBlank(rpath)) {
 			throw new StatusException("9002", "文件路径格式错误:"+path);
 		}

+ 310 - 332
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/AliyunFileStorageImpl.java

@@ -1,24 +1,13 @@
 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.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
 import java.util.Map;
 
-import javax.activation.MimetypesFileTypeMap;
-
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.stereotype.Service;
@@ -27,8 +16,6 @@ import com.aliyun.oss.OSS;
 import com.google.common.collect.Maps;
 
 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.DateUtil;
 import cn.com.qmth.examcloud.commons.util.FreeMarkerUtil;
 import cn.com.qmth.examcloud.web.aliyun.AliYunAccount;
@@ -43,26 +30,17 @@ import cn.com.qmth.examcloud.web.filestorage.YunPathInfo;
 
 @Service(value = "aliyunFileStorage")
 public class AliyunFileStorageImpl implements FileStorage {
-	private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
+//	private ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
 
 	// 文件最大大小(byte)
 	private static int maxFileSize = 100 * 1024 * 1024;
 
 	@Override
 	public YunPathInfo 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);
-			AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
-			String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
-			YunPathInfo pi=new YunPathInfo(url, FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath);
-			return pi;
-		} catch (IOException e) {
-			throw new StatusException("1001", "上传出错", e);
-		}
+		String siteId = "transPath";
+		FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
+		env.setRelativePath(path);
+		return saveFile(siteId, env, file, null);
 	}
 
 	@Override
@@ -83,171 +61,175 @@ public class AliyunFileStorageImpl implements FileStorage {
 	 * @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 = null; // 提交表单的URL为bucket域名
-		if(ossEndpoint.startsWith("https://")) {
-			urlStr = ossEndpoint.replace("https://", "https://" + bucket + "."); 
-		}else if(ossEndpoint.startsWith("http://")) {
-			urlStr = ossEndpoint.replace("http://", "http://" + 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;
-	}
+//	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 = null; // 提交表单的URL为bucket域名
+//		if(ossEndpoint.startsWith("https://")) {
+//			urlStr = ossEndpoint.replace("https://", "https://" + bucket + "."); 
+//		}else if(ossEndpoint.startsWith("http://")) {
+//			urlStr = ossEndpoint.replace("http://", "http://" + 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 YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
+		InputStream in=null;
 		try {
-			String relativePath = uploadObject(siteId, env, file, null);
-			AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
-			AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
-			String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
-			YunPathInfo pi=new YunPathInfo(url, FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath);
-			return pi;
-		} catch (IOException e) {
+			in = new FileInputStream(file);
+			return saveFile(siteId, env, in, md5);
+		} catch (FileNotFoundException e) {
 			throw new StatusException("5001", "上传出错", e);
+		} finally {
+			if(in!=null) {
+				try {
+					in.close();
+				} catch (IOException e) {
+				}
+			}
 		}
 	}
 
@@ -299,140 +281,140 @@ public class AliyunFileStorageImpl implements FileStorage {
 	}
 
 	
-	@SuppressWarnings("unused")
-	private  String postObjectByInputStream(String siteId, FileStoragePathEnvInfo env, InputStream in, 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 filename = path.substring(path.lastIndexOf("/") + 1);
-		String urlStr = null; // 提交表单的URL为bucket域名
-		if(ossEndpoint.startsWith("https://")) {
-			urlStr = ossEndpoint.replace("https://", "https://" + bucket + "."); 
-		}else if(ossEndpoint.startsWith("http://")) {
-			urlStr = ossEndpoint.replace("http://", "http://" + 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);
-
-
-		String ret = formUploadByInputStream(urlStr, textMap, in,filename);
-		log.info("oss上传:" + ret);
-		return path;
-	}
-
-	@SuppressWarnings("rawtypes")
-	private static String formUploadByInputStream(String urlStr, Map<String, String> textMap, InputStream filein,String fileName )
-			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
-
-					StringBuffer strBufFile = new StringBuffer();
-					strBufFile.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
-					strBufFile.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName
-							+ "\"\r\n");
-					strBufFile.append("Content-Type: application/octet-stream\r\n\r\n");
-
-					out.write(strBufFile.toString().getBytes());
-
-					DataInputStream in = new DataInputStream(filein);
-					int bytes = 0;
-					byte[] bufferOut = new byte[1024];
-					while ((bytes = in.read(bufferOut)) != -1) {
-						out.write(bufferOut, 0, bytes);
-					}
-					in.close();
-				StringBuffer strBufTag = new StringBuffer();
-				out.write(strBufTag.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;
-	}
+//	@SuppressWarnings("unused")
+//	private  String postObjectByInputStream(String siteId, FileStoragePathEnvInfo env, InputStream in, 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 filename = path.substring(path.lastIndexOf("/") + 1);
+//		String urlStr = null; // 提交表单的URL为bucket域名
+//		if(ossEndpoint.startsWith("https://")) {
+//			urlStr = ossEndpoint.replace("https://", "https://" + bucket + "."); 
+//		}else if(ossEndpoint.startsWith("http://")) {
+//			urlStr = ossEndpoint.replace("http://", "http://" + 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);
+//
+//
+//		String ret = formUploadByInputStream(urlStr, textMap, in,filename);
+//		log.info("oss上传:" + ret);
+//		return path;
+//	}
+
+//	@SuppressWarnings("rawtypes")
+//	private static String formUploadByInputStream(String urlStr, Map<String, String> textMap, InputStream filein,String fileName )
+//			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
+//
+//					StringBuffer strBufFile = new StringBuffer();
+//					strBufFile.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
+//					strBufFile.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName
+//							+ "\"\r\n");
+//					strBufFile.append("Content-Type: application/octet-stream\r\n\r\n");
+//
+//					out.write(strBufFile.toString().getBytes());
+//
+//					DataInputStream in = new DataInputStream(filein);
+//					int bytes = 0;
+//					byte[] bufferOut = new byte[1024];
+//					while ((bytes = in.read(bufferOut)) != -1) {
+//						out.write(bufferOut, 0, bytes);
+//					}
+//					in.close();
+//				StringBuffer strBufTag = new StringBuffer();
+//				out.write(strBufTag.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 YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) {
@@ -441,7 +423,7 @@ public class AliyunFileStorageImpl implements FileStorage {
 			AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
 			AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
 			String url=FileStorageUtil.getUrl(ac.getDomain(), relativePath);
-			YunPathInfo pi=new YunPathInfo(url, FileStorageType.ALIYUN + "-" + as.getAliyunId() + "://" + relativePath);
+			YunPathInfo pi=new YunPathInfo(url, getTreatyPath(as.getAliyunId(), relativePath));
 			return pi;
 		} catch (IOException e) {
 			throw new StatusException("6001", "上传出错", e);
@@ -488,15 +470,11 @@ public class AliyunFileStorageImpl implements FileStorage {
 
 		return path;
 	}
-	private String uploadObject(String siteId, FileStoragePathEnvInfo env,File file, String md5) throws IOException {
-		InputStream in=null;
-		try {
-			in = new FileInputStream(file);
-			return uploadObject(siteId, env, in, md5);
-		} finally {
-			if(in!=null) {
-				in.close();
-			}
+	private String getTreatyPath(String yunId,String relativePath) {
+		if(relativePath.startsWith("/")) {
+			relativePath=relativePath.substring(1);
 		}
+		String path=FileStorageType.ALIYUN+"-"+yunId+"://"+relativePath;
+		return path;
 	}
 }

+ 30 - 17
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/UpyunFileStorageImpl.java

@@ -1,12 +1,16 @@
 package cn.com.qmth.examcloud.web.filestorage.impl;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.web.filestorage.FileStorage;
 import cn.com.qmth.examcloud.web.filestorage.FileStoragePathEnvInfo;
 import cn.com.qmth.examcloud.web.filestorage.FileStorageType;
@@ -30,15 +34,9 @@ public class UpyunFileStorageImpl implements FileStorage {
 	@Override
 	public YunPathInfo saveFile(File file, String path) {
 		String siteId="transPath";
-		UpyunPathEnvironmentInfo env = new UpyunPathEnvironmentInfo();
+		FileStoragePathEnvInfo env = new FileStoragePathEnvInfo();
 		env.setRelativePath(path);
-		String fpath = upyunService.writeFile(siteId, env, file, null).getRelativePath();
-		UpyunSite site=UpyunSiteManager.getUpyunSite(siteId);
-		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(site.getUpyunId());
-		String relativePath=FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+fpath;
-		String url=FileStorageUtil.getUrl(c.getDomain(), fpath);
-		YunPathInfo pi=new YunPathInfo(url, relativePath);
-		return pi;
+		return saveFile(siteId, env, file, null);
 	}
 
 	@Override
@@ -51,13 +49,20 @@ public class UpyunFileStorageImpl implements FileStorage {
 
 	@Override
 	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, File file,String md5) {
-		UpYunPathInfo pathInfo =upyunService.writeFile(siteId, of(env), file, md5);
-		UpyunSite site=UpyunSiteManager.getUpyunSite(siteId);
-		String relativePath=FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+pathInfo.getRelativePath();
-		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(site.getUpyunId());
-		String url=FileStorageUtil.getUrl(c.getDomain(), pathInfo.getRelativePath());
-		YunPathInfo pi=new YunPathInfo(url, relativePath);
-		return pi;
+		InputStream in=null;
+		try {
+			in = new FileInputStream(file);
+			return saveFile(siteId, env, in, md5);
+		} catch (FileNotFoundException e) {
+			throw new StatusException("1001", "上传出错", e);
+		} finally {
+			if(in!=null) {
+				try {
+					in.close();
+				} catch (IOException e) {
+				}
+			}
+		}
 	}
 	
 	private UpyunPathEnvironmentInfo of(FileStoragePathEnvInfo env) {
@@ -90,14 +95,14 @@ public class UpyunFileStorageImpl implements FileStorage {
 	public void deleteFile(String path) {
 		String upyunId=FileStorageUtil.getYunId(path);
 		String urlpath=FileStorageUtil.getPath(path);
-		upyunService.deleteByUpyunId(upyunId, urlpath);;
+		upyunService.deleteByUpyunId(upyunId, urlpath);
 	}
 
 	@Override
 	public YunPathInfo saveFile(String siteId, FileStoragePathEnvInfo env, InputStream in, String md5) {
 		UpYunPathInfo pathInfo =upyunService.writeFile(siteId, of(env), in, md5);
 		UpyunSite site=UpyunSiteManager.getUpyunSite(siteId);
-		String relativePath=FileStorageType.UPYUN+"-"+site.getUpyunId()+"://"+pathInfo.getRelativePath();
+		String relativePath=getTreatyPath(site.getUpyunId(),pathInfo.getRelativePath());
 		UpYunClient c=UpyunSiteManager.getUpYunClientByUpyunId(site.getUpyunId());
 		String url=FileStorageUtil.getUrl(c.getDomain(), pathInfo.getRelativePath());
 		YunPathInfo pi=new YunPathInfo(url, relativePath);
@@ -115,5 +120,13 @@ public class UpyunFileStorageImpl implements FileStorage {
 		}
 		return FileStorageUtil.getUrl(c.getDomain(), urlpath);
 	}
+	
+	private String getTreatyPath(String yunId,String relativePath) {
+		if(relativePath.startsWith("/")) {
+			relativePath=relativePath.substring(1);
+		}
+		String path=FileStorageType.UPYUN+"-"+yunId+"://"+relativePath;
+		return path;
+	}
 
 }