一、file文件校验像素,比例,大小
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CheckImagesFormatUtil {
public static boolean checkImageElement(File file, int imageWidth, int imageHeight) throws IOException {
Boolean result = false;
if (!file.exists()) {
return false;
}
BufferedImage bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (bufferedImage != null && height == imageHeight && width == imageWidth) {
result = true;
}
return result;
}
public static boolean checkImageScale(File file, int imageWidth, int imageHeight) throws IOException {
Boolean result = false;
if (!file.exists()) {
return false;
}
BufferedImage bufferedImage = ImageIO.read(file);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (imageHeight != 0 && height != 0) {
int scale1 = imageHeight / imageWidth;
int scale2 = height / width;
if (scale1 == scale2) {
result = true;
}
}
return result;
}
public static boolean checkImageSize(File file, Long imageSize) {
if (!file.exists()) {
return false;
}
Long size = file.length() / 1024;
Long maxImageSize = SettingUtils.get().getMaxImageSize();
if (maxImageSize == null) {
maxImageSize = 5 * 1024L;
} else {
maxImageSize = maxImageSize * 1024;
}
if (size > maxImageSize) {
return false;
}
if (imageSize == null) {
return true;
}
if (size.intValue() <= imageSize) {
return true;
}
return false;
}
}
————————————————
版权声明:本文为CSDN博主「一个猴子请来的救兵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_28505133/article/details/79395944
二、MultipartFile 转 File,删除本地临时文件,file转multipartFile
package com.erp.platform.common.utils.file;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.*;
public class MultipartFileToFile {
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
private static void inputStreamToFile(InputStream ins, File file) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(os != null) {
os.close();
}
if(ins != null) {
ins.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void delteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
public static MultipartFile fileToMultipartFile(File file) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem(file.getName(), "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream fis = new FileInputStream(file);
OutputStream os = item.getOutputStream();
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return new CommonsMultipartFile(item);
}
}
三、校验图片格式
public static String getFileType(MultipartFile file) {
String fileName = queryOriginName(file);
int separatorIndex = fileName.lastIndexOf(".");
if (separatorIndex < 0) {
return "";
}
return fileName.substring(separatorIndex + 1).toLowerCase();
}
public void fileExtensionCheck(MultipartFile multipartFile) {
String extension = getFileType(multipartFile);
String[] picExtension = {"jpg", "jpeg", "png"};
if (com.erp.platform.common.utils.excel.StringUtils.isEmpty(extension) || !Arrays.asList(picExtension).contains(extension)) {
throw new CustomException(ErrorConstant.FILE_TYPE_ERROR + "仅支持" + Arrays.asList(picExtension).stream().collect(Collectors.joining(",")));
}
}