caozixuan 3 лет назад
Родитель
Сommit
cb32236ab5

+ 120 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/FileStoreUtil.java

@@ -0,0 +1,120 @@
+package com.qmth.teachcloud.common.util;
+
+import com.qmth.boot.core.fss.service.FileService;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Resource;
+import java.io.*;
+
+/**
+ * @Description: 文件存储工具类
+ * @Author: CaoZixuan
+ * @Date: 2021-08-17
+ */
+public class FileStoreUtil {
+    private final static Logger log = LoggerFactory.getLogger(FileStoreUtil.class);
+
+    @Resource
+    private FileService fileService;
+
+    /**
+     * 上传文件
+     *
+     * @param dirName 上传到地址
+     * @param inputStream 流
+     * @param type fileStore类型
+     */
+    public void ossUpload(String dirName, InputStream inputStream, String type) throws Exception {
+        log.info("ossUpload is come in");
+        fileService.getFileStore(type).write(dirName,inputStream,DigestUtils.md5Hex(inputStream));
+        log.info("dirName:{}", dirName);
+    }
+
+    /**
+     * 上传文件
+     *
+     * @param dirName 上传到地址
+     * @param file 文件
+     * @param type fileStore类型
+     */
+    public void ossUpload(String dirName, File file,String type) throws Exception {
+        log.info("ossUpload is come in");
+        fileService.getFileStore(type).write(dirName,new FileInputStream(file), DigestUtils.md5Hex(new FileInputStream(file)));
+        log.info("dirName:{}", dirName);
+    }
+
+    /**
+     * 上传文件
+     *
+     * @param dirName 上传到地址
+     * @param content 文件
+     * @param type fileStore类型
+     */
+    public void ossUpload(String dirName, String content,String type) throws Exception {
+        log.info("ossUpload is come in");
+        fileService.getFileStore(type).write(dirName,new ByteArrayInputStream(content.getBytes()), DigestUtils.md5Hex(new ByteArrayInputStream(content.getBytes())));
+        log.info("dirName:{}", dirName);
+    }
+
+    /**
+     * 从文件存储上下载文件到本地
+     *
+     * @param dirName 文件地址
+     * @param localPath 本地路径
+     * @param type fileStore类型
+     * @throws IOException
+     */
+    public File ossDownload(String dirName, String localPath ,String type) throws Exception {
+        log.info("ossDownload is come in");
+        return this.saveLocal(fileService.getFileStore(type).read(dirName),localPath);
+    }
+
+
+    /**
+     * 从文件存储上下载文件到byte[]
+     *
+     * @param objectName 文件地址
+     * @param type fileStore类型
+     * @throws Exception
+     */
+    public byte[] ossDownload(String objectName,String type) throws Exception {
+        log.info("oss Download is come in");
+        return IOUtils.toByteArray(fileService.getFileStore(type).read(objectName));
+    }
+
+    /**
+     * 文件存在某本地路径
+     * @param inputStream 输入流
+     * @param dirPath 本地路径
+     * @return 存好的文件
+     * @throws IOException
+     */
+    private File saveLocal(InputStream inputStream, String dirPath) throws IOException {
+        File desFile = new File(dirPath);
+        if (!desFile.getParentFile().exists()) {
+            desFile.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
+        }
+        byte[] bytes = new byte[1024]; // 开辟一个拷贝缓冲区
+        OutputStream outputStream = null;
+        try {
+            outputStream = new FileOutputStream(desFile);
+            int length;
+            while ((length = inputStream.read(bytes)) != -1) { //当读到尽头后,返回值为-1这个时候停止输出,拷贝结束
+                outputStream.write(bytes, 0, length);
+            }
+            return desFile;
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            if (outputStream != null) {
+                outputStream.close();
+            }
+        }
+    }
+}