判断文件大小是否可上传

/*
* 判断文件大小是否可上传*/
public Boolean judgeFileSize(MultipartFile file) throws Exception {
Boolean flag = true;
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File f = fi.getStoreLocation();

if (f.exists() && f.isFile()) {
long fileS = f.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileS < 1073741824) {
String size = df.format((double) fileS / 1048576); // + "MB"
// Integer intSize = Integer;
if (size != null) {
Double intSize = Double.parseDouble(size.trim());//文件大小
//获取配置文件中的文件最大限制
Properties prop = new Properties();
InputStream inputStream = DbFH.class.getClassLoader().getResourceAsStream("dbconfig.properties");
prop.load(inputStream);
inputStream.close();
String str = prop.getProperty("FileMaxSize").trim();//文件限制大小
if (intSize > Double.valueOf(str)) { // 不能上传超过10M的文件!
flag = false;
}
}
}
}
return flag;
}

猜你喜欢

转载自www.cnblogs.com/g-yang/p/9957670.html