===============================直接截取后缀方式判断文件格式===================================
String originalFileName = MultipartFile.getOriginalFilename();
String substring = originalFileName.substring(originalFileName.lastIndexOf("."));
===============================byte[]方式判断文件格式========================================
import jline.internal.Log;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.MemoryCacheImageInputStream;
import java.io.*;
import java.util.Iterator;
public class TestImage {
public static void main(String[] args) throws IOException {
File sourceImage = new File("d:\\\\test.png");
byte[] imageByte = File2byte(sourceImage);
String strImage = getImageType(imageByte);
Log.info("strImage:"+strImage);
}
/**
* 判断图片文件格式
*
* @param mapObj
* @return
* @throws IOException
*/
public static String getImageType(byte[] mapObj) throws IOException {
// FileOutputStream fout = new FileOutputStream("d:\\test.jpg");
// //将字节写入文件
// fout.write(mapObj);
// fout.close();
//支持的文件类型: ["gif", "jpeg", "jpg", "bmp", "png"],
// if (Base64.decode(requestDTO.getCreditImg()).length > StringUtil.sizeImge) {
//大于1M
// throw new RestException(ErrorDetailEnum.FILE_SIZE_ERROR, WatchLogType.MERCHANTS_IN_CENTER);//文件过大
// }
String type = "";
ByteArrayInputStream bais = null;
MemoryCacheImageInputStream mcis = null;
try {
bais = new ByteArrayInputStream(mapObj);
mcis = new MemoryCacheImageInputStream(bais);
Iterator itr = ImageIO.getImageReaders(mcis);
while (itr.hasNext()) {
ImageReader reader = (ImageReader) itr.next();
String imageName = reader.getClass().getSimpleName();
if (imageName != null) {
if ("GIFImageReader".equals(imageName)) {
type = "gif";
} else if ("JPEGImageReader".equals(imageName)) {
type = "jpg";
} else if ("PNGImageReader".equals(imageName)) {
type = "png";
} else if ("BMPImageReader".equals(imageName)) {
type = "bmp";
} else {
type = "noPic";
}
}
}
} catch (Exception e) {
type = "noPic";
} finally {
if (bais != null) {
try {
bais.close();
} catch (IOException ioe) {
type = "noPic";
}
}
if (mcis != null) {
try {
mcis.close();
} catch (IOException ioe) {
type = "noPic";
}
}
}
return type;
}
/**
* 将文件转换成byte数组
* @param filePath
* @return
*/
public static byte[] File2byte(File tradeFile) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2016-05-06 Redis