UploadUtil.java 4.1 KB
package com.ruoyi.utils;

import cn.hutool.core.codec.Base64;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.config.BsthSystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Objects;

/**
 * @author liujun
 * @date 2024年07月25日 14:13
 */
@Slf4j
@Component
public class UploadUtil {

    @Autowired
    private BsthSystemConfig bsthSystemConfig;

    /**
     * 上传图片接口
     *
     * @param content 图片内容
     * @param path    图片路径(相对路径)
     * @param type    图片类型
     * @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
     * @author liujun
     * @date 2024/7/25 14:31
     */
    public ResponseResult<Boolean> uploadImageOfBase64(String content, String path, String type) {
        if (StringUtils.isEmpty(content)) {
            return ResponseResult.error("上传的图片内容为空");
        }

        ByteArrayInputStream inputStream = null;
        BufferedImage bufferedImage = null;
        try {
            content = FileUploadUtils.choosePrictureContent(content);
            byte[] bytes = Base64.decode(content);
            inputStream = new ByteArrayInputStream(bytes);
            bufferedImage = ImageIO.read(inputStream);
            if (bufferedImage.getWidth() < 0 || bufferedImage.getHeight() < 0) {
                return ResponseResult.error("请上传正确的图片");
            }

            StringBuilder filePath = new StringBuilder();
            filePath.append(bsthSystemConfig.getImageBasePath());
            filePath.append(File.separator);
            filePath.append(path);

            File file = new File(filePath.toString());
            if(!file.exists() || !file.isDirectory()){
                FileUtils.forceMkdirParent(file);
            }

            ImageIO.write(bufferedImage, type, file);
            bufferedImage.flush();

            return ResponseResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("上传图片异常:[{}]", path, e);
            return ResponseResult.error("上传图片异常");
        } finally {
            if (Objects.nonNull(inputStream)) {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }

    /**
     * 上传视频文件
     *
     * @param content
     * @param path
     * @return com.ruoyi.common.core.domain.ResponseResult<java.lang.Boolean>
     * @author liujun
     * @date 2024/7/25 14:37
     */
    public ResponseResult<Boolean> uploadVideoOfBase64(String content, String path) {
        if (StringUtils.isEmpty(content)) {
            return ResponseResult.error("上传的视频内容为空");
        }

        FileOutputStream outputStream = null;
        try {
            content = FileUploadUtils.choosePrictureContent(content);
            byte[] bytes = Base64.decode(content);

            StringBuilder filePath = new StringBuilder();
            filePath.append(bsthSystemConfig.getImageBasePath());
            filePath.append(File.separator);
            filePath.append(path);

            File file = new File(filePath.toString());
            if(!file.exists() || !file.isDirectory()){
                FileUtils.forceMkdirParent(file);
            }
            outputStream = new FileOutputStream(file);
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();

            return ResponseResult.success(Boolean.TRUE);
        } catch (Exception e) {
            log.error("上传视频异常:[{}]", path, e);
            return ResponseResult.error("上传视频异常");
        } finally {
            if (Objects.nonNull(outputStream)) {
                IOUtils.closeQuietly(outputStream);
            }
        }
    }
}