|
@@ -0,0 +1,209 @@
|
|
|
+package com.gis.common.util;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
+import com.aliyun.oss.model.OSSObjectSummary;
|
|
|
+import com.aliyun.oss.model.ObjectListing;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 阿里云oss工具类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AliyunOssUtil {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
+
|
|
|
+ private OSSClient init(){
|
|
|
+ return new OSSClient(configConstant.ossPoint, configConstant.ossKey, configConstant.ossSecrecy);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void delete(String key) throws IOException {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
+ ossClient.deleteObject(configConstant.ossBucket, key);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传的数据是byte[],key是上传后的文件名
|
|
|
+ public void upload(byte[] data, String key) throws IOException {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
+ ossClient.putObject(configConstant.ossBucket, key, new ByteArrayInputStream(data));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void upload(String filePath, String key) {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
+ }
|
|
|
+ ossClient.putObject(configConstant.ossBucket, key, new File(filePath));
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void upload2(String filePath, String key) {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
+ ossClient.putObject(configConstant.ossBucket, key, new File(filePath));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
|
|
|
+ *
|
|
|
+ * @param filepaths key : 原文件路径
|
|
|
+ * value: oss路径, oss会自动创建目录
|
|
|
+ */
|
|
|
+
|
|
|
+ public void uploadMulFiles(Map<String, String> filepaths) {
|
|
|
+ if (filepaths == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("开始批量上传到阿里云:" + new Date().toString());
|
|
|
+ if (filepaths.size() > 50) {
|
|
|
+ int i = 1;
|
|
|
+ for (String filePath : filepaths.keySet()) {
|
|
|
+ if (i%100 == 0) {
|
|
|
+
|
|
|
+ log.info("i= " +i + ", 文件:" + filePath + "到阿里云:" + filepaths.get(filePath));
|
|
|
+ }
|
|
|
+ upload2(filePath, filepaths.get(filePath));
|
|
|
+ i ++ ;
|
|
|
+ }
|
|
|
+ log.info("上传文件数量:"+ i);
|
|
|
+ } else {
|
|
|
+ for (String filePath : filepaths.keySet()) {
|
|
|
+ log.debug("文件:" + filePath + "到阿里云:" + filepaths.get(filePath));
|
|
|
+ upload(filePath, filepaths.get(filePath));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("批量上传阿里云完毕:" + new Date().toString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 可以删除目录
|
|
|
+ *
|
|
|
+ * @param prefix 图片路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int deleteFile(String prefix) {
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ ObjectListing objectListing = ossClient.listObjects(configConstant.ossBucket, prefix);
|
|
|
+ List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
+ try {
|
|
|
+ for (OSSObjectSummary s : sums) {
|
|
|
+ delete(s.getKey());
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return sums.size();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过文件名判断并获取OSS服务文件上传时文件的contentType
|
|
|
+ *
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @return 文件的contentType
|
|
|
+ */
|
|
|
+ private static String getContentType(String fileName) {
|
|
|
+ log.info("getContentType:" + fileName);
|
|
|
+ // 文件的后缀名
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ if (".bmp".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/bmp";
|
|
|
+ }
|
|
|
+ if (".gif".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/gif";
|
|
|
+ }
|
|
|
+ if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|
|
|
+ || ".png".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "image/jpeg";
|
|
|
+ }
|
|
|
+ if (".html".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/html";
|
|
|
+ }
|
|
|
+ if (".txt".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/plain";
|
|
|
+ }
|
|
|
+ if (".vsd".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/vnd.visio";
|
|
|
+ }
|
|
|
+ if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/vnd.ms-powerpoint";
|
|
|
+ }
|
|
|
+ if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/msword";
|
|
|
+ }
|
|
|
+ if (".xml".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "text/xml";
|
|
|
+ }
|
|
|
+ if (".pdf".equalsIgnoreCase(fileExtension)) {
|
|
|
+ return "application/pdf";
|
|
|
+ }
|
|
|
+ // 默认返回类型
|
|
|
+ return "image/jpeg";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 以流下载图片
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @Title: getInputStreamByFileUrl
|
|
|
+ * @Description: 根据文件路径获取InputStream流
|
|
|
+ * @return: InputStream
|
|
|
+ */
|
|
|
+ public InputStream getInputStreamByFileUrl(String filePath) {
|
|
|
+ // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
+ OSSClient ossClient = init();
|
|
|
+ OSSObject ossObject = ossClient.getObject(configConstant.ossBucket, filePath);
|
|
|
+ return ossObject.getObjectContent();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|