Quellcode durchsuchen

oss后台上传加入md5校验

xiatian vor 5 Jahren
Ursprung
Commit
a75e5c1bb4

+ 14 - 3
src/main/java/cn/com/qmth/examcloud/web/filestorage/impl/AliyunFileStorageImpl.java

@@ -5,14 +5,18 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Base64;
 import java.util.Date;
 import java.util.Map;
 
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.stereotype.Service;
 
 import com.aliyun.oss.OSS;
+import com.aliyun.oss.model.ObjectMetadata;
 import com.google.common.collect.Maps;
 
 import cn.com.qmth.examcloud.commons.exception.StatusException;
@@ -425,7 +429,7 @@ public class AliyunFileStorageImpl implements FileStorage {
 			String url=FileStorageHelper.getUrl(ac.getDomain(), relativePath);
 			YunPathInfo pi=new YunPathInfo(url, getTreatyPath(as.getAliyunId(), relativePath));
 			return pi;
-		} catch (IOException e) {
+		} catch (Exception e) {
 			throw new StatusException("6001", "上传出错", e);
 		}
 	}
@@ -457,7 +461,7 @@ public class AliyunFileStorageImpl implements FileStorage {
 //		oss.deleteObject(bucket, urlpath);
 		
 	}
-	private String uploadObject(String siteId, FileStoragePathEnvInfo env,InputStream in, String md5) throws IOException {
+	private String uploadObject(String siteId, FileStoragePathEnvInfo env,InputStream in, String md5) throws IOException, DecoderException {
 		AliyunSite as = AliyunSiteManager.getAliyunSite(siteId);
 		AliYunAccount ac = AliyunSiteManager.getAliYunAccountByAliyunId(as.getAliyunId());
 		String bucket = ac.getBucket();
@@ -465,7 +469,14 @@ public class AliyunFileStorageImpl implements FileStorage {
 		// 阿里云文件路径
 		String path = FreeMarkerUtil.process(as.getPath(), env);
 		path=disposePath(path);
-		oss.putObject(bucket, path, in);
+		if(StringUtils.isNotBlank(md5)) {
+		    md5=Base64.getEncoder().encodeToString(Hex.decodeHex(md5));
+		    ObjectMetadata meta = new ObjectMetadata();
+            meta.setContentMD5(md5);
+            oss.putObject(bucket, path, in,meta);
+		}else {
+		    oss.putObject(bucket, path, in);
+		}
 
 		return path;
 	}