图片云存储,以及校验大小宽高是否是真实图片后缀名

**

工具类

依赖

		<dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

**


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.holy.cn.common.exception.ImgException;
import com.holy.cn.common.http.HolyStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import javax.servlet.http.Part;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 开发者:Edward.wei
 * 特点:
 * 开发时间:2021/6/15 11:55
 * 文件说明:
 */
@Component
public class UploadUtils {
    @Autowired
    uploadProperties uploadProperties;

    public  String upload(String fileName, Part part) throws IOException {
        InputStream in = part.getInputStream();
        BufferedImage read = ImageIO.read(in);//用圖片流讀,如果為null,就是沒讀到證明不是圖片
        if (read == null)//校驗是否是圖片
            throw new ImgException(HolyStatus.IS_NOT_IMAGE);
        if (!conformExt(fileName))//校驗文件後綴名
            throw new ImgException(HolyStatus.IMAGE_EXT_ERROR);
        if (!conformSize(part.getInputStream()))//校驗文件大小
            throw new ImgException(HolyStatus.IMAGE_TOO_BIG);
        if (!conformWidthAndHeight(read))//校驗文件寬高
            throw new ImgException(HolyStatus.IMAGE_TOO_LARGE);
        OSS ossClient = new OSSClientBuilder().build(uploadProperties.getEndpoint(), uploadProperties.getAccessKeyId(), uploadProperties.getAccessKeySecret());
        //第一参数: 表示bucket名称
        //第二个参数: 文件名称 携带后缀
        ossClient.putObject(uploadProperties.getBucket(), fileName, part.getInputStream());//這裡必須重新拿流,上面的流已經被賦值走了
        ossClient.shutdown();
        String url = uploadProperties.getBaseURL() + fileName +"!"+uploadProperties.getUrlLast();
        return url;
    }


    public static String getFileUUIDName(Part part){
        String s = UUID.randomUUID().toString().replaceAll("-","");
        String filenameExtension = StringUtils.getFilenameExtension(part.getSubmittedFileName());
        return s+"."+filenameExtension;
    }



    public  boolean conformExt(String fileName){
        if (!uploadProperties.getUploadExt().contains(StringUtils.getFilenameExtension(fileName)))
            return false;
        return true;
    }

    public  boolean conformSize(InputStream in){
        try {
            int available = in.available();//獲得的文件字節大小
            int i = available/1024;//拿到kb
            if (uploadProperties.getUploadSize() < i)
                return false;
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public  boolean conformWidthAndHeight(BufferedImage read){
            int width = read.getWidth();
            int height = read.getHeight();
            if (width > uploadProperties.getImgWidth() || height > uploadProperties.getImgHeight())
                return false;
            return true;
    }


}

对应的配置文件

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : Edward_wei
 * @data: 2021/6/26 17:50
 * @classNam: uploadProperties
 * @description:
 * @kit:jdk-1.8
 */
@Data
@ConfigurationProperties(prefix = "aliyun")
@Component
@ToString
public class uploadProperties {
    //阿里雲賬戶的東西
    private  String endpoint;
    private  String accessKeyId;
    private  String accessKeySecret;
    //阿里雲的桶
    private  String bucket;
    private  String baseURL;//基礎路徑
    private  String urlLast;//路徑後綴,加水印
    private  List<String> uploadExt;//文件允許的格式
    private  int uploadSize;//文件的大小
    //文件寬高
    private  int imgWidth;
    private  int imgHeight;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值