<html>
<head>
<script type="text/javascript">
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
function fileChange(target) {
var fileSize = 0;
if (isIE && !target.files) {
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
alert(fileSize);
} else {
fileSize = target.files[0].size;
console.log(fileSize);
}
var size = fileSize / 1024;
if(size>20000){
document.getElementById("upfileSubmit").disabled=true;
alert("附件不能大于20M,请重新选择!");
}
else
{
document.getElementById("upfileSubmit").disabled=false;
}
}
</script>
</head>
<body>
<form name="form" method="post" action="" enctype="multipart/form-data" >
<input type="file" name="filename" size="20" onchange="fileChange(this);">
<input type="submit" value="上传" name="Send" id="upfileSubmit">
</form>
</body>
</html>
String size = "";
if(f.exists() && f.isFile()){
long fileS = f.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileS < 1024) {
size = df.format((double) fileS) + "BT";
} else if (fileS < 1048576) {
size = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
size = df.format((double) fileS / 1048576) + "MB";
} else {
size = df.format((double) fileS / 1073741824) +"GB";
}
}else if(f.exists() && f.isDirectory()){
size = "";
}else{
size = "0BT";
}
/***********************************************************************/
// 计算文件上传的大小 @RequestParam("file") MultipartFile file
MultipartFile files = file;
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());
if (intSize > 20) { // 不能上传超过20M的文件!
return ControllerUtils.error("不能上传超过20M的文件!");
}
}
}
}