判断该文件是否是Excel文档

String suffix = null;
String info = null;
StringTokenizer fileName = new StringTokenizer(file.getFileName(),".",true); //截取
while (fileName.hasMoreElements()) {
suffix = fileName.nextToken();
}
//System.out.println(suffix);
if(“xls”.equals(suffix)) {

		if(isExcelFile(file.getInputStream())) {
			info = manager.updateWeekPredictDemand(queryForm, file , request);
		}else {
			info = "请上传Excel文件(2003或以下版本)(*.xls)";
		}
	}else {
		info = "请上传Excel文件(2003或以下版本)(*.xls)";
	}

//根据excel魔数对excel文件准确判断
private static Boolean isExcelFile(InputStream inputStream) {
boolean result = false;
try {
InputStream fileMagics = FileMagic.prepareToCheckMagic(inputStream);
FileMagic fileMagic = FileMagic.valueOf(fileMagics);

		if(Objects.equals(fileMagic, FileMagic.OLE2) || Objects.equals(fileMagic, FileMagic.OOXML)) {
			result = true;
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		//e.printStackTrace();
	}
	return result;
	
}
上一篇:2021-01-22 Python TimedRotatingFileHandler 修改suffix后无法自动删除文件


下一篇:B. Suffix Operations(思维)