WANG 6 năm trước cách đây
mục cha
commit
521be0ba42

+ 30 - 4
src/main/java/cn/com/qmth/examcloud/web/upyun/UpYunClient.java

@@ -143,6 +143,7 @@ public class UpYunClient {
 		String date = getDate();
 		String authorization = null;
 		try {
+			md5 = null == md5 ? "" : md5;
 			authorization = sign(userName, md5Password, METHOD_PUT, path, date, "", md5);
 		} catch (Exception e) {
 			throw new StatusException("100005", "[upyun]. fail to build sign", e);
@@ -161,20 +162,29 @@ public class UpYunClient {
 		return sign;
 	}
 
+	public UpYunPathInfo writeFile(String filePath, File file) {
+		return writeFile(filePath, file, false);
+	}
+
 	/**
 	 * 上传文件
 	 *
 	 * @author WANGWEI
 	 * @param filePath
 	 * @param file
+	 * @param withMd5
 	 * @return
 	 */
-	public UpYunPathInfo writeFile(String filePath, File file) {
+	public UpYunPathInfo writeFile(String filePath, File file, boolean withMd5) {
+		String md5 = null;
+		if (withMd5) {
+			md5 = MD5.md5Hex(file);
+		}
 
 		InputStream in = null;
 		try {
 			in = new FileInputStream(file);
-			return writeFile(filePath, in);
+			return writeFile(filePath, in, md5);
 		} catch (FileNotFoundException e) {
 			throw new ExamCloudRuntimeException(e);
 		} finally {
@@ -191,6 +201,19 @@ public class UpYunClient {
 	 * @return
 	 */
 	public UpYunPathInfo writeFile(String filePath, InputStream in) {
+		return writeFile(filePath, in, "");
+	}
+
+	/**
+	 * 上传文件
+	 *
+	 * @author WANGWEI
+	 * @param filePath
+	 * @param in
+	 * @param md5
+	 * @return
+	 */
+	public UpYunPathInfo writeFile(String filePath, InputStream in, String md5) {
 		String path = formatPath(filePath);
 		String url = "https://" + API_DOMAIN + path;
 
@@ -200,12 +223,15 @@ public class UpYunClient {
 
 		long s = System.currentTimeMillis();
 		try {
-
+			md5 = null == md5 ? "" : md5;
 			String date = getDate();
-			String authorization = sign(userName, md5Password, METHOD_PUT, path, date, "", "");
+			String authorization = sign(userName, md5Password, METHOD_PUT, path, date, "", md5);
 			httpPut.addHeader(AUTHORIZATION, authorization);
 			httpPut.addHeader(DATE, date);
 			httpPut.addHeader(MKDIR, "true");
+			if (StringUtils.isNotBlank(md5)) {
+				httpPut.addHeader("Content-MD5", md5);
+			}
 
 			httpPut.setEntity(new InputStreamEntity(in));
 			response = httpclient.execute(httpPut);

+ 8 - 3
src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunService.java

@@ -19,9 +19,11 @@ public interface UpyunService {
 	 * @param siteId
 	 * @param env
 	 * @param in
+	 * @param md5
 	 * @return
 	 */
-	UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in);
+	UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in,
+			String md5);
 
 	/**
 	 * 写文件
@@ -30,9 +32,11 @@ public interface UpyunService {
 	 * @param siteId
 	 * @param env
 	 * @param file
+	 * @param withMd5
 	 * @return
 	 */
-	UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, File file);
+	UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, File file,
+			boolean withMd5);
 
 	/**
 	 * 删除文件
@@ -48,8 +52,9 @@ public interface UpyunService {
 	 *
 	 * @author WANGWEI
 	 * @param filePath
+	 * @param withMd5
 	 * @return
 	 */
-	UpYunSign buildUpYunSign(String siteId, UpyunPathEnvironmentInfo env);
+	UpYunSign buildUpYunSign(String siteId, UpyunPathEnvironmentInfo env, String md5);
 
 }

+ 8 - 6
src/main/java/cn/com/qmth/examcloud/web/upyun/UpyunServiceImpl.java

@@ -22,7 +22,8 @@ public class UpyunServiceImpl implements UpyunService {
 	protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
 
 	@Override
-	public UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in) {
+	public UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, InputStream in,
+			String md5) {
 
 		env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
 
@@ -30,12 +31,13 @@ public class UpyunServiceImpl implements UpyunService {
 		UpYunClient upYunClient = UpyunSiteManager.getUpYunClientByUpyunId(upyunSite.getUpyunId());
 
 		String path = FreeMarkerUtil.process(upyunSite.getPath(), env);
-		UpYunPathInfo pathInfo = upYunClient.writeFile(path, in);
+		UpYunPathInfo pathInfo = upYunClient.writeFile(path, in, md5);
 		return pathInfo;
 	}
 
 	@Override
-	public UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, File file) {
+	public UpYunPathInfo writeFile(String siteId, UpyunPathEnvironmentInfo env, File file,
+			boolean withMd5) {
 
 		env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
 
@@ -43,7 +45,7 @@ public class UpyunServiceImpl implements UpyunService {
 		UpYunClient upYunClient = UpyunSiteManager.getUpYunClientByUpyunId(upyunSite.getUpyunId());
 
 		String path = FreeMarkerUtil.process(upyunSite.getPath(), env);
-		UpYunPathInfo pathInfo = upYunClient.writeFile(path, file);
+		UpYunPathInfo pathInfo = upYunClient.writeFile(path, file, withMd5);
 		return pathInfo;
 	}
 
@@ -58,7 +60,7 @@ public class UpyunServiceImpl implements UpyunService {
 	}
 
 	@Override
-	public UpYunSign buildUpYunSign(String siteId, UpyunPathEnvironmentInfo env) {
+	public UpYunSign buildUpYunSign(String siteId, UpyunPathEnvironmentInfo env, String md5) {
 
 		env.setTimeMillis(String.valueOf(System.currentTimeMillis()));
 
@@ -66,7 +68,7 @@ public class UpyunServiceImpl implements UpyunService {
 		UpYunClient upYunClient = UpyunSiteManager.getUpYunClientByUpyunId(upyunSite.getUpyunId());
 
 		String path = FreeMarkerUtil.process(upyunSite.getPath(), env);
-		UpYunSign sign = upYunClient.buildUpYunSign(path);
+		UpYunSign sign = upYunClient.buildUpYunSign(path, md5);
 		return sign;
 	}