|
@@ -0,0 +1,90 @@
|
|
|
+package com.fdkankan.utils;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Component
|
|
|
+@ComponentScan
|
|
|
+public class UploadUtils {
|
|
|
+ private static Logger log = LoggerFactory.getLogger(UploadUtils.class);
|
|
|
+
|
|
|
+ @Value("${oss.point}")
|
|
|
+ private String point;
|
|
|
+
|
|
|
+ @Value("${oss.key}")
|
|
|
+ private String key;
|
|
|
+
|
|
|
+ @Value("${oss.secrey}")
|
|
|
+ private String secrey;
|
|
|
+
|
|
|
+ @Value("${oss.bucket}")
|
|
|
+ private String bucket;
|
|
|
+
|
|
|
+ public void upload(String filePath, String key1) {
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
+ }
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void upload2(String filePath, String key1) {
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
+ try {
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
+ }
|
|
|
+ if(filePath.contains(".mp4")){
|
|
|
+ metadata.setContentType("video/mp4");
|
|
|
+ }
|
|
|
+ if(filePath.contains(".mp3")){
|
|
|
+ metadata.setContentType("audio/mp3");
|
|
|
+ }
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
|
|
|
+ public void uploadMulFiles(Map<String, String> filepaths) {
|
|
|
+ if (filepaths == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long start = System.currentTimeMillis();
|
|
|
+ log.info("开始批量上传到阿里云:" + start);
|
|
|
+ if (filepaths.size() > 50) {
|
|
|
+ for (String filePath : filepaths.keySet()) {
|
|
|
+ // log.info("文件:"+key);
|
|
|
+ upload2(filePath, filepaths.get(filePath));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (String filePath : filepaths.keySet()) {
|
|
|
+ log.info("文件:" + filePath + "到阿里云:" + filepaths.get(filePath));
|
|
|
+ upload(filePath, filepaths.get(filePath));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("批量上传阿里云完毕:" + (System.currentTimeMillis() - start));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|