JAVA WEB 判断上传的文件是否是图片

2024-12-23 10:01:35
推荐回答(1个)
回答1:

java web识别文件的类型是通过文件后缀识别的。
写一个识别文件类型的方法:
public String identifyFileTypeUsingFilesProbeContentType(final String fileName) {
String fileType = "Undetermined";
final File file = new File(fileName);
try {
fileType = Files.probeContentType(file.toPath());
} catch (IOException ioException) {
out.println( "ERROR: Unable to determine file type for " + fileName + " due to exception " + ioException);
}
return fileType;
}