|
@@ -1,346 +0,0 @@
|
|
|
-package com.gis.oss.util;
|
|
|
-
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
-import cn.hutool.core.util.RuntimeUtil;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.model.*;
|
|
|
-import com.gis.oss.command.CmdConstant;
|
|
|
-import com.gis.oss.command.CmdUtils;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.*;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-
|
|
|
-/**
|
|
|
- * 阿里云oss工具类
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class AliYunOssUtil {
|
|
|
-
|
|
|
-// @Autowired
|
|
|
-// ConfigConstant configConstant;
|
|
|
-
|
|
|
- @Value("${oss.key}")
|
|
|
- public String ossKey;
|
|
|
-
|
|
|
- @Value("${oss.secrecy}")
|
|
|
- public String ossSecrecy;
|
|
|
-
|
|
|
- @Value("${oss.bucket}")
|
|
|
- public String ossBucket;
|
|
|
-
|
|
|
- @Value("${oss.point}")
|
|
|
- public String ossPoint;
|
|
|
-
|
|
|
-
|
|
|
- // 加载对象
|
|
|
- private OSSClient init(){
|
|
|
-// return new OSSClient(configConstant.ossPoint, configConstant.ossKey, configConstant.ossSecrecy);
|
|
|
- return new OSSClient(ossPoint, ossKey, ossSecrecy);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void delete(String key) throws IOException {
|
|
|
- OSSClient ossClient = init();
|
|
|
- try {
|
|
|
-
|
|
|
- // 2019-2-28 启动aliyun oss 空间
|
|
|
- ossClient.deleteObject(ossBucket, key);
|
|
|
- ossClient.deleteObject(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(ossBucket, key, new ByteArrayInputStream(data));
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.toString() + key);
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- * 获取文件内容-阿里云
|
|
|
- * @param objectName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public boolean existKey(String bucket,String objectName){
|
|
|
- //创建oss客户端
|
|
|
- OSSClient ossClient = init();
|
|
|
- // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
- try{
|
|
|
- boolean exist = ossClient.doesObjectExist(bucket, objectName);
|
|
|
- return exist;
|
|
|
- }catch (Exception e){
|
|
|
- log.error("s4判断是否存在key异常,key=" + objectName, e);
|
|
|
- }finally {
|
|
|
- if(ossClient != null){
|
|
|
- ossClient.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param data
|
|
|
- * @param key
|
|
|
- * @param contentType : 不设置,默认浏览器打开图片
|
|
|
- * application/octet-stream 使用cdn后会自动下载
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public void upload(byte[] data, String key, String contentType) throws IOException {
|
|
|
- OSSClient ossClient = init();
|
|
|
-
|
|
|
- // 创建上传文件的元信息,可以通过文件元信息设置HTTP header。
|
|
|
- ObjectMetadata meta = new ObjectMetadata();
|
|
|
- meta.setContentType(contentType);
|
|
|
-
|
|
|
- try {
|
|
|
- // 2019-2-28 启动aliyun oss 空间
|
|
|
- ossClient.putObject(ossBucket, key, new ByteArrayInputStream(data), meta);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.toString() + key);
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param data
|
|
|
- * @param key
|
|
|
- * application/octet-stream 使用cdn后会自动下载
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public void upload(String bucket,byte[] data, String key ) throws IOException {
|
|
|
- OSSClient ossClient = init();
|
|
|
- // 创建上传文件的元信息,可以通过文件元信息设置HTTP header。
|
|
|
- try {
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- if (key.contains(".jpg")) {
|
|
|
- metadata.setContentType("image/jpeg");
|
|
|
- } else if (key.contains(".png")) {
|
|
|
- metadata.setContentType("image/png");
|
|
|
- } else if (key.contains(".json")) {
|
|
|
- metadata.setContentType("application/json");
|
|
|
- }
|
|
|
- // 2019-2-28 启动aliyun oss 空间
|
|
|
- ossClient.putObject(bucket, key, new ByteArrayInputStream(data),metadata);
|
|
|
- } 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);
|
|
|
- }
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- if (filePath.contains(".jpg")) {
|
|
|
- metadata.setContentType("image/jpeg");
|
|
|
- } else if (filePath.contains(".png")) {
|
|
|
- metadata.setContentType("image/png");
|
|
|
- } else if (filePath.contains(".json")) {
|
|
|
- metadata.setContentType("application/json");
|
|
|
- }
|
|
|
- ossClient.putObject(ossBucket, key, new File(filePath),metadata);
|
|
|
-
|
|
|
- } 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(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) {
|
|
|
- for (String filePath : filepaths.keySet()) {
|
|
|
- upload2(filePath, filepaths.get(filePath));
|
|
|
- }
|
|
|
- } else {
|
|
|
- for (String filePath : filepaths.keySet()) {
|
|
|
- log.info("文件:" + filePath + "到阿里云:" + filepaths.get(filePath));
|
|
|
- upload(filePath, filepaths.get(filePath));
|
|
|
- }
|
|
|
- }
|
|
|
- log.info("批量上传阿里云完毕:" + new Date().toString());
|
|
|
- }
|
|
|
-
|
|
|
- public List<String> getFileFolder(String bucket, String keyName) {
|
|
|
- log.info("获取文件夹bucket-{},路径{}",bucket,keyName);
|
|
|
- OSSClient ossClient = init();
|
|
|
- List<String> keyList = new ArrayList<>();
|
|
|
- try {
|
|
|
- boolean flag = true;
|
|
|
- String nextMaker = null;
|
|
|
- ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket);
|
|
|
- //指定下一级文件
|
|
|
- listObjectsRequest.setPrefix(keyName);
|
|
|
- //设置分页的页容量
|
|
|
- listObjectsRequest.setMaxKeys(200);
|
|
|
- do {
|
|
|
- //获取下一页的起始点,它的下一项
|
|
|
- listObjectsRequest.setMarker(nextMaker);
|
|
|
- ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
|
|
|
- List<String> collect = objectListing.getObjectSummaries().parallelStream()
|
|
|
- .map(OSSObjectSummary::getKey).collect(Collectors.toList());
|
|
|
- if (!CollectionUtils.isEmpty(collect)) {
|
|
|
- keyList.addAll(collect);
|
|
|
- }
|
|
|
- nextMaker = objectListing.getNextMarker();
|
|
|
- //全部执行完后,为false
|
|
|
- flag = objectListing.isTruncated();
|
|
|
- } while (flag);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取文件列表失败,path=" + keyName, e);
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- log.info("获取文件夹集合={}", JSON.toJSONString(keyList));
|
|
|
- return keyList;
|
|
|
- }
|
|
|
- public ObjectMetadata downloadFile(String bucket, String pathKey, String file) {
|
|
|
- OSSClient ossClient = init();
|
|
|
- GetObjectRequest request = new GetObjectRequest(bucket, pathKey);
|
|
|
- log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
|
|
|
- File downloadFile = new File(file);
|
|
|
- if (!FileUtil.exist(downloadFile.getParent())){
|
|
|
- FileUtil.mkdir(downloadFile.getParent());
|
|
|
- }
|
|
|
- return ossClient.getObject(request, downloadFile);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 可以删除目录
|
|
|
- *
|
|
|
- * @param prefix 图片路径
|
|
|
- * @return
|
|
|
- */
|
|
|
- public int deleteFile(String prefix) {
|
|
|
- OSSClient ossClient = init();
|
|
|
- ObjectListing objectListing = ossClient.listObjects(ossBucket, prefix);
|
|
|
- List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
- try {
|
|
|
- for (OSSObjectSummary s : sums) {
|
|
|
- delete(s.getKey());
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return sums.size();
|
|
|
- }
|
|
|
-
|
|
|
- public void main(String[] args) throws IOException {
|
|
|
-
|
|
|
- HashMap<String, String> map = new HashMap<>();
|
|
|
-// map.put("F:\\test\\aa.jpg", "kanfang/test/aa.jpg");
|
|
|
- map.put("F:\\test\\oss\\floor.json", "kanfang/test/faa.json");
|
|
|
- uploadMulFiles(map);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过文件名判断并获取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(ossBucket, filePath);
|
|
|
- return ossObject.getObjectContent();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public void uploadBySh(String filePath, String key) {
|
|
|
- String cmd = String.format(CmdConstant.UPLOAD_DIR, ossBucket, filePath, key , "oss");
|
|
|
- CmdUtils.callShell(cmd);
|
|
|
- }
|
|
|
-}
|