package com.li.web.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class InvalidFileType {
private static Properties properties = new Properties();
static {
try {
properties.load(Url.class.getClassLoader().getResourceAsStream(
"allowuploadfiletype.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断是否允许上传的文件类型
* @param file 上传的文件
* @param fileContentType 该文件类型
* @return 是否允许上传
*/
public static boolean invalidUploadFile(File file, String contentType) {
if (file != null && file.length() > 0) {
String ext=contentType.substring(contentType.lastIndexOf("/")+1).toLowerCase();
List<String> allowType = new ArrayList<String>();
for (Object key : properties.keySet()) {
String value = (String) properties.get(key);
String[] values = value.split(",");
for (String v : values) {
allowType.add(v.trim());
}
}
return allowType.contains(contentType)&&properties.keySet().contains(ext);
}
return true;
}
根目录下属性文件为
gif=image/gif
jpg=image/jpg,image/jpeg,image/pjpeg
bmp=image/bmp
png=image/png
swf=application/x-shockwave-flash
doc=application/msword
txt=text/plain
xls=application/vnd.ms-excel
ppt=application/vnd.ms-powerpoint
pdf=application/pdf
exe=application/octet-stream