**
工具类
依赖
<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;
}